PHP表单上传多个图像文件 [英] PHP form uploading multiple image files

查看:155
本文介绍了PHP表单上传多个图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个网站,用户必须至少上传一张个人资料照片,以及4张额外的可选照片。我很努力地把自己的头围绕着所需的逻辑,所以有人可以请我指出一个大方向。以下是表单:

I'm trying to create a site where the user has to upload at least a profile picture, and 4 additional yet optional pictures. 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>

以下是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?

推荐答案

感谢Hasibur Ra​​haman提供的解决方案。我将它改为一个函数,并在$ _FILES ['image name']不为空时调用它。

Thanks to Hasibur Rahaman for the solution. I changed it into a function and called it when $_FILES['image name'] is not empty.

这篇关于PHP表单上传多个图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