开发者

Codeigniter do_upload() silently fails on "success"

开发者 https://www.devze.com 2023-03-15 00:14 出处:网络
I\'m using CodeIgniter to write an image upload form. I\'ve previously got similar code to work for a different site. At the moment - the code for receiving a multipart/form-data image is failing sile

I'm using CodeIgniter to write an image upload form. I've previously got similar code to work for a different site. At the moment - the code for receiving a multipart/form-data image is failing silently. While configuring the server/script I received error开发者_运维问答s, such as incorrect filepath, dissalowed mime-types, but now I get nothing.

The code below returns: "ABC" and fails before "D" without error.

If I change 'photo_filedata' to 'photo_filedata2' I get a more useful error: "ABCD You did not select a file to upload."

I am at a complete loss to debugging this, since I'm getting no error at all from the server.

Does anyone know what might be happening?

Server: WAMP, running on Windows 7. Have an existing project that does file uploads with no problem.

function upload_photo()
{
    echo "A";

    $config['upload_path'] = './images/uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['file_name'] = 'photo_' . substr(md5(time()), 0, 16);
    $config['max_size']    = 2000;
    $config['max_width']  = 0;
    $config['max_height']  = 0;

    echo "B";

    $this->load->library('upload', $config);

    echo "C";

    $result = $this->upload->do_upload('photo_filedata');

    echo "D";

    if (!$result)
    {
        $error = $this->upload->display_errors();
        $data = false;
    }   
    else
    {
        $error = false;
        $data = $this->upload->data();
    }

    $this->load->view('home-photo-upload', array('error' => $error, 'data' => $data));
}


$config['upload_path'] = 'uploads/category/'.$id.'/';
        //echo $file_name;die;
        //echo $config['upload_path'];
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = '750';
        $config['max_width'] = '1920';
        $config['max_height'] = '1280';
        $this->load->library('upload');
         foreach ($_FILES as $key => $value) {
            //print_r($key);

            if (!empty($key['name'])) {

                $this->upload->initialize($config);
                if (!$this->upload->do_upload($key)) {
                  // echo 'test';die;
//                    rmdir('uploads/category/'.$id);
                    $errors = $this->upload->display_errors();
                    flashMsg($errors);
                }
}
}

try this!


your giving maxwidth and maxheight as zero may be that was the problem check it.


(Reposting from comments): There was a typo/bug in system/application/config/mimes.php which caused the is_allowed_filetype() method to fail in do_upload();

Erronous: 
        ...
        'gif' => array('image/gif', 'application/octet-stream'),
        'jpeg' => array('image/jpeg', 'image/pjpeg', 'application/octet-stream'),
        'jpg' => array('image/jpeg', 'image/pjpeg', 'application/octet-stream'),
        'jpe' => array('image/jpeg', 'image/pjpeg', 'application/octet-stream'),
        'png' => array('image/png', 'image/x-png', 'application/octet-stream' 
    );

// missing ) for final entry. – John Beech (aka Markavian)

Any errors in the CodeIgniter configuration files seem to fail silently, so any further errors are not reported. Found problem by debugging internal CI method to find out which line failed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号