php - 如何在发布带有文件上传的表单后保留上传的文件?

标签 php forms file-upload postback

是否可以在上传文件之后保留上传的文件?

我正在构建电子邮件编辑器应用程序。用户可以将文件作为附件上传。有时 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/

相关文章:

php - 使用 select 语句和 php 变量插入 MySQL

php - 同时运行多个 cron 实例

php - ON DUPLICATE KEY UPDATE 上的 SQL 语法错误

javascript - Angular.js 与 Ruby On Rails Forms 的集成

flash - 上传文件时,flash是否总是发布“Filename”参数?

javascript - 上传本地文件,无需选择按钮

php - 使用 PHPUnit 对 Doctrine 对象进行单元测试

javascript - Jquery获取单字段数据

javascript - 无法将 jQuery 按钮提交更改为下拉选择并提交

php - codeigniter 上传无法在 Linux 上运行