从PHP上传文件时遇到问题。奇怪的是,有些文件有效,而另一些则无效。我可以用以下脚本复制它:
<!DOCTYPE html>
<html>
<body>
<form action="test.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="submit">
</form>
</body>
</html>
<?php
var_dump($_FILES);
失败时,输出为:
array(1) {
["fileToUpload"]=>
array(5) {
["name"]=>
string(13) "image_2_3.jpg"
["type"]=>
string(0) ""
["tmp_name"]=>
string(0) ""
["error"]=>
int(3)
["size"]=>
int(0)
}
}
据我所知,错误代码3表明这是部分上传,但我不确定为什么它只发生在某些文件中。我在文件中看不到任何不起作用的模式。例如,其中一个不起作用的文件大约是70kb,而另一个起作用的是180kb,所以这似乎与文件大小无关。
我在运行Windows10的docker容器上运行这个。docker镜像是基于PHP的官方镜像。我已经测试了标签5.5-apache和5.6-apache。代码本身是在Windows中的一个挂载卷上运行的。
加载的Apache模块有:
core mod_so mod_watchdog http_core mod_log_config mod_logio mod_version
mod_unixd mod_access_compat mod_alias mod_auth_basic mod_authn_core
mod_authn_file mod_authz_core mod_authz_host mod_authz_user mod_autoindex
mod_deflate mod_dir mod_env mod_filter mod_headers mod_mime prefork
mod_negotiation mod_php5 mod_rewrite mod_setenvif mod_status
任何想法都将不胜感激。
编辑:做了一个单独的测试,运行了一个普通的php:5.5-apache映像,只提到了php代码,我仍然能够重现这个问题。
在我的上一次测试中,此文件产生了以下问题:http://s000.tinyupload.com/?file_id=14588523736227077930
尽管这一次有效:http://s000.tinyupload.com/?file_id=03989814485857897493
经过进一步挖掘,我发现这是Docker论坛上最近提到的一个悬而未决的问题:https://forums.docker.com/t/php-file-upload-fails-with-upload-err-partial-on-docker-for-windows/18252
当前的解决方法(我已经测试过)是在通过浏览器访问应用程序时使用http://docker
或docker.local
而不是http://localhost
。