简体   繁体   English

PHP表单上传多个图像文件

[英]PHP form uploading multiple image files

I'm trying to create a site where the user has to upload at least a profile picture, and 4 additional yet optional pictures. 我正在尝试创建一个站点,用户必须在该站点上载至少个人资料图片和4张其他可选图片。 I'm struggling to wrap my head around the logic required, so can someone please point me in a general direction. 我正在努力地围绕所需的逻辑,所以有人可以指出我的一般方向。 Here's the form: 形式如下:

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

And Here's the PHP script for it: 这是它的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");
}


?>

Right now, the script uploads the profile picture, but how do I get it to check for the other files and upload them too, even though they're optional? 现在,该脚本会上传个人资料图片,但是,即使它们是可选的,我如何获取它来检查其他文件并也上传它们?

Thanks to Hasibur Rahaman for the solution. 感谢Hasibur Ra​​haman的解决方案。 I changed it into a function and called it when $_FILES['image name'] is not empty. 我将其更改为函数,并在$ _FILES ['image name']不为空时调用了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM