How to save downloaded files after a POST form with file upload?

Hi guys, it is possible to be able to save the downloaded file AFTER it was downloaded, I mean that I am creating a composite email application. Users can upload files as attachments. At times out of 10 downloaded files, one of them is not loaded correctly or the submitted form is invalid, the layout page will be displayed again, but this time the user uploaded the file again. Is it possible to somehow support the downloaded files somewhere in the first download, and if the form is invalid - can you display links to the downloaded file or so?

+3
source share
1 answer

Yes, just copy the files to a temporary folder and show the files as a list that was successfully downloaded. Then save the file names inside hidden input fields (for example), and when the user submits the form again, you can upload new files, as well as retrieve files that were previously downloaded.

Some pseudo noise:

/*
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
*/
+2
source

Source: https://habr.com/ru/post/1747939/


All Articles