是否可以在上传文件之后保留上传的文件?
我正在构建电子邮件编辑器应用程序。用户可以将文件作为附件上传。有时 10 个上传的文件中有一个未正确上传,或者提交的表单无效。然后将再次显示撰写页面。但是,用户这次需要重新上传文件。是否有可能在第一次上传时以某种方式在某处维护上传的文件,如果表单无效,则显示上传文件的链接?
最佳答案
是的,只需将文件复制到临时文件夹并显示已成功上传的文件列表。然后将文件名存储在隐藏的输入字段中(例如),当用户再次提交表单时,您将能够上传新文件并检索之前上传的文件。
一些伪代码:
/*
If $_POST request
Loop through every file uploaded ($_FILES)
Check for errors
Save the file into a temporary directory
Build an array of files successfully uploaded
Loop through previously uploaded files ($_POST['uploaded'])
Append the filenames to the array of uploaded files
Validate the rest of the form
If no errors
Move files from temporary location to the actual location
Else
Show the same form
Print the filenames inside hidden input fields
<input type="hidden" name="uploaded[]" value="filename.txt" />
Show the user the list of files and allow deletion of files
Rinse and repeat
*/
关于php - 如何在发布带有文件上传的表单后保留上传的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2947853/