【问题标题】:Image upload not working in byethost server图片上传在 byethost 服务器中不起作用
【发布时间】:2018-03-25 06:04:57
【问题描述】:

我已经编写了图片上传的代码,但它在 byethost 服务器中不起作用。我在服务器的根目录下创建了"uploads/products" 目录,该文件夹也是可写的,但仍然无法正常工作。请帮忙。

我为应用程序打开了错误日志记录。如果我访问"application/logs" 目录的日志文件,则会显示以下错误。

错误 - 2018-03-25 00:47:08 --> 404 页面未找到:/index

ERROR - 2018-03-25 00:58:24 --> 上传路径似乎不是 有效。

bytehost服务器中图片上传目录的图片

我的产品控制器的代码。

if(isset($_FILES['productimage']['name'])){
    $filename = $_FILES['productimage']['name']; 
    $config['file_name'] = $filename;
    $config['upload_path'] = realpath(FCPATH.'uploads/products/');
    $config['allowed_types'] = 'jpg|jpeg|png';    
    $config['remove_spaces'] = TRUE;
    $config['encrypt_name'] = TRUE;
    $config['min_width'] = 360;
    $config['min_height'] = 360;
    $config['max_width'] = 360;
    $config['max_height'] = 360;
    $this->upload->initialize($config);
    if($this->upload->do_upload('productimage')){
        $uploadeddata = $this->upload->data();
        $data['image'] = $uploadeddata['file_name'];
    }else{
            $this->session->set_flashdata('error', $this->upload->display_errors());
            $result = array("status"=>false, "message"=>$this->upload->display_errors());
    }

}   

【问题讨论】:

  • 试试 $config['upload_path'] = 'application/uploads/products/';
  • $config['upload_path'] 的值显示为“/home/vol15_4/byethost14.com/b14_21532169/htdocs/uploads/products”
  • 当我尝试 $config['upload_path'] = 'application/uploads/products/' 时,日志生成错误“无法将文件写入磁盘。”
  • 您的托管计划可能磁盘空间不足

标签: php codeigniter-3


【解决方案1】:

检查upload_tmp_dir 权限(PHP 使用的用户权限)。原生文件上传分两步:上传到 tmp 目录和移动到你想要的任何地方。

还要确保您尝试上传符合upload limit 的文件。

【讨论】:

  • 为什么我需要检查upload_tmp_dir权限? Codeigniter 使用文件上传库,当我在本地服务器上尝试相同的图像上传代码时,它工作了!!
  • 因为您的本地服务器使用本地 php.ini 和本地文件系统。尝试调查整个上传路径 - 这将帮助您了解究竟是什么失败了。
【解决方案2】:

除了我在这里看不到的糟糕配置之外,还有两个可能的问题:

  1. 树中的一个文件夹不可读,即“上传”和“产品”目录都必须是可读的,当然也是可写的。

  2. 其他一些系统错误导致目录访问失败,例如没有足够的磁盘空间或 selinux 权限。

【讨论】:

  • 我已将“上传”和“产品”目录设置为具有读写权限。但它仍然无法正常工作。
【解决方案3】:

你可以更改服务器上传目录777权限,这样可以解决你的问题

【讨论】:

    【解决方案4】:

    尝试使用以下更新您的 .htaccess

    RewriteEngine On
    RewriteBase /
    RewriteCond %{SERVER_PORT} 80 
    RewriteRule ^(.*)$ http://your-website.com/$1 [R,L]
    RewriteRule ^(system|application|uploads|cgi-bin) - [F,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* index.php/$0 [PT,L]
    

    【讨论】:

    • 它不工作,它显示“此页面不工作,重定向你太多次”。
    • @NileshSanyal 您是否在 RewriteRule 行提供了完整的 url?
    • 是的,我做到了。
    • @NileshSanyal 你在 HTTP 和 HTTPS 版本上都有吗?
    【解决方案5】:

    创建文件upload_form.php 并将其放置在application/views/ 文件夹中:

    <html>
        <head>
            <title>Upload Form</title>
        </head>
        <body>
            <?php echo $error;?>
            <?php echo form_open_multipart('upload/do_upload');?>
                <input type="file" name="userfile" size="20" />
                <br /><br />
                <input type="submit" value="upload" />
            </form>
        </body>
    </html>
    

    创建upload_success.php 并将其放置在application/views/ 文件夹中:

    <html>
        <head>
            <title>Upload Form</title>
        </head>
        <body>
            <h3>Your file was successfully uploaded!</h3>
            <ul>
                <?php foreach ($upload_data as $item => $value):?>
                    <li><?php echo $item;?>: <?php echo $value;?></li>
                <?php endforeach; ?>
            </ul>
            <p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
        </body>
    </html>
    

    将其命名为upload.php 并将其放在application/controllers/ 文件夹中:

    class Upload extends CI_Controller {
        public function __construct(){
            parent::__construct();
            $this->load->helper(array('form', 'url'));
        }
    
        public function index(){
            $this->load->view('upload_form', array('error' => ' ' ));
        }
    
        public function do_upload(){
            $config['upload_path']          = './upload/';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['remove_spaces'] = TRUE;
            $config['encrypt_name'] = TRUE;
            $config['max_size']             = 100;
            $config['max_width']            = 1024;
            $config['max_height']           = 768;
    
            $this->load->library('upload', $config);            
            $this->upload->initialize($config);
    
    
            if ( ! $this->upload->do_upload('userfile')){
                $error = array('error' => $this->upload->display_errors());
                $this->load->view('upload_form', $error);
            } else {
                $data = array('upload_data' => $this->upload->data());
    
                $this->load->view('upload_success', $data);
            }
        }
    }
    

    代码点火器的 REST 过程与往常相同。在/htdocs/codeigniter/ 文件夹中创建一个上传文件夹。如果您使用的是 Windows,请在 /var/www/html/codeigniter/ 文件夹中创建一个上传文件夹。

    输出应该是这样的。

        Your file was successfully uploaded!
        file_name: 6cedb4d846cade717196c96c155537fd.png
        file_type: image/png
        file_path: /var/www/html/ci/upload/
        full_path: /var/www/html/ci/upload/6cedb4d846cade717196c96c155537fd.png
        raw_name: 6cedb4d846cade717196c96c155537fd
        orig_name: download.png
        client_name: download.png
        file_ext: .png
        file_size: 4.76
        is_image: 1
        image_width: 126
        image_height: 126
        image_type: png
        image_size_str: width="126" height="126"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多