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

前端 未结 7 2031
时光取名叫无心
时光取名叫无心 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: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.';
                    }
                }
            }
    

提交回复
热议问题