$_FILES field 'tmp_name' has no value on .JPG file extension

前端 未结 7 2004
时光取名叫无心
时光取名叫无心 2020-11-29 07:53

i was making an upload script when i tested an image file wit this extension .JPG, i don\'t know whats the difference between jpg or jpeg, but it seems that $_FILES don\'t r

相关标签:
7条回答
  • 2020-11-29 08:35

    Posting an answer because my rating is too low.

    Remember to restart your server after setting the max file size cap in your php.ini. I spent hours on this issue thinking that it wasn't a file size problem meanwhile I forgot to restart. After restart everything worked.

    I hope this can help someone.

    0 讨论(0)
  • 2020-11-29 08:38

    If $_FILES[$field]['tmp_name'] is empty then the file hasn't been uploaded. You should look at $_FILES[$field]['error'] to see why.

    FWIW, and as far as I understand it, the mime-type in $_FILES[] is provided by the browser.

    Update: here is a bit of potted code to handle all file upload errors:

            $message = 'Error uploading file';
            switch( $_FILES['newfile']['error'] ) {
                case UPLOAD_ERR_OK:
                    $message = false;;
                    break;
                case UPLOAD_ERR_INI_SIZE:
                case UPLOAD_ERR_FORM_SIZE:
                    $message .= ' - file too large (limit of '.get_max_upload().' bytes).';
                    break;
                case UPLOAD_ERR_PARTIAL:
                    $message .= ' - file upload was not completed.';
                    break;
                case UPLOAD_ERR_NO_FILE:
                    $message .= ' - zero-length file uploaded.';
                    break;
                default:
                    $message .= ' - internal error #'.$_FILES['newfile']['error'];
                    break;
            }
            if( !$message ) {
                if( !is_uploaded_file($_FILES['newfile']['tmp_name']) ) {
                    $message = 'Error uploading file - unknown error.';
                } else {
                    // Let's see if we can move the file...
                    $dest .= '/'.$this_file;
                    if( !move_uploaded_file($_FILES['newfile']['tmp_name'], $dest) ) { // No error supporession so we can see the underlying error.
                        $message = 'Error uploading file - could not save upload (this will probably be a permissions problem in '.$dest.')';
                    } else {
                        $message = 'File uploaded okay.';
                    }
                }
            }
    
    0 讨论(0)
  • 2020-11-29 08:43

    I faced the issue with $_FILES field 'tmp_name' having no value for .JPG file extension and successfully fixed it in few steps. These measures may help someone in the future.

    1. First of all, I implemented the Switch Case solution offered by the user 'staticsan'. Link here - https://stackoverflow.com/a/14472801/3681985. This solution helped me trace the potential issues such as parameters in php.ini file and folder permissions.
    2. The php.ini file was named php.ini.default. Changing the parameters upload_max_filesize and post_max_size didn't yield any result, until I renamed the file to php.ini. Remember to experiment with parameter values.
    3. After fixing the file name issue, I encountered a challenge with the permissions to the folder in which the uploaded temp image has to be moved. I have changed the permissions and was able to see the image uploaded in to the folder.
    0 讨论(0)
  • 2020-11-29 08:45

    I was having the same problem and being familiar with mostly .NET platform makes you forget about stuff that happens in the client side html.

    My problem was my form is having a MAX_FILE_SIZE hidden input which has some value lesser than file's equavalent bytes.

    Your form should have this;

    Other than that, your form tag must include enctype="multipart/form-data"

    I was thining that max file size was in kb, but it's in bytes, thanks to some other page in stackoverflow.

    The other reason that might cause this problem your php.ini settings that people have mentioned in previous comments. You should can post_max_size = 200M to php.ini file too.

    If you are developing on Windows like me, you can see a dump file that showing errors at C:\Windows\Temp called as "php**VERSION**_errors.log". It helps.

    0 讨论(0)
  • 2020-11-29 08:54

    Check your php.ini and in particular this setting

    ; Maximum allowed size for uploaded files.
    ; http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize
    upload_max_filesize = 6M
    

    Or do this in your Apache Config:

    <Directory "/var/www/vhosts/path/to/your/directory/import/">
        php_value post_max_size 6M
        php_value upload_max_filesize 6M
    </Directory>
    

    I would also say that it is poor that PHP doesn't report an error in the error logs if you upload a file that is larger than your php.ini upload_max_filesize setting. For example, if you upload a 6MB file when you have it set at 2M (which I think is the default).

    0 讨论(0)
  • 2020-11-29 08:55

    Seems as though it's a random problem because while I was making a script to upload CSV files, some CSV files would have no problem uploading but in other cases, $_FILES['file'][tmp_name] would be empty.

    0 讨论(0)
提交回复
热议问题