July 20th, 2006

You are currently browsing the articles from TechToolBlog written on July 20th, 2006.

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’];
}

}
?>



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

Written by Tim on July 20th, 2006 with 18 comments.
Read more articles on php.



Famous Homemade Soups