PHP Script to Upload an Image and Write to MySQL

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 visitor to your website 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. 

of 04

Create a Database

First, you'll need to create a database.

CREATE TABLE employees (name VARCHAR(30), email VARCHAR(30), phone VARCHAR(30), photo VARCHAR(30)) 

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

of 04

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

of 04

Processing the Data

To process the data, save the following code as add.php. To understand what each step of this script does, read the comments in the code. 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.

 //This is the directory where images will be saved 
 $target = "images/"; 
 $target = $target . basename( $_FILES['photo']['name']); 
 //This retrieves all the other information from the form 
 // Connects to your database 
 mysql_connect("", "username", "password") or die(mysql_error()) ; 
 mysql_select_db("Database_Name") or die(mysql_error()) ; 
 //Writes the information to the database 
 mysql_query("INSERT INTO 'employees' VALUES ('$name', '$email', '$phone', '$pic')") ; 
 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
 //Tells you if it is all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 else { 
 //Gives an error if it is
not ok 
 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 remedy this is to rename each incoming image with a unique ID.

of 04

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

// Connects to your Database 
mysql_connect("", "username", "password") or die(mysql_error()) ; 
mysql_select_db("Database_Name") or die(mysql_error()) ; 

//Retrieves data from MySQL 
$data = mysql_query("SELECT * FROM employees") or die(mysql_error());

//Puts it into an array 
while($info = mysql_fetch_array( $data )) { 

//Outputs the image and other data 
Echo "<img src=".$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. For more information on retrieving information from the database, read this PHP MySQL tutorial.

mla apa chicago
Your Citation
Bradley, Angela. "PHP Script to Upload an Image and Write to MySQL." ThoughtCo, Dec. 4, 2017, Bradley, Angela. (2017, December 4). PHP Script to Upload an Image and Write to MySQL. Retrieved from Bradley, Angela. "PHP Script to Upload an Image and Write to MySQL." ThoughtCo. (accessed January 24, 2018).