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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14472741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:20:35  来源:igfitidea点击:

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

phpfile-upload

提问by lemoncodes

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.

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

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.

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

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.

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

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

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

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

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

回答by staticsan

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

如果$_FILES[$field]['tmp_name']为空,则文件尚未上传。你应该看看$_FILES[$field]['error']为什么。

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

FWIW,据我所知,mime-type in$_FILES[]是由浏览器提供的。

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

回答by crmpicco

Check your php.iniand in particular this setting

检查您的php.ini,特别是此设置

; 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:

或者在您的 Apache 配置中执行此操作:

<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_filesizesetting. For example, if you upload a 6MB file when you have it set at 2M(which I think is the default).

我还要说,如果您上传的文件大于 php.iniupload_max_filesize设置,PHP 不会在错误日志中报告错误是很糟糕的。例如,如果您将 6MB 文件设置为 6MB 2M(我认为这是默认设置)。

回答by Reasurria

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.

请记住在 php.ini 中设置最大文件大小上限后重新启动服务器。我在这个问题上花了几个小时认为这不是文件大小问题,同时我忘记重新启动。重启后一切正常。

I hope this can help someone.

我希望这可以帮助某人。

回答by Alper ?zhan

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.

我遇到了同样的问题,并且主要熟悉 .NET 平台会让你忘记客户端 html 中发生的事情。

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

我的问题是我的表单有一个 MAX_FILE_SIZE 隐藏输入,它的某些值小于文件的等效字节。

Your form should have this;

你的表格应该有这个;

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

除此之外,您的表单标签必须包含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.

我认为最大文件大小以 kb 为单位,但由于 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 = 200Mto php.ini file too.

可能导致此问题的另一个原因是人们在之前的评论中提到的 php.ini 设置。你也应该可以post_max_size = 200M到 php.ini 文件。

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.

如果您像我一样在 Windows 上进行开发,您可以看到一个在 C:\Windows\Temp 显示错误的转储文件,称为“php**VERSION**_errors.log”。它有助于。

回答by Sriram

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.

我遇到了 $_FILES 字段 'tmp_name' 对于 .JPG 文件扩展名没有值的问题,并通过几个步骤成功修复了它。这些措施将来可能会对某人有所帮助。

  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.
  1. 首先,我实现了用户 'staticsan' 提供的 Switch Case 解决方案。链接在这里 - https://stackoverflow.com/a/14472801/3681985。该解决方案帮助我跟踪了潜在问题,例如 php.ini 文件中的参数和文件夹权限。
  2. php.ini 文件被命名为 php.ini.default。更改参数 upload_max_filesize 和 post_max_size 没有产生任何结果,直到我将文件重命名为 php.ini。请记住对参数值进行试验。
  3. 修复文件名问题后,我遇到了对必须移动上传的临时图像的文件夹的权限的挑战。我已经更改了权限并且能够看到上传到文件夹中的图像。

回答by vinu

Just try this and see what happens,

试试这个,看看会发生什么

if (($_FILES['file']['type']) == "image/jpg" || ($_FILES['file']['type']) == "image/jpeg") {
                      //do uploading stuff here
                    }else{
                    echo 'File is not a valid JPG,JPEG';
                    }

回答by BanguluruKtm

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.

似乎这是一个随机问题,因为当我制作一个脚本来上传 CSV 文件时,一些 CSV 文件上传没有问题,但在其他情况下,$_FILES['file'][tmp_name]会是空的。