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

Storing User Submitted Data and Files in MySQL

By , About.com Guide

4 of 7

Adding File Uploads to MySQL

Next we need to actually create upload.php, which will take our users file and store it in our database. Below is sample coding for upload.php.

<?php
mysql_connect("your.server.com","username","password");
mysql_select_db("database_name");
$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
$result=MYSQL_QUERY("INSERT INTO uploads (description, data,filename,filesize,filetype) ". "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");
$id= mysql_insert_id();
print "<p>File ID: <b>$id</b><br>";
print "<p>File Name: <b>$form_data_name</b><br>";
print "<p>File Size: <b>$form_data_size</b><br>";
print "<p>File Type: <b>$form_data_type</b><p>";
print "To upload another file <a href=http://www.yoursite.com/yourpage.html> Click Here</a>";
?>

Learn more about what this actually does on the next page.

Explore PHP / MySQL
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. PHP with MySQL
  5. SQL Form - SQL File - File Upload

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

All rights reserved.