PRG 多个图像上传 => 修复临时文件夹中缺少的图像时出现问题

  • 本文关键字:图像 文件夹 问题 PRG php
  • 更新时间 :
  • 英文 :


下面的代码在没有 PRG 的情况下工作正常,但我想要 PRG,因为我真的很讨厌刷新-重新发布。虽然,我没有预见到这个问题:我假设如果我不立即上传我的图像,get 找不到任何文件从临时文件夹移动到服务器文件夹(我猜它们在发布后立即被删除?

现在,由于图像无论如何都应该在过滤之前上传(使用服务器端语言(,我有一个想法,即不使用 PRG 进行上传,然后使用像临时文件这样的会话状态,该文件将包含所有提交信息,以便销毁它并获得免费刷新重新发布......好吧,它不起作用。我知道它只上传正确的文件,但我无法从中获取任何$vars,导致无法向用户显示我的$error变量/警告。

那么......我如何用一次射击的PHP大师获得两只鸟?


试图弄清楚"假"会话是否可以工作:

<?php
session_start();
include_once('../includes/connection.php');
if (isset($_SESSION['logged_in'])) {
if (isset($_POST['submit_pic'])) {
$countfiles = count($_FILES['image']['name']);
$target_dir = "../images/";
$error_array = array();
$upload_array = array();
$eupload_array = array();
for($i=0;$i<$countfiles;$i++)   {
$filename = $_FILES['image']['name'][$i];
$target_file = $target_dir.$filename;
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$check = getimagesize($_FILES["image"]["tmp_name"][$i]);
if($check !== false) {
$uploadOk = 1;
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
array_unshift($error_array, "Image $filename is not JPG, JPEG, PNG or GIF.");
$uploadOk = 0;
}
} else {
array_unshift($error_array, "File $filename is not an image.");
$uploadOk = 0;
}
if (file_exists($target_file)) {
array_unshift($error_array, "Image $filename already exists.");
$uploadOk = 0;
}
if ($_FILES["image"]["size"][$i] > 500000) {
$u = 1;
array_unshift($error_array, "Image $filename is too large.");
$uploadOk = 0;
}
if ($uploadOk == 0) {
array_unshift($eupload_array, "Sorry, image $filename was not uploaded.");
} else {
if (move_uploaded_file($_FILES['image']['tmp_name'][$i], $target_file)) {
array_unshift($upload_array, "Image $filename has been uploaded.");
} else {
array_unshift($eupload_array, "Sorry, $filename there was an error uploading your file.");
}
}
} 
}
if(strcasecmp($_SERVER['REQUEST_METHOD'],"POST") === 0) {
$_SESSION['postdata'] = $_FILES;
header("Location: ".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']);
exit;
}
if( isset($_SESSION['postdata'])) {
$_FILES = $_SESSION['postdata'];
unset($_SESSION['postdata']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel='stylesheet' href='../assets/style.css'/>
<title>CMS TEST</title>
</head>
<body>
<div class='container'>
<a href='add_picture.php' id='logo'><h2>Add Picture</h2></a>
<br/>
<small class='test_warning' style='color:#B83333'>
&#10102; Images should be JPG, JPEG, PNG or GIF format extentions. <br/>
&#10103; Images should not be above 500KB size.<br/>
&#10104; Please try to rename images before uploading for easier use.
</small> <br/><br/>
<form action='add_picture.php' method="post" enctype="multipart/form-data">
<input type="file" name="image[]" id="image" class="custom-file-input" multiple required/><br/><br/><br/>
<input type="submit" value="Upload" name="submit_pic"/>
</form>
<?php if (!empty($upload_array)) { ?>
<small style='color:#00FF00'>
<?php echo '<br/><br/>', implode("<br/>",$upload_array), '<br/>'; ?>
</small>
<?php }?>
<?php if (!empty($eupload_array)) { ?>
<small style='color:#aa0000'>
<?php echo '<br/><br/>', implode("<br/>",$eupload_array), '<br/>'; ?>
</small>
<?php }?>
<?php if (!empty($error_array)) { ?>
<small style='color:#aa0000'>
<?php echo implode("<br/>",$error_array), '<br/>'; ?>
</small>
<?php }?>
<br/><br/>
<a href='index.php'>&larr; Back</a>
<br/>
</div>
<a class='logout' href='logout.php'>Logout</a>
</body>
</html>
<?php
} else {
header('Location:index.php');
}
?>

"适当的一个":

<?php
session_start();
include_once('../includes/connection.php');
if (isset($_SESSION['logged_in'])) {
if( strcasecmp($_SERVER['REQUEST_METHOD'],"POST") === 0) {
$_SESSION['postdata'] = $_FILES;
header("Location: ".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']);
exit;
}
if( isset($_SESSION['postdata'])) {
$_FILES = $_SESSION['postdata'];
$countfiles = count($_FILES['image']['name']);
$target_dir = "../images/";
$error_array = array();
$upload_array = array();
$eupload_array = array();
for($i=0;$i<$countfiles;$i++)   {
$filename = $_FILES['image']['name'][$i];
$target_file = $target_dir.$filename;
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$check = getimagesize($_FILES["image"]["tmp_name"][$i]);
if($check !== false) {
$uploadOk = 1;
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
array_unshift($error_array, "Image $filename is not JPG, JPEG, PNG or GIF.");
$uploadOk = 0;
}
} else {
array_unshift($error_array, "File $filename is not an image.");
$uploadOk = 0;
}
if (file_exists($target_file)) {
array_unshift($error_array, "Image $filename already exists.");
$uploadOk = 0;
}
if ($_FILES["image"]["size"][$i] > 500000) {
$u = 1;
array_unshift($error_array, "Image $filename is too large.");
$uploadOk = 0;
}
if ($uploadOk == 0) {
array_unshift($eupload_array, "Sorry, image $filename was not uploaded.");
} else {
if (move_uploaded_file($_FILES['image']['tmp_name'][$i], $target_file)) {
array_unshift($upload_array, "Image $filename has been uploaded.");
} else {
array_unshift($eupload_array, "Sorry, $filename there was an error uploading your file.");
}
}
} 
unset($_SESSION['postdata']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel='stylesheet' href='../assets/style.css'/>
<title>CMS TEST</title>
</head>
<body>
<div class='container'>
<a href='add_picture.php' id='logo'><h2>Add Picture</h2></a>
<br/>
<small class='test_warning' style='color:#B83333'>
&#10102; Images should be JPG, JPEG, PNG or GIF format extentions. <br/>
&#10103; Images should not be above 500KB size.<br/>
&#10104; Please try to rename images before uploading for easier use.
</small> <br/><br/>
<form action='add_picture.php' method="post" enctype="multipart/form-data">
<input type="file" name="image[]" id="image" class="custom-file-input" multiple required/><br/><br/><br/>
<input type="submit" value="Upload" name="submit_pic"/>
</form>
<?php if (!empty($upload_array)) { ?>
<small style='color:#00FF00'>
<?php echo '<br/><br/>', implode("<br/>",$upload_array), '<br/>'; ?>
</small>
<?php }?>
<?php if (!empty($eupload_array)) { ?>
<small style='color:#aa0000'>
<?php echo '<br/><br/>', implode("<br/>",$eupload_array), '<br/>'; ?>
</small>
<?php }?>
<?php if (!empty($error_array)) { ?>
<small style='color:#aa0000'>
<?php echo implode("<br/>",$error_array), '<br/>'; ?>
</small>
<?php }?>
<br/><br/>
<a href='index.php'>&larr; Back</a>
<br/>
</div>
<a class='logout' href='logout.php'>Logout</a>
</body>
</html>
<?php
} else {
header('Location:index.php');
}
?>

除了帖子获取请求之间不存在的临时图片外,变量也从刷新中删除,我无法在我的 html 中使用它们来显示错误。

所以我尝试更多地使用会话,我将图片上传代码移动到发布,创建一些会话来存储我的错误数组,然后我从我为它们创建的会话中重新创建了我的错误数组。

问题已解决,图片已过滤,错误显示,PRG 正在工作。

唉。。。我刚开始使用PHP,有很多我不明白D的东西:

<?php
session_start();
include_once('../includes/connection.php');
if (isset($_SESSION['logged_in'])) {

if( strcasecmp($_SERVER['REQUEST_METHOD'],"POST") === 0) {
$_SESSION['postdata'] = $_FILES;
$error_array = array();
$upload_array = array();
$eupload_array = array();
$countfiles = count($_FILES['image']['name']);
$target_dir = "../images/";

for($i=0;$i<$countfiles;$i++)   {
$filename = $_FILES['image']['name'][$i];
$target_file = $target_dir.$filename;
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$check = getimagesize($_FILES["image"]["tmp_name"][$i]);
echo $_FILES["image"]["tmp_name"][$i];
if($check !== false) {
$uploadOk = 1;
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
array_unshift($error_array, "Image $filename is not JPG, JPEG, PNG or GIF.");
$uploadOk = 0;
}
} else {
array_unshift($error_array, "File $filename is not an image.");
$uploadOk = 0;
}
if (file_exists($target_file)) {
array_unshift($error_array, "Image $filename already exists.");
$uploadOk = 0;
}
if ($_FILES["image"]["size"][$i] > 500000) {
$u = 1;
array_unshift($error_array, "Image $filename is too large.");
$uploadOk = 0;
}
if ($uploadOk == 0) {
array_unshift($eupload_array, "Sorry, image $filename was not uploaded.");
} else {
if (move_uploaded_file($_FILES['image']['tmp_name'][$i], $target_file)) {
array_unshift($upload_array, "Image $filename has been uploaded.");
} else {
array_unshift($eupload_array, "Sorry, $filename there was an error uploading your file.");
}
}
} 
$_SESSION['error_array'] = $error_array;
$_SESSION['upload_array'] = $upload_array;
$_SESSION['eupload_array'] = $eupload_array;

header("Location:add_picture.php");
exit;
}
if( isset($_SESSION['postdata'])) {
$error_array = $_SESSION['error_array'];
$upload_array = $_SESSION['upload_array'];
$eupload_array = $_SESSION['eupload_array'];
unset($_SESSION['postdata']);
unset($_SESSION['error_array']);
unset($_SESSION['upload_array']);
unset($_SESSION['eupload_array']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel='stylesheet' href='../assets/style.css'/>
<title>CMS TEST</title>
</head>
<body>
<div class='container'>
<a href='add_picture.php' id='logo'><h2>Add Picture</h2></a>
<br/>
<small class='test_warning' style='color:#B83333'>
&#10102; Images should be JPG, JPEG, PNG or GIF format extentions. <br/>
&#10103; Images should not be above 500KB size.<br/>
&#10104; Please try to rename images before uploading for easier use.
</small> <br/><br/>
<form action='add_picture.php' method="post" enctype="multipart/form-data">
<input type="file" name="image[]" id="image" class="custom-file-input" multiple required/><br/><br/>
<input type="submit" value="Upload" name="submit_pic"/>
</form>
<?php if (!empty($upload_array)) { ?>
<small style='color:#00FF00'>
<?php echo '<br/><br/>', implode("<br/>",$upload_array), '<br/>'; ?>
</small>
<?php }?>
<?php if (!empty($eupload_array)) { ?>
<small style='color:#aa0000'>
<?php echo '<br/><br/>', implode("<br/>",$eupload_array), '<br/>'; ?>
</small>
<?php }?>
<?php if (!empty($error_array)) { ?>
<small style='color:#aa0000'>
<?php echo implode("<br/>",$error_array), '<br/>'; ?>
</small>
<?php }?>
<br/><br/>
<a href='index.php'>&larr; Back</a>
<br/>
</div>
<a class='logout' href='logout.php'>Logout</a>
</body>
</html>
<?php
} else {
header('Location:index.php');
}
?>

最新更新