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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PDF to MySQL and Back

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
I need to upload a PDF file into a MySQL database rather than to the file system and it's working but I'm not sure if the data stream is proper. The basic code for creating the data is here (I left out the MySQL calls as it is not the issue) so the variable $content should contain the necessary data.

Code:
$fp = fopen($source, 'r');
$content = fread($fp, filesize($source));
$content = addslashes($content);
fclose($fp);

However, viewing the raw data I see

0x255044462D312E360D25E2E3CFD30D0A3138342030 . . .

which does not appear to be in an expected format. I'm not sure what I expected but this looks nothing like any other data files I've inserted in the past, which were mainly images.

For viewing the file, I am using (database code omitted):

Code:
Header("Content-type: application/pdf");
Header("Content-Disposition: attachment; filename=$ImageName");

and the resulting file is about the proper size but attempts to open it in Adobe Reader gives an Out or Memory error and Adobe Acrobat just opens to a white screen. I think I missed something necessary in the inserting to MySQL code. Any ideas?
 
don't use addslashes.
Code:
$contentForMysql = mysql_real_escape_string(file__contents($pdfFile));

for the download make sure the imagename has no white space or better still put it in quotes.
run an output buffer and explicitly clean it before outputting the pdf
explicitly kill the script after

Code:
ob_start(); // first line of script
//lots of code


ob_end_clean();

//output binary headers
//output the PDF
exit();
 
Thank you. I've never needed to use ob_start() and related functions for saving and viewing blob images. Do you mean that it is necessary to do so with PDF files? The code I'm using is something I created years ago that works with images and I am now trying to add PDF functionality to it.
 
you don't _need_ to use them. this is to ensure that nothing gets output to the browser other than the PDF code. if you are certain of your coding skills and thus sure that there are no notices or blank lines or white space etc that gets output before the pdf or after it, then you can do without the ob_start etc.

there is no difference between a pdf and an image. they are all binary formats.

but do make sure that you never use addslashes for mysql storage. nor should you ever ever use magic_quotes and the like. escape and enquote variables properly.
 
I used to be certain of my programming skills but a long time of disuse and age have played their toll.

Anyway, interesting but when I don't use addslashes the resulting file has zero, or close to zero, bytes. For viewing, the PDF file is being output either directly to Acrobat Reader or to a file but, of course, with zero bytes there is nothing to display. With addslashes the files is the proper size but then gives an Out of Memory error upon loading. Any more clues?
 
zero bytes suggests that it did not get stored properly.
improper storage suggests a database insert error
most likely cause of that is improper escaping.

I guess you're not using mysql_real_escape_string as suggested?

 
No, mysql_real_escape_string is depreciated and I haven't yet tried the current version, if there is one.
 
use it.

the whole mysql extension is deprecated. but if you are using it you must use the mysql_real_escape_sting to escape a string. you still must enquote it yourself.

there is little likelihood of the mysql extension being removed from the phone core shortly.
 
This seems to be working properly now and I modified my image viewing script to fetch it back to the reader, either to view in Acrobat Reader or to save to their file system:

Code:
if ($source = $_FILES["OriginalDoc"]["tmp_name"]) {
	if (file_exists($source)) {

		$fp = fopen($source, "rb");
		while(!feof($fp)) {
			$content .= fread($fp, 1024*20);
		}
	fclose($fp);
	// Code here to insert $content to MySQL
	}
}

(The above is paraphrased so, if a reader tries it, double-check all the opening and closing brackets!)

To view it, after opening the database and fetching the PDF stream from the table as $im and the name to use for the file as $ImageName:

Code:
Header("Pragma: no-cache");
Header("Content-type: application/pdf");
Header("Content-Disposition: attachment; filename=$ImageName");
echo $im;

If inserting an image file, then use this for viewing but note that the purpose of this script is for downloading or viewing an image in a new screen. If you need to view an image inline, you'll need to change the disposition. $mime_type is from the database as jpeg, gif or png and was created when uploading the image to the database.

Code:
Header("Pragma: no-cache");
Header("Content-type: image/".$mime_type."\"");
	Header("Content-Disposition: attachment; filename=$ImageName");
 
To future readers: Remember to enquote and escape the binary code before inserting to a database unless you use load data infile syntax.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top