【发布时间】:2011-02-26 05:34:18
【问题描述】:
上传的文件在上传后是否可以保留?
我正在构建一个电子邮件编辑器应用程序。用户可以将文件作为附件上传。有时在 10 个上传的文件中,有一个没有正确上传,或者提交的表单无效。然后将再次显示撰写页面。但是,用户这次需要再次上传文件。是否有可能在第一次上传时以某种方式维护上传的文件,如果表单无效,则显示上传文件的链接等?
【问题讨论】:
标签: php forms file-upload postback
上传的文件在上传后是否可以保留?
我正在构建一个电子邮件编辑器应用程序。用户可以将文件作为附件上传。有时在 10 个上传的文件中,有一个没有正确上传,或者提交的表单无效。然后将再次显示撰写页面。但是,用户这次需要再次上传文件。是否有可能在第一次上传时以某种方式维护上传的文件,如果表单无效,则显示上传文件的链接等?
【问题讨论】:
标签: php forms file-upload postback
是的,只需将文件复制到一个临时文件夹,并将已成功上传的文件显示为列表。然后将文件名存储在隐藏的输入字段中(例如),当用户再次提交表单时,您将能够上传新文件并检索之前上传的文件。
一些伪代码:
/*
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
*/
【讨论】:
for example 部分,这意味着隐藏字段不是解决此问题的唯一(或最佳)方法,只是显示最快的方法?该示例显示了完成这样一个系统应该采取的基本步骤,它并不意味着是一个完整的实现。