apache PHP文件上传问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/585431/
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
PHP file upload problem
提问by Siu Ching Pong -Asuka Kenji-
I've got a really annoying problem with file uploads.
我在文件上传方面遇到了一个非常烦人的问题。
Users can choose a file in an html file field. When they submit the form, this file will be uploaded.
用户可以在 html 文件字段中选择一个文件。当他们提交表单时,该文件将被上传。
On the serverside I just use standard PHP code (move_uploaded_file). I do nothing weird.
在服务器端,我只使用标准的 PHP 代码(move_uploaded_file)。我什么都不奇怪。
Everything works perfectly.
一切正常。
I can see the file on the server, I can download it again, ...
我可以在服务器上看到文件,我可以再次下载它,...
However sometimes this doesn't work. I upload the file, process it and I get no errors.
但是有时这不起作用。我上传文件,处理它,我没有得到任何错误。
But the file just doesn't exist on the server.
但是该文件不存在于服务器上。
Each time I upload that specific file I get no errors but it never gets saved.
每次我上传该特定文件时,我都不会出错,但它永远不会被保存。
Only if I rename it (test.file to tst.file for example) I can upload it and it'll actually get saved.
只有我重命名它(例如 test.file 到 tst.file),我才能上传它并且它实际上会被保存。
I get this problem very rarely. And renaming always works. But I can't ask users to rename their files obviously...
我很少遇到这个问题。重命名总是有效的。但我不能要求用户明显地重命名他们的文件......
I have no access to the apache tmp file directory, no access to logs or settings so this makes debugging even harder. I only have this problem on this particular server (which I don't manage; I don't even have access to it) and I use the exact same code on lots of servers that don't have this problem.
我无法访问 apache tmp 文件目录,也无法访问日志或设置,因此这使得调试更加困难。我只在这个特定的服务器上有这个问题(我没有管理;我什至无法访问它)并且我在很多没有这个问题的服务器上使用完全相同的代码。
I would be grateful if someone could help me out here or point me in the right direction.
如果有人能在这里帮助我或为我指明正确的方向,我将不胜感激。
回答by James Hall
Trying adding this debug code:
尝试添加此调试代码:
echo '<pre>';
print_r($_FILES);
echo '</pre>';
You should see an error number. You can lookup what it means at http://uk3.php.net/manual/en/features.file-upload.errors.php
您应该会看到一个错误编号。您可以在http://uk3.php.net/manual/en/features.file-upload.errors.php查找它的含义
Might also be worth checking to make sure the destination file doesn't already exist.
也可能值得检查以确保目标文件不存在。
回答by enobrev
My first thought was filesize issues. In the php.ini, if the post_max_size or upload_max_filesize are too small, you can end up with similar results - where the file just seems to disappear. You would get an error in the apache logs (which you mention you've no access to).
我的第一个想法是文件大小问题。在 php.ini 中,如果 post_max_size 或 upload_max_filesize 太小,您最终会得到类似的结果 - 文件似乎消失了。你会在 apache 日志中得到一个错误(你提到你无权访问)。
In those cases, the $_FILES array would simply be empty - as if the file never arrived. Since your responses to Gumboand James Hallshow that php is reporting a proper upload, I'm led to wonder about the processing you mention.
在这些情况下, $_FILES 数组将只是空的 - 就好像文件从未到达一样。由于您对Gumbo和James Hall的回复表明 php 正在报告正确的上传,因此我对您提到的处理感到疑惑。
If, during the process, your memory gets maxed or the script runs too long, the script may be dying out before it gets a chance to move it. You'll want to check these:
如果在此过程中,您的内存达到最大值或脚本运行时间过长,则脚本可能会在有机会移动它之前就消失了。您需要检查这些:
memory_limit
内存限制
max_execution_time
最大执行时间
max_input_time
最大输入时间
Otherwise, without the apache logs, I'd say it might be a good idea to start outputting to a log file of your own throughout your file processing script. Try a file_exists on the tmp file, see what info you can get from the file (permissions, etc).
否则,如果没有 apache 日志,我会说在整个文件处理脚本中开始输出到您自己的日志文件可能是个好主意。在 tmp 文件上尝试 file_exists,看看你可以从文件中获得什么信息(权限等)。
Unfortunately PHP doesn't get involved until the upload is finished, which means you won't get much info during - only after the fact. You best option might be to talk to the hosting company and get access to the logs - even if for a short time. In my experience, I've rarely had trouble getting ot the logs - or at least getting a tech to check the logs for me while I run tests (in the case where a shared server doesn't split their logs - seems ridiculous, but I've seen it before).
不幸的是,在上传完成之前 PHP 不会参与,这意味着您不会在上传期间获得太多信息 - 只有在事后。您最好的选择可能是与托管公司交谈并访问日志 - 即使是很短的时间。根据我的经验,我很少在获取日志时遇到问题 - 或者至少在我运行测试时获得一项技术来为我检查日志(在共享服务器不拆分他们的日志的情况下 - 看起来很荒谬,但是我以前看过)。
Edit: I realize you can't change those php settings, but you might want to see what they are in order to find out if they're potential problems for your script. For instance, a low memory limit will kill your processor script if it's less than the size of the uploaded file.
编辑:我意识到您无法更改这些 php 设置,但您可能想查看它们是什么,以便找出它们是否对您的脚本造成潜在问题。例如,如果内存限制小于上传文件的大小,那么低内存限制将杀死您的处理器脚本。
回答by Siu Ching Pong -Asuka Kenji-
This is what you said...
这是你说的...
"I have no access to the apache tmp file directory, no access to logs or settings so this makes debugging even harder. I only have this problem on this particular server (which I don't manage; I don't even have access to it) and I use the exact same code on lots of servers that don't have this problem."
“我无法访问 apache tmp 文件目录,也无法访问日志或设置,因此这使得调试更加困难。我只有在这个特定的服务器上有这个问题(我没有管理;我什至无法访问它)并且我在许多没有这个问题的服务器上使用完全相同的代码。”
According to what you said above, I assume that you are using a server that is shared among many users. If the Apache of this server is configured with something like "mod_suphp", then your PHP scripts will be executed using the privileges of your UNIX user account ("jef1234", for example), which means the files you create will have you ("jef1234") as the owner (instead of "apache" or "www-data").
根据您上面所说的,我假设您使用的是在许多用户之间共享的服务器。如果此服务器的 Apache 配置了类似“mod_suphp”的内容,那么您的 PHP 脚本将使用您的 UNIX 用户帐户(例如“jef1234”)的权限执行,这意味着您创建的文件将拥有您(“ jef1234”)作为所有者(而不是“apache”或“www-data”)。
The system's temporary directory (usually "/tmp") is usually configured with the "sticky bit" on. This means everyone can create files in this directory, but the created files are only accessible by the owner (you may treat this as the one who created it).
系统的临时目录(通常是“/tmp”)通常配置为开启“粘滞位”。这意味着每个人都可以在此目录中创建文件,但创建的文件只能由所有者访问(您可以将其视为创建者)。
As a result, if the server configuration is not careful enough, you may have file naming collisions with other users' files. For example, when you upload "test.file", if another user has already uploaded another file with the same name, the system refuses to overwrite the file created by him, as thus you have to use another name.
因此,如果服务器配置不够仔细,您可能会与其他用户的文件发生文件命名冲突。例如,当您上传“test.file”时,如果其他用户已经上传了另一个同名文件,系统拒绝覆盖他创建的文件,因此您必须使用其他名称。
Usually the problem does not exist because PHP is smart enough to generate temporary names for the uploaded file (ie. $_FILES["html_form_input_name"]["tmp_name"]). If somehow you can confirm that this is really the reason, the server is obviously mis-configured. Tell your system administrator the problem as ask him to solve it. If this could not be solved, you may do some JavaScript tricks on the name of the file before it is uploaded (not tested, just an idea)...
通常这个问题不存在,因为 PHP 足够聪明,可以为上传的文件生成临时名称(即 $_FILES["html_form_input_name"]["tmp_name"])。如果您能以某种方式确认这确实是原因,则服务器显然配置错误。把问题告诉你的系统管理员,让他解决。如果这无法解决,您可以在上传文件之前对文件名进行一些 JavaScript 技巧(未测试,只是一个想法)...
★When the user submits the form, rename the file from, for example, "test.file" to "jef1234-test.file-jef1234". After the file is uploaded, move the file (ie move_uploaded_file()) to another place and rename it to the original filename by removing the added strings.
★当用户提交表单时,将文件重命名,例如从“test.file”改为“jef1234-test.file-jef1234”。文件上传后,将文件(即move_uploaded_file())移动到另一个地方,并通过删除添加的字符串将其重命名为原始文件名。
Hope this helps...
希望这可以帮助...
Asuka Kenji
明日香贤治
回答by Gumbo
If an upload failes you don't get the same kind of error like a PHP syntax error or such.
如果上传失败,您不会收到类似 PHP 语法错误等类型的错误。
But you can check the file upload statusand report the error to the user yourself.
但是你可以自己检查文件上传状态,向用户报告错误。

