I want to be able to upload both binary and ascii files. The code below seems to work and have been able to upload pdfs, jpgs and zips. The size of the file is correct but when I go to view the file, it seems to be corrupted. Say for example I go to retrieve the file with the default application, the applications say no data, corrupt, etc.
Table configuration:
Code: Select all
$query="CREATE TABLE tbl_Files (id_files tinyint(3) unsigned NOT NULL auto_increment,bin_data longblob NOT NULL,description tinytext NOT NULL,filename varchar(50) NOT NULL,filesize varchar(50) NOT NULL,filetype varchar(50) NOT NULL,PRIMARY KEY (id_files))";Code: Select all
<?php
include "voc_header.php";
if ($_POST['action'] == "upload") {
// ok, let's get the uploaded data and insert it into the db now
$binFile='';
$filetype = $_FILES['binFile']['type'];
$filename = $_FILES['binFile']['name'];
$filesize = $_FILES['binFile']['size'];
$binFile=$_POST['binFile'];
$txtDescription=$_POST['txtDescription'];
$MAX_FIlE_SIZE=$_POST['MAX_FILE_SIZE'];
$db = mysql_connect("localhost", "$username", "$password");
mysql_select_db($database, $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");
if (isset($filename) && $filename != "") {
$data = fread(fopen($_FILES['binFile']['tmp_name'], "rb"), $_FILES['binFile']['size']);
$data = base64_encode($data);
$strDescription= "(\"${filetype}\",\"${filename}\",\"${data}\")";
$sql = "INSERT INTO tbl_Files ";
$sql .= "(description, bin_data, filename, filesize, filetype) ";
$sql .= "VALUES ('$txtDescription', '$data', ";
$sql .= "'$filename', '$filesize', '$filetype')";
$result = mysql_query($sql, $db);
mysql_free_result($result);
echo "<p><center>The new file was successfully added to the database.<br><br>";
echo "<a href='upload_file.php'><font color='white'><u>Upload Another File</u></font></a></center>";
} else { echo "<p><center>There was an error in your upload, please go back and correct any errors</center>"; }
mysql_close();
} else {
echo "<a href='upload_view.php'><font color='white'><u>View Uploads</u></font></a></center>";
?>Code: Select all
<?php
$id_files=$_GET['id_files'];
include("dbinfo.inc.php");
$db = mysql_connect("localhost", "$username", "$password");
mysql_select_db($database, $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");
if ($id_files) {
$sql = "SELECT bin_data, filetype, filename, filesize FROM tbl_Files WHERE id_files=$id_files";
$result = @mysql_query($sql, $db);
$data = @mysql_result($result, 0, "bin_data");
$name = @mysql_result($result, 0, "filename");
$size = @mysql_result($result, 0, "filesize");
$type = @mysql_result($result, 0, "filetype");
header("Content-type: $type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Content-Description: PHP Generated Data");
echo $data;
}
?>

