导航离开php中的页面后上传文件会发生什么?

导航离开php中的页面后上传文件会发生什么?

问题描述:

I have a form page that posts to another page where multiple fields as well as file uploads are processed. Just wondering what happens to the 'tmp_name' files when/if the user enters some incorrect info and I send them back to the form page with a meta refesh?

If successful, I move the file to a new location. But if not successful, do the files get unset or erased if the user gets redirected? If they don't, can I reaccess them again so the user doesnt have to reupload? OTOH if there is a problem with the file, say it is not the expected MIME type, should I unlink($_FILES['userFile']['tmp_name']? Its easy to force the user to re-upload again, i think, but I dont want the server being filled up with files that will never be used? If the form passes inspection, and I use rename() to move the file, is the temp file really gone? Did it ever exist on the server's hard drive, or was it only in RAM? Whats the best practice here?

我有一个表单页面发布到另一个页面,其中处理多个字段和文件上载。 只是想知道当'/如果用户输入一些不正确的信息并且我将它们发送回带有meta refesh的表单页面时'tmp_name'文件会发生什么? p>

如果成功,我移动 文件到新位置。 但如果不成功,如果用户被重定向,文件是否会被取消设置或删除? 如果他们不这样做,我可以再次访问它们,以便用户不必重新上载吗? OTOH如果文件有问题,说它不是预期的MIME类型,我应该取消链接($ _ FILES ['userFile'] ['tmp_name']?我认为很容易强迫用户重新上传 ,但我不希望服务器被填满永远不会被使用的文件?如果表单通过检查,我使用rename()移动文件,临时文件真的不见了吗?它是否曾经存在于服务器上 驱动器,还是只在RAM中?这是最好的做法吗? p> div>

do the files get unset or erased if the user gets redirected?

The uploaded files are stored in the /tmp directory (or whatever is specified as PHP's temporary location). Once your script has run, files left there are subject to deletion any time. I don't think they usually get deleted straight away, but the contents of /tmp will be automatically purged by the OS when necessary.

/tmp is usually located on a hard drive, not in RAM.

Managing this is usually nothing you need to worry about.

If the form passes inspection, and I use rename() to move the file, is the temp file really gone?

Yes, but you must use move_uploaded_file() on uploaded files instead of rename() for security reasons.

The file is stored in the tmp folder and if you don't move it elsewhere it will stay there. It will be removed automatically by the OS on next cleanup.

Edit: Please look at Marc's comment below.

http://www.php.net/manual/en/features.file-upload.post-method.php

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