【发布时间】:2018-01-07 10:27:16
【问题描述】:
我正在尝试在会话中一次上传多张图片。在我的情况下,用户必须上传他之前为一个“位置”指定的确切数量的图像,并使用不同的图像再次以正确的数量重复另一个“位置”的操作。不幸的是上传不起作用,我不知道为什么。我应该使用$_POST 而不是$_FILES,还是foreach 方法不正确?代码如下:
HTML:
<?php
session_start();
include('php/uploadspec.php');
if(!isset($_SESSION['face'][$_SESSION['counter']])){
header("Location: cuberender.php");
}
$speccount = 1;
?>
<div class="counter">
<form method="post">
<div class="form-group">
<p>Please Choose all <?php echo $_SESSION['quantity'] ?> photos for <?php echo $_SESSION['face'][$_SESSION['counter']] ?></p>
</div>
<div class="form-group">
<input type="file" name="images[]" accept="image/*" multiple />
</div>
<div class="form-group">
<input type="submit" name="Submit" value="Submit!" />
</div>
</form>
<?php
if (isset($_POST['Submit'])) {
$imgamount=count($_POST['images']);
if ($imgamount==$_SESSION['quantity']){
uploadspec(session_id(), $speccount);
$_SESSION['counter']++;
header("Location: posteditor.php");
}
}
?>
PHP:
function uploadspec($id, $speccount){
foreach($_FILES["images"]["tmp_name"] as $image) {
$target_file = $id . '/' .$_SESSION['face'][$_SESSION['counter']]. '/specimage'.$speccount;
$speccount++;
move_uploaded_file($image, $target_file);
}
}
提前致谢!
【问题讨论】:
-
“我应该使用 $_POST 而不是 $_FILES,还是 foreach 方法不正确?” -
$_FILES,手册中有一个示例你可以基于php.net/manual/en/features.file-upload.post-method.php -
如果你使用的任何东西都失败了,使用错误报告php.net/manual/en/function.error-reporting.php
-
if(!isset($_SESSION['face'][$_SESSION['counter']]))顺便说一句,这可能会让你失望。 -
好吧,您在下面有几个“请检查”答案,所以请“检查”那些。
标签: php forms file-upload upload image-uploading