pdf to mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Anant
Forum Commoner
Posts: 66
Joined: Wed Jul 14, 2010 11:46 am

pdf to mysql

Post by Anant »

Hi all,

As am new to mysql and php. i am just looking for ways i can store pdf file directly to database.
I don't have to write any upload page in php and all. I just want to insert a pdf file that is saved in my system to database using mysql.

Any suggestions ?

Thank You
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: pdf to mysql

Post by buckit »

I know you arent uploading, but... Upload script:

Code: Select all

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0){
	$eid = sanitize($_POST['i']);
	$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($tmpName));
	$content = addslashes($content);
	fclose($fp);
	
	if(!get_magic_quotes_gpc())
	{
		$fileName = addslashes($fileName);
	}
	
	$sql = "INSERT INTO files (file_name, file_size, file_type, file_content,expert_id) ".
	"VALUES ('$fileName', '$fileSize', '$fileType', '$content','$eid')";
	

		if($db->Execute($sql)){
			$success = "File $fileName uploaded<br>";
		}else{
			$error = "Could not process file";
		}

} 
the file_content field in the database is a mediumblob... but regardless... should be blob.

here is how I call it for download:

Code: Select all

if(isset($_GET['d']) && $_GET['d'] != ''){

	$fid = sanitize($_GET['d']);
	$files = $db->Execute("SELECT * FROM files WHERE file_id = ".$fid);
		if($files->RecordCount() == 1){
			$size = $files->fields['file_size'];
			$type = $files->fields['file_type'];
			$name = $files->fields['file_name'];
			$content = $files->fields['file_content'];
			header('Content-Type: application/octet-stream; name="'.$name.'"'); 
			header('Content-Disposition: attachment; filename="'.$name.'"'); 
			header('Accept-Ranges: bytes'); 
			header('Pragma: no-cache'); 
			header('Expires: 0'); 
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
			header('Content-transfer-encoding: binary'); 
			header('Content-length: ' . $size); 
			
			//header("Content-length: ".$size);
			//header("Content-type: ".$type);
			//header("Content-Disposition: attachment; filename=".$name);
			print $content;
		}else{
			$error = "File not found";
		}
}
Gargoyle
Forum Contributor
Posts: 130
Joined: Wed Jul 14, 2010 12:25 am

Re: pdf to mysql

Post by Gargoyle »

you don't need to upload the file if you have a local lamp server.

consider this code:

Code: Select all

$data = file_get_contents('/path/to/myfile.pdf');
$data = mysql_real_escape_string($data);
mysql_query('INSERT INTO table(myblob) VALUES("'.$data.'")');
Anant
Forum Commoner
Posts: 66
Joined: Wed Jul 14, 2010 11:46 am

Re: pdf to mysql

Post by Anant »

Below is the code that am using for download but at the moment it just echo the content to the page in binary. But i would like to have a option to save it to the system and need a correct data and not binary.

Any clue ??

Code: Select all

<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
include 'library/config.php';
include 'library/opendb.php';

$query = "SELECT id, name FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
}
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="download.php?id=<?php=$id;?>"><?php=$name;?></a> <br>
<?php
}
}
include 'library/closedb.php';
?>
</body>
</html>

<?php
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database

include 'library/config.php';
include 'library/opendb.php';

$id    = $_GET['id'];
$query = "SELECT name, type, size, content " .
         "FROM upload WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
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;

include 'library/closedb.php';
exit;
}

?>
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: pdf to mysql

Post by buckit »

you need to call headers BEFORE any output to the browser.

move this:

Code: Select all

<?php
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database

include 'library/config.php';
include 'library/opendb.php';

$id    = $_GET['id'];
$query = "SELECT name, type, size, content " .
         "FROM upload WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
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;

include 'library/closedb.php';
exit;
}

?>
to the very top and see if that helps.
Anant
Forum Commoner
Posts: 66
Joined: Wed Jul 14, 2010 11:46 am

Re: pdf to mysql

Post by Anant »

No it didn't really helped - All am getting is -

Code: Select all

