1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Upload a File and write to MySQL

By

1 of 4

Create a Database
On our site, we have tutorials about adding data to a MySQL database, and tutorials about uploading files, but recently one of our forum users asked:
"I know how to submit data into a mySQL table through a form. Now I want to upload the image file to the remote directory (say, 'images/') but at the same time save the file-name in the table. "
This is a very common question because it has a lot of uses. Often you want a user to be able to upload a photo, but you don't want to bog down your database space by saving all the images directly into the database. You instead save the image to your server, but keep a record in the database of what file was saved so you can easily reference the image when needed. First let's create a database:
CREATE TABLE employees (name VARCHAR(30), email VARCHAR(30), phone VARCHAR(30), photo VARCHAR(30)) 
This SQL code creates a database called 'employees' that can hold their name, email, phone and the name of their photo.
  1. About.com
  2. Technology
  3. PHP / MySQL
  4. PHP with MySQL
  5. Upload a File to Your Server and Add Its Details to MySQL

©2014 About.com. All rights reserved.