php 文件上传不在 /tmp 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5516002/
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 21:40:54  来源:igfitidea点击:

File Upload not in /tmp

php

提问by Frank Conry

This sounds like a stupid question but, after uploading a file the file isnt in the location php said it would be.

这听起来像是一个愚蠢的问题,但是在上传文件后,该文件不在 php 所说的位置。

First the simple test page:

首先是简单的测试页面:

<html><body> 
<h1><?=$_FILES['imgup']['tmp_name'];?></h1>
<?
        print_r($_FILES);
?>
<form enctype="multipart/form-data" method="post" action="upload.php">
<input type="file" name="imgup" id="imgup">
<input type="submit">
</form>
</body></html>

Now, the print_r in plain text:

现在,纯文本的print_r:

Array ( [imgup] => Array ( [name] => ace.jpg [type] => image/jpeg [tmp_name] => /tmp/phpEdfBjs [error] => 0 [size] => 29737 ) )

So no error, standard looking path, but /tmp has no such file. Further, running a find on my whole system turns up bubkis.

所以没有错误,标准的外观路径,但 /tmp 没有这样的文件。此外,在我的整个系统上运行 find 会出现 bubkis。

FYI: php.ini has

仅供参考:php.ini 有

max_execution_time = 120
file_uploads = On
upload_max_filesize = 2M

and the file I've been uploading is 29k

我上传的文件是 29k

Any thoughts?

有什么想法吗?

回答by Pascal MARTIN

The temporary file is deleted when the PHP script that received it has finished running : it is just a temporary file.

当接收它的 PHP 脚本完成运行时,临时文件被删除:它只是一个临时文件。

The PHP script to which you are posting the form -- upload.php -- should move the temporary file to a non-temporary location, using move_uploaded_file()

您将表单发布到的 PHP 脚本——upload.php——应该将临时文件移动到一个非临时位置,使用 move_uploaded_file()


Basically, the idea is :


基本上,这个想法是:

  • The user uploads a file to your script,
    • That file is stored in a temporary lcoation.
  • Your script works on that file :
    • Checks if the file is OK (content-type, size, ...)
  • And if the file is OK, your script moves it to the permanent storage directory (whereever you want)
  • 用户将文件上传到您的脚本,
    • 该文件存储在临时文件中。
  • 您的脚本适用于该文件:
    • 检查文件是否正常(内容类型、大小...)
  • 如果文件没问题,您的脚本会将其移动到永久存储目录(您想要的任何位置)

If the upload doesn't finish successfully, or if you don't move the file somewhere else, the temporary file is automatically deleted.

如果上传没有成功完成,或者如果您没有将文件移动到其他地方,临时文件将被自动删除。


As a reference, you should read the following section of the manual : Handling file uploads - POST method uploads


作为参考,您应该阅读手册的以下部分:处理文件上传 - POST 方法上传

Quoting the part of it which is related to your problem :

引用与您的问题相关的部分:

The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed.

如果该文件尚未移走或重命名,则该文件将在请求结束时从临时目录中删除。