简体   繁体   English

PHP文件上载错误(UPLOAD_ERR_PARTIAL)/ $ _FILES为空或具有空“tmp_name”

[英]PHP file upload error (UPLOAD_ERR_PARTIAL) / $_FILES is null or has empty“tmp_name”

I have a quite common problem and spent hours to read some things about it, but I don't manage to fix it and I'm going crazy... 我有一个相当普遍的问题,并花了几个小时阅读有关它的一些事情,但我没有设法解决它,我会发疯...

Context 上下文

I have a website where users can upload a picture to my server (from iOS / Safari browser in the common case). 我有一个网站,用户可以将图片上传到我的服务器 (在常见情况下从iOS / Safari浏览器)。 The page with the upload form is this one . 包含上传表单的页面就是这个页面。

The backend is made with PHP (no framework). 后端是用PHP(没有框架)制作的。 Front-end is HTML / Javascript. 前端是HTML / Javascript。 This is a quite old site I've made in 2010 so this is not "super sexy"! 这是我在2010年制作的一个非常古老的网站,所以这不是“超级性感”! :-) :-)

I made logs to monitor upload results, and I see that about 30% of user uploads failed ... I guess this is not usual! 我制作了日志来监控上传结果,我发现大约有30%的用户上传失败了 ...我想这不常见!

These errors are from 2 origins (same user trying to upload several times can have a mix of these 2 origins) : 这些错误来自2个来源(尝试上传多次的同一用户可以混合使用这两个来源):

  • $_FILES['myFormFieldName'] is NULL $_FILES['myFormFieldName']NULL
  • $_FILES['myFormFieldName'] is NOT NULL but $_FILES['myFormFieldName']['error'] is 3 ( UPLOAD_ERR_PARTIAL ) with $_FILES['xxx']['tmp_name'] and $_FILES['xx']['type'] empty and $_FILES['xxx']['size'] is "0" $_FILES['myFormFieldName']是非空的但$_FILES['myFormFieldName']['error']3( UPLOAD_ERR_PARTIAL ),带有$_FILES['xxx']['tmp_name']$_FILES['xx']['type']空且$_FILES['xxx']['size']为“0”

These errors appears not to be deterministic... 这些错误似乎不是确定性的......

  • When I test myself (using the same image than a user having problems with uploading it) I have no problems 当我测试自己(使用相同的图像而不是上传它的用户)我没有问题
  • I see in my logs that some users semms to always have problems with upload, but other don't. 我在日志中看到一些用户semms总是遇到上传问题,但其他用户没有。
  • I also see that some users having problems managed to upload their file with siccess after several retry 我还看到一些有问题的用户在几次重试后设法用siccess上传文件
  • I had contact with one user having problems saying that he has the Safari cannot open the page because network connection was lost error page from Safari when trying to upload its picture (whenever he is in WiFi or mobile 3G/4G data) 我曾与一个用户有过联系,但是有一个问题,说他有Safari cannot open the page because network connection was lost当尝试上传图片时(无论何时他是WiFi或移动3G / 4G数据),Safari都会Safari cannot open the page because network connection was lost网页Safari cannot open the page because network connection was lost错误页面

Code

HTML of form to upload the file (limit the user uploads to 35M from client-side) 用于上传文件的表单的HTML (限制用户从客户端上传到35M)

    <ul class="pageitem">
        <form name="empty-custom-native" 
              enctype="multipart/form-data" 
              action="/upload.php?method=nativeUpload" 
              method="post">
            <input type="hidden" name="MAX_FILE_SIZE" value="36700160" />
            <li class="button" >
                <input type="file" name="iosImageFILE" id="iosImageFILE"/>
            </li>
        </form>
        <li class="button directUrlButton" onclick="javascript:onclickNativeUploadForm();">
            <input type="button" value="Upload" />
        </li>
    </ul>

Javascript called by HTML (used to check that the file input is not empty before submit the form) HTML调用的Javascript (用于在提交表单之前检查文件输入是否为空)

  function onclickNativeUploadForm(){
    if( document.getElementById('iosImageFILE').value=="" ){
      alert("Please choose an image FIRST !");
    } else{
      showPleaseWait();
      setTimeout(submitNativeUploadForm, 2000);
    }
  }

  function submitNativeUploadForm(){
      document.forms['empty-custom-native'].submit();
  }

PHP code managing the file upload (extract) 管理文件上传的PHP代码(提取)

<?php
if( isset($_FILES['iosImageFILE']) && !empty($_FILES['iosImageFILE']) ){
    $uploadStatus = $_FILES['iosImageFILE']['error'];
    if( $uploadStatus==0 ){
        // Copy source file to temp file
        if( !move_uploaded_file($_FILES['iosImageFILE']['tmp_name'], $_SERVER['DOCUMENT_ROOT']."/".$userImageTemp) ) {
            throw new Exception("fileUploadCopy");
        }

    } else{
        if( $uploadStatus==1 ){ throw new Exception("fileUploadIniSize"); }
        else if( $uploadStatus==2 ){ throw new Exception("fileUploadFormSize"); }
        else if( $uploadStatus==3 ){ throw new Exception("fileUploadPartial"); } // THIS ERROR ...
        else if( $uploadStatus==4 ){ throw new Exception("fileUploadNoFile"); }
        else if( $uploadStatus==6 ){ throw new Exception("fileUploadNoTmpDir"); }
        else if( $uploadStatus==7 ){ throw new Exception("fileUploadCantWrite"); }
        else if( $uploadStatus==8 ){ throw new Exception("fileUploadExtension"); }
        else{
            throw new Exception("fileUploadSystem");
        }
    }
} else{
    throw new Exception("fileUploadUpload"); // ... OR THIS ERROR
}

