Having problem downloading files from database but able to w
Posted: Wed Jul 30, 2008 6:56 pm
Gentlemen, I really need your help:
I am writing few piece of code that uploads files into database, then download the files from database:
Here is upload
And here is download.php that's being called from a page that list all files available to that user who owns:
Files uploaded to mysql with no problem, I can use mysql's field viewer to view jpgs I uploaded, but the weird thing is I cannot download. The downloaded JPEGs, Zips has the same size as the original file, but cannot be opened(corrupted).
But I can download PDFs!!!!
I even tried to write the file from db to a directory:
Why is it working for PDF but not JPEG, TIF, ZIP? Please HELP! I thought it's something wrong with encoding, so I even tried base64 encoding and decoding. But still only works for PDF, not JPEG, had people around me tried on their machine, they cannot open the JPEG either. I am going nuts..... 
I am writing few piece of code that uploads files into database, then download the files from database:
Here is upload
Code: Select all
if ( is_file($uploadedfile) && isset($userid) )
{
$database =& JFactory::getDBO();
$filesize = filesize($uploadedfile);
$fp = fopen($uploadedfile, 'r');
$content = fread($fp, $filesize);
$content = addslashes($content);
fclose($fp);
$storedata = "insert into udp_uploads values ('', '$userid', '$filename' , '$filesize', 'unreviewed', '$recordid', '$content',
'$original_type')";
$database->setQuery($storedata);
if (!$database->query()) { echo "<script>alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>"; }
}
Code: Select all
<?php
if(isset($_GET['fileid']))
{
include 'config.php';
include 'opendb.php';
$fileid = $_GET['fileid'];
$query = "SELECT name, size, type, filecontent FROM udp_uploads WHERE fileid = '$fileid'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $size, $type, $content) = mysql_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;
include 'closedb.php';
exit;
}
?>
But I can download PDFs!!!!
I even tried to write the file from db to a directory:
Code: Select all
<?php
if(isset($_GET['fileid']))
{
include 'config.php';
include 'opendb.php';
$fileid = $_GET['fileid'];
$query = "SELECT name, size, type, filecontent FROM udp_uploads WHERE fileid = '$fileid'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $size, $type, $content) = mysql_fetch_array($result);
//$content=base64_decode($content);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;
//$content is saved to desktop, but cannot be opened. For JPEGS, it says "No preview". But for PDF it just works.
$filename = $name;
$FileHandle = fopen($filename, 'w') or die("can't open file");
fwrite($FileHandle, $content);
fclose($FileHandle);
//Yet, files are successfully written to directory in webroot.
include 'closedb.php';
exit;
}
?>