【问题标题】:How do I retain uploaded files after POsting a form with a file upload?发布带有文件上传的表单后,如何保留上传的文件?
【发布时间】:2011-02-26 05:34:18
【问题描述】:

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

我正在构建一个电子邮件编辑器应用程序。用户可以将文件作为附件上传。有时在 10 个上传的文件中,有一个没有正确上传,或者提交的表单无效。然后将再次显示撰写页面。但是,用户这次需要再次上传文件。是否有可能在第一次上传时以某种方式维护上传的文件,如果表单无效,则显示上传文件的链接等?

【问题讨论】:

    标签: php forms file-upload postback


    【解决方案1】:

    是的,只需将文件复制到一个临时文件夹,并将已成功上传的文件显示为列表。然后将文件名存储在隐藏的输入字段中(例如),当用户再次提交表单时,您将能够上传新文件并检索之前上传的文件。

    一些伪代码:

    /*
    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
    */
    

    【讨论】:

    • 您完全忘记了垃圾收集部分。并且使用隐藏字段是愚蠢的。
    • @Col。弹片,听说过一个简化的例子吗?注意到我的文本中关于隐藏字段使用的for example 部分,这意味着隐藏字段不是解决此问题的唯一(或最佳)方法,只是显示最快的方法?该示例显示了完成这样一个系统应该采取的基本步骤,它并不意味着是一个完整的实现。
    • @Your Common Sense:而不是发布您的解决方案。
    猜你喜欢
    • 2012-03-26
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 2014-01-11
    相关资源
    最近更新 更多