input type =“file” - 使用post-redirect-get删除临时文件

时间:2023-01-26 14:57:36

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.

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

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

2 个解决方案

#1


0  

You might be able to pass an encoded copy of the file(s) into the session.

您可以将文件的编码副本传递到会话中。

Something like...

$tempImages = array();

foreach($_FILES as $file)
{
    $tempImages[] = base64_encode(file_get_contents($file['tmp_name']));
}

$_SESSION['post-files'] = serialize($tempImages);

#2


0  

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...

最后,简化的解决方案是处理redirect.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);
  }
}

#1


0  

You might be able to pass an encoded copy of the file(s) into the session.

您可以将文件的编码副本传递到会话中。

Something like...

$tempImages = array();

foreach($_FILES as $file)
{
    $tempImages[] = base64_encode(file_get_contents($file['tmp_name']));
}

$_SESSION['post-files'] = serialize($tempImages);

#2


0  

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...

最后,简化的解决方案是处理redirect.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);
  }
}