Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

php - $_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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.';
                }
            }
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[4] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

56.9k users

...