Welcome Guest, Not a member yet? Register   Sign In
File Uploading Problem: "You did not select a file to upload."
#1

[eluser]Unknown[/eluser]
Hi all,

I am trying to use CodeIgniter to upload a thumbnail file for use in my application. The strange part is that it used to be working perfectly, but then when I added a 'max_width' and 'max_height' parameter, it stopped working. Even commenting out those options now leads to the same problem. Here's the issue: when I try to upload an image, I get the error "You did not select a file to upload."

My view code is as follows:
Code:
echo form_open_multipart('trax/create_character');
...
echo '<tr><td>'.form_label('Thumbnail Image: ', 'ThumbnailURL').'<br/>(should be 85x85)</td>';
echo '<td>'.form_upload('ThumbnailURL', 'ThumbnailURL').'</td></tr>';
...
echo form_close();

My controller code is as follows:
Code:
if (isset($_FILES['ThumbnailURL']) != FALSE) {
    log_message('error', print_r($_FILES, true));
    $upload_config = $this->get_upload_config();
    //$upload_config['max_width'] = 85;
    //$upload_config['max_height'] = 85;
    $this->load->library('upload', $upload_config);
    if ($this->upload->do_upload('ThumbnailURL') == false) {
     $errors = $this->upload->display_errors('<p class="warning">', '</p>');
     $this->load->view('character_create_form', array('UserId' => $this->UserId, 'errors' => $errors));
     return;
    } else {
     $data = $this->upload->data();
     $_POST['ThumbnailURL'] = $data['file_name'];
    }
   }

And from the log file (showing the $_FILES array) after trying to upload an image:
Code:
ERROR - 2012-09-21 09:09:59 --&gt; Array
(
    [ThumbnailURL] => Array
        (
            [name] => image.jpg
            [type] => image/jpeg
            [tmp_name] => /var/tmp/phpw2OQ6E
            [error] => 0
            [size] => 2936
        )
)

So you can see that the $_FILES array is setup as expected, with the correct key. What's more is that the file is actually properly uploaded to my "uploads" directory. So I know there isn't a problem with the do_upload call (as Google indicates is the most common cause of this error).

To try to get more to the bottom of this, I placed a few log_message statements inside the File Uploading library itself (in /system/libraries/), and I found a very strange situation. It seems the error is caused by the following code:

Code:
if ( ! is_uploaded_file($_FILES[$field]['tmp_name']))
    $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];

I did a log_message statement here and saw that the $error is in fact 4, which the subsequent switch statement leads to
Code:
$this->set_error('upload_no_file_selected')
.

So that's why I'm seeing my error. But the strange part is that this error should lead the function to return FALSE right there. But I am seeing my file actually uploaded to the correct directory, which it seems to me occurs later on in the do_upload() function when it calls copy(...) or move_uploaded_file(...).

Am I going crazy here? Any ideas or suggestions of things to try?

Thanks in advance.
#2

[eluser]Unknown[/eluser]
Turns out I was not going crazy, but I was ignoring an important part of my code. I was actually uploading two images in this manner, and the code I was using to check if it was populated was not sufficient.

Code:
if (isset($_FILES['ThumbnailURL']) != FALSE) {
    // .. upload code
}
if (isset($_FILES['ImageURL']) != FALSE) {
    // .. upload code
}

It turns out that the $_FILES array is set up for all file fields, even when the user does not select a file. So in my case, the ImageURL field was setup but there was no file for it, hence creating the error message that I experienced.

If anyone else is having a similar problem, make sure you don't rely on the non-existence of the $_FILES key when checking whether to upload a file or not.




Theme © iAndrew 2016 - Forum software by © MyBB