// If anything was OK, we continue here with the uploaded file copied in our working dir to do some stuff with it...
?>

So in my case I have fileUploadUpload or fileUploadPartial exceptions. 所以在我的情况下,我有fileUploadUploadfileUploadPartial异常。


Investigations 调查

Server-side (php.ini config) 服务器端(php.ini配置)

I've checked the PHP environment variables dealing with file upload (with ini_get command) to check that I didn't exceed the max file size or max uploads number. 我检查了处理文件上传的PHP环境变量(使用ini_get命令)来检查我没有超过最大文件大小或最大上传数量。

Everything seems fine, I have quite generous limits, post_max_size is gretaer than upload_max_filesize . 似乎一切都很好,我有相当慷慨的限制, post_max_size高于gretaer upload_max_filesize upload_tmp_dir is empty by I guess this means that I use the default system /tmp dir? upload_tmp_dir是空的我想这意味着我使用默认的system /tmp目录?

Tell me if you see problems or other things to check. 如果您发现问题或需要检查的其他事项,请告诉我。

Note: I don't have access to php.ini , but these values are fixed by my hosting provider (I am on a shared hosting). 注意:我无法访问php.ini ,但这些值是由我的托管服务提供商修复的(我在共享主机上)。

file_uploads : '1'
upload_tmp_dir : ''
upload_max_filesize : '128M'
max_file_uploads : '20'
post_max_size : '130M'
max_execution_time : '300'

Client-side (HTML/JS code) 客户端(HTML / JS代码)

MAX_FILE_SIZE is less than the max file size allowed by my PHP server. MAX_FILE_SIZE小于我的PHP服务器允许的最大文件大小。

I used enctype="multipart/form-data" and method="post" as expected. 我按预期使用了enctype="multipart/form-data"method="post"

I don't do AJAX file upload, just using Javascript to check input file emptiness before submit the form with JS. 我没有做AJAX文件上传,只是使用Javascript检查输入文件空虚,然后用JS提交表单。

Server-side (PHP code) 服务器端(PHP代码)

When I debug the content of $_FILES['iosImageFILE'] I get this for the fileUploadPartial error (and this is null for the fileUploadUpload error). 当我调试的内容$_FILES['iosImageFILE']我得到这个为fileUploadPartial错误(这是空的fileUploadUpload错误)。

iosImageFILE = {"name":"DD292A1C-9CBF-4843-9E1C-7C815593C67A.png","type":"","tmp_name":"","error":3,"size":0}

Server-side (filesystem) 服务器端(文件系统)

I tried to check the /tmp directory where the uploaded files are supposed to be, but I don't have access to it. 我试图检查上传文件所在的/tmp目录,但我无法访问它。 I would like to check that it is not "full of old tmp files" but I guess not. 我想检查它是不是“充满了旧的tmp文件”,但我猜不是。

Indeed I made a PHP script to scan its content using var_export(scandir('/tmp')) and I only see some sess_xxxxx session files (about 7000 files). 实际上,我使用var_export(scandir('/tmp'))制作了一个PHP脚本来扫描其内容,我只看到一些sess_xxxxx会话文件(大约7000个文件)。

Code upgrade 代码升级

I've upgraded and enhanced my website to be in https (was non secured http before) and upgrade PHP version from 5.6 to 7.0). 我已将我的网站升级并增强为https(之前是非安全的http),并将PHP版本从5.6升级到7.0)。 Without any change on file upload problems. 没有任何文件上传问题的变化。

Firewall ?!? 防火墙?!?

I perhaps suspect a firewall problem with my hosting provider (I've read that it could block some things about file upload...). 我可能怀疑我的托管服务提供商存在防火墙问题(我读过它可能会阻止一些关于文件上传的事情......)。 But they said me that there is no firewall activated on my host... 但他们告诉我,我的主机上没有激活防火墙......


Conclusion 结论

I really don't know what to check next to investigate and find this problem. 我真的不知道接下来要检查什么调查并找到这个问题。

Please note that almost everybody uploading files comes from iOS / Safari browser. 请注意,几乎所有上传文件的人都来自iOS / Safari浏览器。 I don't know if there would be the same problems from other OS or browsers... 我不知道其他操作系统或浏览器是否会出现同样的问题......

I'm a front-end web developer and I must admint I'm not comfortable with this kind of problem. 我是一名前端Web开发人员,我必须管理我对此类问题并不满意。

Any help appreciated! 任何帮助赞赏!

End of the story : as suspected, this was a problem with my hosting provider (they took 1 month to solve it...) 故事的结尾:怀疑,这是我的托管服务提供商的问题(他们用了1个月的时间来解决它......)

I didn't managed to get more details, except that it was an "Apache configuration problem", and that it has a link with load balacing... 我没有设法获得更多细节,除了它是一个“Apache配置问题”,并且它有一个与load balacing的链接...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM