//, PHP/Upload Files to MySQL using PHP Tutorial

Upload Files to MySQL using PHP Tutorial

Upload Files to MySQL using PHP Tutorial

I much prefer to upload files to mysql instead of saving them
directly to the file system. I can run database backups/mirrors that
are much easier to manage then if files were placed on the file system.

Here are the simple scripts I use to upload files and a script to stream the file back to the browser.

MySQL Database Script:

CREATE TABLE `UploadedFiles` (
`UploadedFileID` int(11) NOT NULL auto_increment,
`name` varchar(30) default NULL,
`type` varchar(30) default NULL,
`size` int(11) default NULL,
`content` longblob,
PRIMARY KEY (`UploadedFileID`)
) TYPE=MyISAM;


Upload Script:

'holds db constructor
include("{$_SERVER['DOCUMENT_ROOT']}/phplibrary/dbconnect.php");
###################
//Consume Post Vars
###################
if (!empty($_POST['upload']))
{
//Loop thru Post Array
foreach($_POST as $key => $value) {
$$key = $value;
}

$connect->connect_db(mydatabase)

//Insert File
if(isset($_POST[’upload’]) && $_FILES[’SpecialFile’][’size’] > 0) {
$fileName = $_FILES[’SpecialFile’][’name’];
$tmpName = $_FILES[’SpecialFile’][’tmp_name’];
$fileSize = $_FILES[’SpecialFile’][’size’];
$fileType = $_FILES[’SpecialFile’][’type’];
$fp= fopen($tmpName, ‘r’);
$content = addslashes($content);
fclose($fp);
}
if(!get_magic_quotes_gpc()) {
$fileName = addslashes($fileName);
}

$query = “INSERT INTO UploadedFiles (name, size, type, content) “.
“VALUES (’$fileName’, ‘$fileSize’, ‘$fileType’, ‘$content’)”;

$result = mysql_query($query);
if (!$result) {
dberror (mysql_error(), $_SERVER[’PHP_SELF’] );
echo mysql_error();
}

//Display Confirmation
$message = urlencode(”Your file has been uploaded.”);
header( “Location: confirmed.php?m=$message” );
exit;
}
?>

Upload File:

To download the file I prefer to push the file down as octet
stream/binary data (NOTE: To display images you would need to push out
the particular mime type). That way users get the “Save” dialoge
instead of the browser (especially IE) automatically trying to open
with what it *thinks* should. The following codes displays the file in
a drop down list for users to select and have the file streamed to
their web browser.

Download File Script

//holds db constructor
include("{$_SERVER['DOCUMENT_ROOT']}/phplibrary/dbconnect.php");

if (!empty($_POST[‘GetFile’]))
{
$UploadedFileID = $_POST["UploadedFileID"];
$connect->connect_db(mydatabase)
$query = “SELECT * FROM UploadedFile WHERE UploadedFileID = $UploadedFileID”;
$result = mysql_query( $query );
if( !$result ) {
echo mysql_error();
exit;
}

$row = mysql_fetch_array( $result );
if (!empty($row[”content”]))
{
// Output the MIME header – Force as Octet Stream
// You could get this from the FileType Column
header(”Content-type: application/octet-stream”);
header(”Content-Length: ” . strlen($row[’content’]) );
header(”Content-Type: application/octet-stream”);
header(’Content-Disposition: attachment; filename=”‘.$row[’name’].’”‘);
header(”Content-Transfer-Encoding: binary\n”);
echo $row[’content’];
}

}
?>

– Select File –

$connect->connect_db(mydatabase)
$result = mysql_query(”SELECT UploadedFileID, name FROM UploadedFiles”);
if ($myrow = mysql_fetch_array($result)) {
do { ?>

2010-05-25T22:50:28+00:00 November 22nd, 2007|MySQL, PHP|0 Comments

About the Author:

A little background - I have a bachelors degree in computer science from the University of Cincinnati, graduating in 2005. I’ve worked on numerous fortune 100 websites and web applications. I’m most interested in Web work but also have a good knowledge of how online technology is effecting/changing marketing.

Technologies I’m Experienced With:

  • ASP.NET 1.1/2.0
  • PHP
  • Databases (MS SQL, MySQL, Oracle)
  • AJAX
  • Microsoft Great Plains

Marketing Ideas I Explore:

  • Google Adwords/Adsense
  • Software Marketing
  • Email / Blog Marketing
  • Online Tracking

What’s Next For Me?

  • Silverlight
  • Ruby

Leave A Comment