Search code examples
phpfile-uploadpost-redirect-get

input type="file" - temp files deleted with post-redirect-get


I have a form where I allow the user to upload files. I just changed the post processing to a post-redirect-get since the user enters other information as well. I noticed that the global $_FILE is visible to the redirect.php but is lost after redirecting back to the input form. I attempted to save the $_FILE array, but it appears that the temp files are removed with the post-redirect-get. Is there any way to tell the server to preserve the temp files when leaving the redirect.php so I can process them when I see fit? Thanks in advance.

User Form:

<input type="file" name="file[]" id="userfiles" size='1px' multiple onChange="makeFileList();" />

Redirect File:

if (isset($_FILES)){
    $_SESSION['post-files'] = $_FILES;
}
header("Location: /back/to/input/form.php");

Solution

  • In the end, the simplist solution was to process the temp files in the redirect.php and store the files in my own temp location. I then can deal with them once back in my processing form. For anyone who follows, this is what I did...

    if (isset($_FILES)){
      $_SESSION['post-files'] = $_FILES;
      $i=0;
      foreach ($_SESSION['post-files']['file']['name'] as $filename){
        // get the file to upload
        $fromfile=$_SESSION['post-files']['file']['tmp_name'][$i];
    
        // get just the filename
        $filename = pathinfo($fromfile, PATHINFO_FILENAME) . '.' . pathinfo ($fromfile, PATHINFO_EXTENSION);
    
        // give it a new path
        $tofile = "/some/temp/path/". $filename;
    
        // store the new temp location
        $_SESSION['post-files']['file']['tmp_name'][$i] = $tofile;
    
        // move the files to a temp location
        if (!is_dir(pathinfo($tofile,PATHINFO_DIRNAME))) {
            mkdir(pathinfo($tofile,PATHINFO_DIRNAME), 0777, true);
        }
        move_uploaded_file($fromfile,$tofile);
      }
    }