$_FILES 字段 'tmp_name' 对 .JPG 文件扩展名没有值

2021-12-24 00:00:00 file-upload php

当我测试带有此扩展名 .JPG 的图像文件时,我正在制作上传脚本,我不知道 jpg 或 jpeg 之间有什么区别,但似乎 $_FILES 无法识别这种文件类型.

我读过几个线程,说 $_FILES 在 mime 类型方面并不可靠,所以我决定使用 php 的 mime 类型函数 mime_content_type(),php 的 getimagesize(), pathinfo(),虽然路径信息返回文件名和类型,但我需要不存在的文件路径,所有函数都通过 $_FILES['file']['tmp_name'] 作为参数.

所以当我决定上传一个图像文件时出现了这个问题,例如 sample.JPG,我认为这些文件中的大部分都是来自相机的原始文件 <-- 这就是我的想法,但更重要的是我可以上传 .JPG、.jpg、jpeg、.png.除了 .JPG 之外,所有这些都可以正常工作.

主要问题是在上传.JPG 时$_FILES 中的字段['tmp_name'] 没有值.

遇到此问题的任何人都请分享您的解决方法或您是如何做到的"之类的事情.

解决方案

如果 $_FILES[$field]['tmp_name'] 为空,则文件尚未上传.您应该查看 $_FILES[$field]['error'] 以了解原因.

FWIW,据我所知,$_FILES[] 中的 MIME 类型是由浏览器提供的.

更新:这里有一些处理所有文件上传错误的代码:

 $message = '上传文件时出错';开关($_FILES['newfile']['error']){情况 UPLOAD_ERR_OK:$消息=假;;休息;案例 UPLOAD_ERR_INI_SIZE:案例 UPLOAD_ERR_FORM_SIZE:$message .= ' - 文件太大('.get_max_upload().'字节限制).';休息;案例 UPLOAD_ERR_PARTIAL:$message .=' - 文件上传未完成.';休息;案例 UPLOAD_ERR_NO_FILE:$message .=' - 上传的文件长度为零.';休息;默认:$message .= ' - 内部错误 #'.$_FILES['newfile']['error'];休息;}如果(!$消息){if(!is_uploaded_file($_FILES['newfile']['tmp_name'])) {$message = '上传文件时出错 - 未知错误.';} 别的 {//让我们看看我们是否可以移动文件...$dest .='/'.$this_file;if( !move_uploaded_file($_FILES['newfile']['tmp_name'], $dest) ) {//没有错误支持,所以我们可以看到潜在的错误.$message = '上传文件时出错 - 无法保存上传(这可能是 '.$dest.' 中的权限问题)';} 别的 {$message = '文件上传正常.';}}}

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.

解决方案

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

相关文章