Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving PDF in Database

Status
Not open for further replies.

jsteiner87

Programmer
Oct 9, 2003
76
US
I am trying to save PDF files in a database. I have made an upload page with PHP and interface the database with SQLyog to do the design and other things as well. When I upload my file using my upload page it uploads everything just fine (images, excel files, and pdf files). However when I try to download them via another php page everything works but the PDF files are corrupted somehow.

The error I get in Adobe PDF: "There was an error opening this document. The file cannot be found"

I then try and get the files thru SQLyog and the same problem. Right now I have them being saved in a longblob. Is this the right data type? Why would everything work but PDF files?

MySQL Version 4.1.16
 
A LONGBLOB can contain a file up to 4GB in size, so it's certainly a safe choice. Have you done any before/after comparison of your files? Can you show us the SQL code which is used to upload and download your files?
 
Here is my uploading code that processes the information. Any help would be great.

if(isset($_POST['upload'])) {
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp = fopen($tmpName, 'r');
$content = fread($fp, $fileSize);
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}

$query = "INSERT INTO table (file_title, file_name, file_size, file_type, file_content) VALUES ('$fileTitle', '$fileName', '$fileSize', '$fileType', '$content')";

mysql_select_db($database_conn, $database);
mysql_query($query) or die(mysql_error());

echo "<br>File $fileName uploaded<br>";
}
?>
 
Regarding the before/after comparison, is there any difference in the file sizes? Have they been truncated?
 
This is my download script.

<?php
if(isset($_GET['id']))
{
$id = $_GET['id'];
mysql_select_db($database_conn, $database);
$query = "SELECT file_name, file_type, file_size, file_content FROM table WHERE id = '$id'";
$result = mysql_query($query, $database) or die(mysql_error());
list($name, $type, $size, $content) = mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;
exit;
}
?>

I don't know if I am in the right forum now. I have figured out that it is not the database that I am having problems with. It is something wrong with my download script and IE. If I use the download script above my browser asks me if I want to save it or open it. When I save it to my desktop it opens up with no errors. If I try to open it without saving it to the desktop, it gives me this error in Adobe. "There was an error opening this document. The file does not exist." When I try this in FireFox I don't get an error either way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top