6

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 errors, 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));
}
John Beech
  • 61
  • 1
  • 2
  • @John Beech - Can you add an `ini_set('display_errors', true);` and `error_reporting(E_ALL);` directly below `echo "C";` and let us know if anything shows up? – Francois Deschenes Jun 22 '11 at 04:41
  • @Francois - Sure, will test that now. I've got the above code working in the other project, but when I copy back I get the same issue - so it looks like it might be a project wide config issue. – John Beech Jun 22 '11 at 04:43
  • @Francois - Still nothing, never makes it to "D": ` echo "C"; ini_set('display_errors', true); error_reporting(E_ALL); $result = $this->upload->do_upload('photo_filedata');` echo "D";` – John Beech Jun 22 '11 at 04:46
  • @John Beech - Are you sure that the folder is writeable? I use CodeIgniter on Linux and OSX and your code seems like it would work. The only other thing that comes to mind is that the path should be using \ instead of /. – Francois Deschenes Jun 22 '11 at 04:47
  • @Francois - Windows 7 users: SYSTEM, Administrators, Me - Permission Full control. Same for both image/uploads/ in each project – John Beech Jun 22 '11 at 04:49
  • I would assume that the user that Apache/IIS runs under will need permission as well. You should try to give read/write access to "Everyone". – Francois Deschenes Jun 22 '11 at 04:50
  • I've just added debug lines to: libraries/Upload.php, it fails during this: // Is the file type allowed to be uploaded? if ( ! $this->is_allowed_filetype()) { $this->set_error('upload_invalid_filetype'); return FALSE; } – John Beech Jun 22 '11 at 04:55
  • 3
    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 Jun 22 '11 at 05:01
  • @John Beech - Do you have `gd` installed? Can you make sure that the module is loaded? – Francois Deschenes Jun 22 '11 at 05:01
  • @John Beech - I'm glad you found it. :) – Francois Deschenes Jun 22 '11 at 05:02

3 Answers3

1
$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!

srbhbarot
  • 1,317
  • 12
  • 16
0

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

praneeth
  • 535
  • 1
  • 6
  • 17
  • No, that works fine in the original code: // working code from other project: $config['upload_path'] = './images/uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = 1024 * 3; $config['max_width'] = 0; $config['max_height'] = 0; – John Beech Jun 22 '11 at 04:28
0

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

Raj
  • 22,346
  • 14
  • 99
  • 142
Markavian
  • 84
  • 4