PDF-1.6 %âãÏÓ 275 0 obj <> endobj xref 275 26 0000000016 00000 n 0000002075 00000 n 0000002194 00000 n 0000002323 00000 n 0000002644 00000 n 0000002730 00000 n 0000002791 00000 n 0000003717 00000 n 0000004553 00000 n 0000005380 00000 n 0000006206 00000 n 0000007068 00000 n 0000007873 00000 n 0000008547 00000 n 0000009250 00000 n 0000009403 00000 n 0000009613 00000 n 0000010177 00000 n 0000010391 00000 n 0000010800 00000 n 0000010888 00000 n 0000011705 00000 n 0000012617 00000 n 0000012749 00000 n 0000018152 00000 n 0000000816 00000 n trailer <<069C366835D4974DAC1A208A610152FF>]>> startxref 0 %%EOF 300 0 obj <>stream xÚÜVoL[U¿ïž¥Ð”€vc±kì±NÑt‹%o $dY°}ŒBH_iÚ5n&˜I4‹šÒbm¤ƒ!"ò¡¸ ý"ë2@³,faÑ©ŽLÌ2粡_\Œ‹‰‰÷___×:}É»÷œßùsçœw›�X�b�-Ð€ì£Š€òcðß.WM>WÇ›y�.ˆ?9BÅgU¶ê¤%Ü}o‰‰)渙҅í,ý>~öjO䏡7›Õ=W_ù|ÐYçž)MÕLÕ§{O_Ýr,9ËÜ;Êm­â¥>†µ)ï—'›Â^°²+Á~ÉíW½]ÚV?Úÿ#ˆ(~.u÷êÓG“øD‡5VªZÙ~Öan/Ÿú±‡ý4r¾d¦l®nó•ô¢á>ûÚPD³[?tÄp,|›ÙÎUŒ?~Ð`iîâÃ|³KƒÜ‡e_èk?ëá¿™~ÁѦyÝ2wl¢zþ׺•†„._û3RbûþZ¬ÞA˵}ߍõÝi91¹>ÿÉñ€nþañîÁöw§îîwõg›d¶³QqíˆKˆì½µÊ'7\BTy¾ñª.S»ÅªágÜ‚8b±êíì©Ö«cÏóg\½­¶Ô†rKÕTµ&ˆ§lÓfë6$¤6ìì˜à‡±,$DåÏ@p›§Cf"QYç|ŸâÌsö«Ã£„ ¤±÷Ö’¯u’WGÓOBD& rrj’¢2\EÓ?™2yäÈC‡fMP -ÊoH †ãŠ‘\JXX)k—ÿï…Gt2Kéß¶ô¡Jló…,Içi3 ü¶æ”Ú"b0öÃË[‡\nâüÒújËrµ$„kܞΈš¾ÇnQNžFá᱂[…9 ãr8XÕ¶‹˜ƒü;ßR©cîÌ©Ç×1Hãιr7tk›^—¤žÞ“uUÝ€ú¼ùû˜b‰[°æFÖ &„LR£ü9˜sŽù'=!©Z?é1¹š_¢q­Ç%Llinë0÷¾Šº**qü¢$}57”èÊ'1C6;…h^¯2 ì¦cð­Çñ¼(«÷v¾ØÚ2¬BæOWê\á£_e²ø þÁA £ÕÅ¡¨­-‚ª–l�(ÑÚÒWE£ ûé$œÁ8‘Y£Y’‹…®l¬Ìš›”Èr„¬äÂÈŽ¤édQÔJŽÇ8ƒgD!œ”‘IÍ´(9š›Dãä„=ÉÄäÕF³â^´´Èë!Ìòê¤é°5S[î¡TC#É8hÁób@ò$½eW®`B†îòáPJÚŠŒ #–b!žÉÇh›„Œ;ÖjåÇp”ºœœ‡•ÂBgŽ]ÑH
I am assuming the above is binary data where as i need a pdf file back with option to save it to the system.

Not sure what am doing wrong here...
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: pdf to mysql

Post by buckit »

try setting a content type so the browser knows what to do with it

Code: Select all

header("Content-type: application/pdf"); 
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: pdf to mysql

Post by buckit »

my bad... looks like you already have that... you sure that $type is returning "application/pdf"?

this is the only other header I have when I deal strictly with PDFs

Code: Select all

header("Content-Transfer-Encoding: Binary");
Anant
Forum Commoner
Posts: 66
Joined: Wed Jul 14, 2010 11:46 am

Re: pdf to mysql

Post by Anant »

I tried changing the content type to pdf like you mentioned above but still am getting following output -

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /asd/fers/ere/test.php:2) in /asd/fers/ere/test.php:2  on line 7

Warning: Cannot modify header information - headers already sent by (output started at /asd/fers/ere/test.php:2) in /asd/fers/ere/test.php:2 on line 8

Warning: Cannot modify header information - headers already sent by (output started at /asd/fers/ere/test.php:2) in /asd/fers/ere/test.php:2 on line 9

Warning: Cannot modify header information - headers already sent by (output started at /asd/fers/ere/test.php:2) in /asd/fers/ere/test.php:2 on line 10

%PDF-1.6 %âãÏÓ 275 0 obj <> endobj xref 275 26 0000000016 00000 n 0000002075 00000 n 0000002194 00000 n 0000002323 00000 n 0000002644 00000 n 0000002730 00000 n 0000002791 00000 n 0000003717 00000 n 0000004553 00000 n 0000005380 00000 n 0000006206 00000 n 0000007068 00000 n 0000007873 00000 n 0000008547 00000 n 0000009250 00000 n 0000009403 00000 n 0000009613 00000 n 0000010177 00000 n 0000010391 00000 n 0000010800 00000 n 0000010888 00000 n 0000011705 00000 n 0000012617 00000 n 0000012749 00000 n 0000018152 00000 n 0000000816 00000 n trailer <<069C366835D4974DAC1A208A610152FF>]>> startxref 0 %%EOF 300 0 obj <>stream xÚÜVoL[U¿ïž¥Ð”€vc±kì±NÑt‹%o $dY°}ŒBH_iÚ5n&˜I4‹šÒbm¤ƒ!"ò¡¸ ý"ë2@³,faÑ©ŽLÌ2粡_\Œ‹‰‰÷___×:}É»÷œßùsçœw›�X�b�-Ð€ì£Š€òcðß.WM>WÇ›y�.ˆ?9BÅgU¶ê¤%Ü}o‰‰)渙҅í,ý>~öjO䏡7›Õ=W_ù|ÐYçž)MÕLÕ§{O_Ýr,9ËÜ;Êm­â¥>†µ)ï—'›Â^°²+Á~ÉíW½]ÚV?Úÿ#ˆ(~.u÷êÓG“øD‡5VªZÙ~Öan/Ÿú±‡ý4r¾d¦l®nó•ô¢á>ûÚPD³[?tÄp,|›ÙÎUŒ?~Ð`iîâÃ|³KƒÜ‡e_èk?ëá¿™~ÁѦyÝ2wl¢zþ׺•†„._û3RbûþZ¬ÞA˵}ߍõÝi91¹>ÿÉñ€nþañîÁöw§îîwõg›d¶³QqíˆKˆì½µÊ'7\BTy¾ñª.S»ÅªágÜ‚8b±êíì©Ö«cÏóg\½­¶Ô†rKÕTµ&ˆ§lÓfë6$¤6ìì˜à‡±,$DåÏ@p›§Cf"QYç|ŸâÌsö«Ã£„ ¤±÷Ö’¯u’WGÓOBD& rrj’¢2\EÓ?™2yäÈC‡fMP -ÊoH †ãŠ‘\JXX)k—ÿï…Gt2Kéß¶ô¡Jló…,Içi3 ü¶æ”Ú"b0öÃË[‡\nâüÒújËrµ$„kܞΈš¾ÇnQNžFá᱂[…9 ãr8XÕ¶‹˜ƒü;ßR©cîÌ©Ç×1Hãιr7tk›^—¤žÞ“uUÝ€ú¼ùû˜b‰[°æFÖ &„LR£ü9˜sŽù'=!©Z?é1¹š_¢q­Ç%Llinë0÷¾Šº**qü¢$}57”èÊ'1C6;…h^¯2 ì¦cð­Çñ¼(«÷v¾ØÚ2¬BæOWê\á£_e²ø þÁA £ÕÅ¡¨­-‚ª–l�(ÑÚÒWE£ ûé$œÁ8‘Y£Y’‹…®l¬Ìš›”Èr„¬äÂÈŽ¤édQÔJŽÇ8ƒgD!œ”‘IÍ´(9š›Dãä„=ÉÄäÕF³â^´´Èë!Ìòê¤é°5S[î¡TC#É8hÁób@ò$½eW®`B†îòáPJÚŠŒ #–b!žÉÇh›„Œ;ÖjåÇp”ºœœ‡•ÂBgŽ]ÑHu&×_¾T“bÑxÃùÈÿ4`…·b` î5ð}×ljÖp�<˜T¢Ö»ø§^y©è+³aën¾ù¾ê&·¼ÙToº~²#Üll÷ecù¡hè©ðD—ÁÌ/(ZÖÙ,ýïØŠ)#j:|-ðý êíhpÔ’× ˜ž¦^»áVË•T¿ü—��2iU endstream endobj 276 0 obj 
Anant
Forum Commoner
Posts: 66
Joined: Wed Jul 14, 2010 11:46 am

Re: pdf to mysql

Post by Anant »

i placed

Code: Select all

<? ob_start(); ?>
on top of the page and <? ob_flush(); ?> at the bottom of the page. Surprisingly this works and gives me option to save the file.

But when i save it - it saves the name of the php file say - the page where am writing this script is test.php - so it will download the test.php only and not test.pdf.

How can i sort this out... ?

Thanks
Anant
Forum Commoner
Posts: 66
Joined: Wed Jul 14, 2010 11:46 am

Re: pdf to mysql

Post by Anant »

finally this works - i changed some code just for testing - so reverting back to original code with the <? ob_start(); ?> at the top and <? ob_flush(); ?> at the bottom.
Post Reply