Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 recognize this file type.

I've read several threads that $_FILES ins't that reliable when it comes to mime type, so i decided to used the php's mime type function mime_content_type(), php's getimagesize(), pathinfo(), though pathinfo returns a file name, and type, but i need the path of the file which is NOT present, all of the functions are being passed with $_FILES['file']['tmp_name'] as parameters.

So this problem came up when i decided to upload an image file e.g sample.JPG, i think most of this files are raw from the camera <-- that's what i think though but nevertheless what is more important is that i can upload them .JPG, .jpg, jpeg, .png. all of them works fine except for .JPG.

Main problem is that field ['tmp_name'] in $_FILES has no values when .JPG is to be uploaded.

Any of you guys who have encountered this problem please do share your workaround or "how did you do it" kind of thing.

like image 899
lemoncodes Avatar asked Jan 23 '13 04:01

lemoncodes


People also ask

What is $_ files [' file Tmp_name ']?

$_FILES['file']['tmp_name'] - The temporary filename of the file in which the uploaded file was stored on the server.

What does Tmp_name mean in PHP?

tmp_name is the temporary name of the uploaded file which is generated automatically by php, and stored on the temporary folder on the server.

How to get upload file path in PHP?

header("Content-Length: ". filesize($filePath)); Where $filePath should be absolute path to file not just file handle.

What is $_ files in PHP?

$_FILES is a super global variable which can be used to upload files. Here we will see an example in which our php script checks if the form to upload the file is being submitted and generates a message if true.


4 Answers

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.';
                }
            }
        }
like image 89
staticsan Avatar answered Oct 25 '22 02:10

staticsan


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

like image 28
crmpicco Avatar answered Oct 25 '22 03:10

crmpicco


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.

like image 5
Reasurria Avatar answered Oct 25 '22 04:10

Reasurria


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.

like image 3
alper Avatar answered Oct 25 '22 03:10

alper