我正在尝试创建一个网站,用户必须至少上传个人资料图片,以及另外4张可选图片。我正在努力绕过所需的逻辑,所以有人可以指点我的方向。这是表格:
<form id=userform action="updatepictures.php" method=post enctype="multipart/form-data">
<div id="slogan">Upload A Profile Picture*</div>
<center><input type=file name=profilepicture></center>
<div id="slogan">Upload Up To Four Additional Pictures</div>
<center><input type=file name=pic1></center>
<br>
<center><input type=file name=pic2></center>
<br>
<center><input type=file name=pic3></center>
<br>
<center><input type=file name=pic4></center>
<center><input style="margin-top: 20px;" type="submit" value="Update" class="butt" name="upload"></center>
</form>
以下是它的PHP脚本:
<?php
include ('connect.php');
session_start();
$target_dir = "gallery/";
$target_file = $target_dir . basename($_FILES["profilepicture"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$target_file = $target_dir . bin2hex(openssl_random_pseudo_bytes(16)) . "." . $imageFileType;
if (isset($_POST["upload"]))
{
$check = getimagesize($_FILES["profilepicture"]["tmp_name"]);
if($check == false)
{
header("refresh:0;url=profile.php?error=filetype");
return ;
}
}
if ($_FILES["profilepicture"]["size"] > 1000000)
{
header("refresh:0;url=profile.php?error=size");
return ;
}
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" )
{
header("refresh:0;url=profile.php?error=unsupported");
return ;
}
if (move_uploaded_file($_FILES["profilepicture"]["tmp_name"], $target_file))
{
//Push Images To Database
$conn = Connect();
$pfp = $conn->prepare("INSERT INTO `images` (`id`, `user`, `profile`, `pic1`, `pic2`, `pic3`, `pic4`) VALUES (NULL, :email, :pfp, '', '', '', '')");
$pfp->bindParam(':email', $_SESSION['email']);
$pfp->bindParam(':pfp', $target_file);
$pfp->execute();
$confirmed = $conn->prepare("UPDATE `users` SET `confirmed` = '1' WHERE `users`.`email` = :email");
$confirmed->bindParam(':email', $_SESSION['email']);
$confirmed->execute();
header("refresh:0;url=home.php");
}
else
{
header("refresh:0;url=profile.php?error=unknown");
}
?>
现在,脚本会上传个人资料图片,但是我如何检查其他文件并上传它们,即使它们是可选的?
答案 0 :(得分:0)
感谢Hasibur Rahaman的解决方案。我把它改成了一个函数,当$ _FILES [&#39;图像名称&#39;]不为空时调用它。