[英]input type=“file” - temp files deleted with post-redirect-get
我有一个允许用户上传文件的表格。 因为用户也输入了其他信息,所以我只是将后处理更改为后重定向获取。 我注意到全局$ _FILE对redirect.php是可见的,但是在重定向回输入表单后丢失了。 我试图保存$ _FILE数组,但似乎临时文件已通过post-redirect-get删除。 有什么方法可以让服务器在离开redirect.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");
您也许可以将文件的编码副本传递到会话中。
就像是...
$tempImages = array();
foreach($_FILES as $file)
{
$tempImages[] = base64_encode(file_get_contents($file['tmp_name']));
}
$_SESSION['post-files'] = serialize($tempImages);
最后,简化列表解决方案是在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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.