图像上传在byethost服务器中不起作用

I have written code on image upload, but it is not working in byethost server. I created "uploads/products" directory under root location of server, the folder is also writable, but still it is not working. Please help.

I turned on error logging for the application. If I visit "application/logs" directory's log file following error is shown.

ERROR - 2018-03-25 00:47:08 --> 404 Page Not Found: /index

ERROR - 2018-03-25 00:58:24 --> The upload path does not appear to be valid.

Picture of image upload directory in byethost server

enter image description here

Code of my Product Controller.

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());
    }

}   

Check upload_tmp_dir permissions (permissions for user, that PHP uses). Native file uploading comes in two steps: uploading to tmp dir and moving to whatever you want.

Also be sure you are trying to upload file that meets upload limit.

Create file upload_form.php & place it application/views/ folder:

<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>

Create upload_success.php and place it application/views/ folder:

<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>

Name it as upload.php and place it in application/controllers/ folder:

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);
        }
    }
}

The REST procedure for code igniter is the same as usual. Create an upload folder in the /htdocs/codeigniter/ folder. If you're using Windows, create an upload folder in /var/www/html/codeigniter/ folder .

The output should look like this.

    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"

There are two possible problems apart from sheer bad configuration which I don't see here:

  1. One of the folders in the tree is not readable, i.e. both the 'uploads' and 'products' directory have to be readable and of course writable.

  2. Some other system error is causing the directory access to fail, e.g. not enough disk space or selinux permissions.

you can change the server upload directory 777 permissions,this can resolve your issue

Try updating your .htaccess with following

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]