如何将图像上传添加到现有表单?

时间:2015-03-13 14:29:13

标签: php mysql image forms

我有一个我正在构建的表单,它将表单内容上传到数据库(mysql)和单独的页面以显示数据库的内容。到目前为止一切都很好。我需要用户能够上传图像文件和表单,我需要图像本身在页面上显示数据库内容。

如何通过修改现有代码来实现此目的?我已经包含了表单代码,发布到数据库的代码以及显示以下内容的页面代码。

谢谢!

形式:

<form name="sponsor-registration" method="post" enctype="multipart/form-data"        action="sponsor-registration.php">
 <div class="formcentered">
 <div class="formfield"><input type="text" name="yourname" size="30" maxlength="70" value="" required></div><div class="formlabel">Your Name:</div>
 <br class="clearfloat" />
 <div class="formfield"><input type="text" name="email" size="30" maxlength="70" value="" required></div><div class="formlabel">Email:</div>
 <br class="clearfloat" />
 <div class="formfield"><input type="text" name="phone" size="30" maxlength="20" value="" required></div><div class="formlabel">Phone</div>
 <br class="clearfloat" />
 <div class="formfield"><input type="text" name="sponsorname" size="30" maxlength="70" value="" required></div><div class="formlabel">Sponsor Name:</div>
 <br class="clearfloat" />
 <div class="formfield"><input type="text" name="sponsorshiplevel" size="30" maxlength="70" value="" required></div><div class="formlabel">Sponsorship Level:   </div>
 <br class="clearfloat" />
 <p class="pagecentered"><input type="submit" name="submit" value="submit">  </p>
 </div>
 </form>

发布到数据库:

<?php //start php tag
//include connect.php page for database connection
include('connect.php');
//if submit is not blanked i.e. it is clicked.
{
$sql="insert into      sponsors2015(yourname,email,phone,sponsorname,sponsorshiplevel,logofile) values('".$_REQUEST['yourname']."', '".$_REQUEST['email']."', '".$_REQUEST['phone']."', '".$_REQUEST['sponsorname']."', '".$_REQUEST['sponsorshiplevel']."')";


$res=mysql_query($sql);
if($res)



{
Echo header('Location: sponsor-registration-success.php');
}
Else
{
Echo header('Location: sponsor-registration-problem.php');
}

}

?>

显示数据库的内容     

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, yourname, email, phone, sponsorname, sponsorshiplevel,    logofile FROM sponsors2015";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "ID: " . $row["id"]. "<br>". " Name: " . $row["yourname"]. "<br>". "  Email: " . $row["email"]. "<br>". " Phone: " . $row["phone"]. "<br>". "Sponsor Name: " . $row["sponsorname"]. "<br>". "Sponsorship Level: " . $row["sponsorshiplevel"]. "<br>". "<hr>";
}
} else {
echo "0 results";
}
$conn->close();
?>

1 个答案:

答案 0 :(得分:0)

要添加图像,您需要一个文件输入标记

在此之后,您需要按以下<input type="file" name="profilepic" accept=".jpeg,.jpg,.png" />

处理该文件
 <?php function imagEupload()
{
global $picerror;

if ($_FILES['profilepic']['error'] == 4) {
    $picerror[] = "<p class='formerrors'>No picture was uploaded,kindly upload one.</p>";
}

if ($_FILES['profilepic']['error'] >= 1) {
    $picerror[] = "<p class='formerrors'> There was an error processing your image," . $_FILES['profilepic']['name'];
} else {
    $validfiletype = ['jpg', 'jpeg', 'png']; // starts an array to hold the valid file extensions.
    $max_file_size = 1024 * 3072; // 3Mb
    $uploadpath = "uploads/";
    $uploadedfilename = $_FILES['profilepic']['name'];

    if ($_FILES['profilepic']['size'] > $max_file_size) {
        $picerror[] = "<p class='formerrors'>The uploaded file," . " $uploadedfilename" . " is too large</p>";
    } else {
//            get the file extension and check it to make sure it tallies with the valid extensions set in the array

        if (!in_array(pathinfo($_FILES['profilepic']['name'], PATHINFO_EXTENSION), $validfiletype)) {
            $picerror[] = "<p class='formerrors'> The uploaded file," . " $uploadedfilename" . " is not a valid image</p>";
        } else {

            $fileext = pathinfo($_FILES['profilepic']['name'], PATHINFO_EXTENSION);
            $filename = uniqid($_FILES['profilename']['name']) . ".$fileext";


            if (file_exists($uploadpath . $filename)) {
                $picerror[] = "<p class='formerrors'>The uploaded File," . " $uploadedfilename" . " exists,please upload another or do consider renaming it.</p>";
            } else {
                $filepath = $uploadpath . $filename;
                if (move_uploaded_file($_FILES['profilepic']['tmp_name'], $filepath)) {

                    global $newfilepath;

                    $newfilepath = $filepath; // input this to your database, to echo the image, it would be something like this <img src='<?php echo $_row['image'] ;' />

                } else {
                    $picerror[] = "<p class='formerrors'>We could not process your image due to an unavoidable error,Please contact us or try again</p>";
                }
            }
        }
    }
}
}
?>  

你也可以参考php手册http://php.net/manual/en/features.file-upload.php

顺便说一下,不推荐使用mysql扩展,请考虑使用mysqli或PDO,header('location') ;不使用echo。我希望这有帮助