简体   繁体   English

$ _FILES字段'tmp_name'在.JPG文件扩展名上没有任何值

[英]$_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. 当我用这个扩展名测试图像文件时,我正在制作一个上传脚本.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 eg 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. 所以当我决定上传一个图像文件例如sample.JPG时出现了这个问题,我认为这些文件大部分来自相机< - 这就是我的想法,但更重要的是我可以上传它们。 JPG,.jpg,jpeg,.png。 all of them works fine except for .JPG. 除了.JPG,所有这些都很好。

Main problem is that field ['tmp_name'] in $_FILES has no values when .JPG is to be uploaded. 主要问题是$ _FILES中的字段['tmp_name']在上传.JPG时没有值。

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. 如果$_FILES[$field]['tmp_name']为空,则表示文件尚未上传。 You should look at $_FILES[$field]['error'] to see why. 您应该查看$_FILES[$field]['error']来了解原因。

FWIW, and as far as I understand it, the mime-type in $_FILES[] is provided by the browser. FWIW,据我所知,浏览器提供$_FILES[]的mime类型。

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

Check your php.ini and 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 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. 我还要说,如果您上传的文件大于php.ini upload_max_filesize设置,那么PHP不会在错误日志中报告错误。 For example, if you upload a 6MB file when you have it set at 2M (which I think is the default). 例如,如果您将其设置为2M (我认为是默认值),则上传6MB文件。

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. 记住在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. 我希望这可以帮助别人。

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. 可能导致此问题的另一个原因是您在之前的评论中提到的php.ini设置。 You should can post_max_size = 200M to php.ini file too. 你也应该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". 如果您在像我这样的Windows上进行开发,您可以看到一个转储文件,在C:\\ Windows \\ Temp中显示错误,称为“php ** VERSION ** _ errors.log”。 It helps. 它有助于。

I faced the issue with $_FILES field 'tmp_name' having no value for .JPG file extension and successfully fixed it in few steps. 我遇到了$ _FILES字段'tmp_name'没有.JPG文件扩展名的问题,并且只需几步即可成功修复它。 These measures may help someone in the future. 这些措施可能有助于将来的某些人。

  1. First of all, I implemented the Switch Case solution offered by the user 'staticsan'. 首先,我实现了用户'staticsan'提供的Switch Case解决方案。 Link here - https://stackoverflow.com/a/14472801/3681985 . 链接在这里 - https://stackoverflow.com/a/14472801/3681985 This solution helped me trace the potential issues such as parameters in php.ini file and folder permissions. 这个解决方案帮助我跟踪潜在的问题,如php.ini文件中的参数和文件夹权限。
  2. The php.ini file was named php.ini.default. php.ini文件名为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. 在将文件重命名为php.ini之前,更改参数upload_max_filesize和post_max_size不会产生任何结果。 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. 我已经更改了权限,并且能够看到上传到该文件夹​​的图像。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP $ _FILES [&#39;file&#39;] [&#39;tmp_name&#39;]:如何保留文件名和扩展名? - PHP $_FILES['file']['tmp_name']: How to preserve filename and extension? $ _FILES [“file”] [“tmp_name”]返回空值 - $_FILES[“file”][“tmp_name”] return blank value 什么时候会删除$ _FILES [&#39;file&#39;] [&#39;tmp_name&#39;]? - When will $_FILES['file']['tmp_name'] be removed? 为$ _FILES选择值[ <filename> ] [“ tmp_name”] - Choose value for $_FILES[<filename>][“tmp_name”] $ _FILES [&#39;file&#39;] [&#39;tmp_name&#39;] +从tmp图像中裁剪 - $_FILES['file']['tmp_name'] + Cropping from the tmp Image 是否可以使用javascript从文件字段中获取$ _FILES [“inputID”] [“tmp_name”]? - Is it possible to get $_FILES[“inputID”][“tmp_name”] from a file field using javascript? PHP文件上载错误(UPLOAD_ERR_PARTIAL)/ $ _FILES为空或具有空“tmp_name” - PHP file upload error (UPLOAD_ERR_PARTIAL) / $_FILES is null or has empty“tmp_name” 如何将 imagejpeg() 与 $_FILES[&quot;file&quot;][&quot;tmp_name&quot;] 一起使用 - How do I use imagejpeg() with $_FILES["file"]["tmp_name"] 您可以使用$ _FILES [tmp_name]记住文件位置吗? - Can you use $_FILES[tmp_name] for remembering file location? PHP codeigniter 文件上传 $_FILES[&#39;pic&#39;][&#39;tmp_name&#39;]; - PHP codeigniter file upload $_FILES['pic']['tmp_name'];
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM