This is using OLD mysql calls. Update it to mysqli or PDO. I havent needed it since I wrote it long ago, so it has not been updated. It works as is though.
Keep in mind, the Php default upload size is 2 meg. You will have to change your php.ini file to upload larger files.
[php]
File Upload To Database
Please Choose a File and click Submit
<?
require "config.php";
/*
Upload any type of files to MySQL.
Final Version 1.1
Last Modified Sunday, February 21, 2010 1:55:32 PM
Author Galaxy Internet - Kevin Rubio
Change Log Ver 1.1: Added array for allowed file types.
TO DO: Check to see if temp file gets deleted after it is inserted into MySQL. Do an unlink if it doesnt to delete the temp file.
*/
//------------------------------------------------------------------------------
// Allowed File Types - 1=Allow 0=Not Allowed
//------------------------------------------------------------------------------
//Image Types
$allow_jpeg =‘1’;
$allow_jpg =‘1’;
$allow_gif =‘1’;
$allow_png =‘1’;
// Document Types
$allow_txt=‘1’;
$allow_word=‘1’;
$allow_pdf=‘1’;
// Maximum upload size in bytes
$max_upload_size= ‘5000000’;
//------------------------------------------------------------------------------
if(!isset($_FILES[‘file’])) { echo ‘
Select a file to Upload
’;}
// Selected and uploaded a file
if (isset($_FILES[‘file’]) && $_FILES[‘file’][‘size’] > 0) {
$file= $_FILES[‘file’];
$file_name= $_FILES[“file”][“name”] ;
$file_type= $_FILES[“file”][“type”] ;
$file_size= $_FILES[“file”][“size”] ;
$file_location= $_FILES[“file”][“tmp_name”] ;
//------------------------------------------------------------------------------
// Check for maximum file upload size
//------------------------------------------------------------------------------
if ($file_size >"$max_upload_size") // In Bytes
{ echo “
<font color=“red”>Your file is too big!\n
The file $file_name is $file_size bytes. Upload max = $max_upload_size bytes”; die; }
//------------------------------------------------------------------------------
// Check for allowed file types
//------------------------------------------------------------------------------
$allowed_file_type = array();
if ($allow_jpeg==‘1’){
array_push($allowed_file_type, “image/jpeg”);
}
if ($allow_jpg==‘1’){
array_push($allowed_file_type, “image/jpg”);
}
if ($allow_gif==‘1’){
array_push($allowed_file_type, “image/gif”);
}
if ($allow_png==‘1’){
array_push($allowed_file_type, “image/png”);
}
if ($allow_pdf==‘1’){
array_push($allowed_file_type, “application/pdf”);
}
if ($allow_txt==‘1’){
array_push($allowed_file_type, “text/plain”);
}
if ($allow_word==‘1’){
array_push($allowed_file_type, “application/msword”);
}
if (!in_array ($file_type, $allowed_file_type)) {
die(“File Type $file_type not allowed.”);
}
//------------------------------------------------------------------------------
// Display Uploaded File Details
//------------------------------------------------------------------------------
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Size: " . $_FILES["file"]["size"] . " Bytes<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
//------------------------------------------------------------------------------
// Temporary file name stored on the server
$tmpName = $_FILES['file']['tmp_name'];
// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
// Insert file into database.
$query = "INSERT INTO $DBtable";
$query .= "(file, file_name, file_type) VALUES ('$data', '$file_name', '$file_type')";
$result = mysql_query($query) or die("<b><font color=\"#FF0000\">ERROR</font></b><br>There is a problem with your query <b>$query</b><br>MySQL says: " . mysql_error());
echo "Thank you, your file has been uploaded.";
// Close our MySQL Link
mysql_close($con);
}
?>
[/php]