PHP Script to Upload an Image and Write to MySQL

Allow a Website Visitor to Upload an Image

PHP Code
Scott-Cartwright / Getty Images

Website owners use PHP and MySQL database management software to enhance their website capabilities. Even if you want to allow a site visitor to upload images to your web server, you probably don't want to bog down your database by saving all the images directly to the database. Instead, save the image to your server and keep a record in the database of the file that was saved so you can reference the image when needed. 

01
of 04

Create a Database

First, create a database using the following syntax:

This SQL code example creates a database called visitors that can hold names, email addresses, phone numbers, and the names of the photos.

02
of 04

Create a Form

Here is an HTML form that you can use to collect information to be added to the database. You can add more fields if you want, but then you'd also need to add the appropriate fields to the MySQL database.

<form enctype="multipart/form-data"
action="add.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br>
Phone: <input type="text" name = "phone"><br>
Photo: <input type="file" name="photo"><br>
<input type="submit" value="Add">   </form>
03
of 04

Process the Data

To process the data, save all the following code as add.php. Basically, it gathers the information from the form and then writes it to the database. When that is done, it saves the file to the /images directory (relative to the script) on your server. Here is the necessary code along with an explanation of what is going on.

Designate the directory where the images will be saved with this code:

<?php
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']); 

Then retrieve all the other information from the form: 

$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']); 

Next, make the connection to your database: 

mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()) ;
mysql_select_db("Database_Name") or die(mysql_error()) ; 

This writes the information to the database: 

mysql_query("INSERT INTO 'visitors' VALUES ('$name', '$email', '$phone', '$pic')") ; 

This writes the photo to the server 

if(move_uploaded_file($_FILES['photo']['tmp_name'],$target))

This code tells you if it is all ok or not.

echo "The file ". basename( $_FILES['uploadedfile']
['name']). " has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your file."; }?> 

If you only allow photo uploads, consider limiting the allowed file types to JPG, GIF, and PNG. This script doesn't check if the file already exists, so if two people both upload a file called MyPic.gif, one overwrites the other. A simple way to fix this is to rename each incoming image with a unique ID.

04
of 04

View Your Data

To view the data, use a script like this one, which queries the database and retrieves all the information in it. It echos each back until it has shown all the data.

<?php
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()) ;
mysql_select_db("Database_Name") or die(mysql_error()) ;
$data = mysql_query("SELECT * FROM visitors") or die(mysql_error());
while($info = mysql_fetch_array( $data )) {
Echo "<img src=http://www.yoursite.com/images/".$info['photo'] ."> <br>"; Echo "<b>Name:</b> ".$info['name'] . "<br> "; Echo "<b>Email:</b> ".$info['email'] . " <br>"; Echo "<b>Phone:</b> ".$info['phone'] . " <hr>"; } ?>

To show the image, use normal HTML for the image and only change the last part—the actual image name—with the image name stored in the database. More information on retrieving information from the database can be found in a PHP MySQL tutorial.

Format
mla apa chicago
Your Citation
Bradley, Angela. "PHP Script to Upload an Image and Write to MySQL." ThoughtCo, Aug. 13, 2021, thoughtco.com/upload-a-file-and-write-to-mysql-2694113. Bradley, Angela. (2021, August 13). PHP Script to Upload an Image and Write to MySQL. Retrieved from https://www.thoughtco.com/upload-a-file-and-write-to-mysql-2694113 Bradley, Angela. "PHP Script to Upload an Image and Write to MySQL." ThoughtCo. https://www.thoughtco.com/upload-a-file-and-write-to-mysql-2694113 (accessed April 16, 2024).