输入类型=“文件” - 使用重定向后获取删除的临时文件


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

我有一个表单,我允许用户上传文件。我刚刚将后处理更改为后重定向获取,因为用户也输入了其他信息。我注意到全局 $_FILE 对重定向可见.php但在重定向回输入表单后丢失。我试图保存 $_FILE 数组,但似乎临时文件已通过重定向后 get 删除。有没有办法告诉服务器在离开重定向时保留临时文件.php以便我可以在我认为合适的时候处理它们?提前谢谢。

用户表格:

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

重定向文件:

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

最后,简单的解决方案是在重定向中处理临时文件.php并将文件存储在我自己的临时位置。然后,我可以在处理表单中处理它们。对于任何关注的人,这就是我所做的......

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);
  }
}

您也许能够将文件的编码副本传递到会话中。

像...

$tempImages = array();
foreach($_FILES as $file)
{
    $tempImages[] = base64_encode(file_get_contents($file['tmp_name']));
}
$_SESSION['post-files'] = serialize($tempImages);