PHP / MySQL

  1. Home
  2. Computing & Technology
  3. PHP / MySQL

Upload a File and write to MySQL

By Angela Bradley, About.com

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.

Explore PHP / MySQL

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

PHP / MySQL

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. PHP with MySQL
  5. Upload a file to your server and add it's details to MySQL - SQL Upload File

©2009 About.com, a part of The New York Times Company.

All rights reserved.