Hi,

I am trying to upload a pdf document into mySQL as a blob (using php). I have got the file going into the database no problem:

	  if(!empty($HTTP_POST_FILES["pdf"]['name']))
      {
         $pdf_name = $HTTP_POST_FILES['pdf']['name'];
         $pdf_size = $HTTP_POST_FILES['pdf']['size'];
         $pdf_type = $HTTP_POST_FILES['pdf']['type'];
         $pdf_tmp  = $HTTP_POST_FILES['pdf']['tmp_name'];   

     $file = fopen($pdf_tmp, "r");
     if(!$file)
     {
        $pdfError = $pdfError."file did not work... ";
		$contentOk=false;
     }	// end of check file works
	 else
	 {
	    // $pdfError = $pdfError."file did work... "; 
	    $pdf = AddSlashes(fread($file, $pdf_size)); 
        if(!$pdf)
        {
           $pdfError = $pdfError."fread did not work... ";
		   $contentOk=false;
        } // end of check pdf works
        else
        {
           $pdfOk = true;
        } // end of pdf ok
	 } // end of file ok
  } //end of organise pdf details

//Then go onto insert into the database table//

This all works as expected and inserts the binary data into the database.

The problem I am having is trying to get it back out again for display.

I have tried using:

$getPdfQuery = "select pdf, format from company where companyid ='{$id}'";
   $getPdfResult = $connection->query($getPdfQuery);
   while($getPdfRow = $getPdfResult->fetchRow(DB_FETCHMODE_ASSOC))
   {
      $pdf = $getPdfRow["pdf"];
      //$format = $getPdfRow["format"];
   }

   header ("Content-type: application/pdf"); 
   echo $pdf;

However, when I run this code, I get an Acrobat warning:
"The file is damaged and could not be repaired"

Any ideas?

    Hi,
    I can only hint to write the pdf in a file. The compare it with original version and look what is wrong.

    I think you need to do some stripslashes(). And check if you has magic_quotes active, else you can remove addslashes()...

    Good luck.

      Thanks bad67,

      I have done as you suggest and stripped the slashes. Instead of trying to output the pdf I have now written to a file.

      id = $_GET["companyId"];
         $getPdfQuery = "select pdf, format from company where companyid ='{$id}'";
         $getPdfResult = $connection->query($getPdfQuery);
         while($getPdfRow = $getPdfResult->fetchRow(DB_FETCHMODE_ASSOC))
         {
            $Pdf = stripslashes($getPdfRow["logo"]);
            $format = $getPdfRow["format"];
         }
      
         $filename = "test.pdf";
         if (!$file = fopen($filename,"w")) {
         echo "Cannot open file ($filename)";
         exit;
         }
         if (!fwrite($file,$picture)) {
           echo "Cannot write to file ($filename)";
           exit;
         }
         echo"File written OK";
         fclose($file);
      
      

      WHen you open up the two pdf files in a text editor (ie: before it was uploaded and then the resultant file that is written after it is extracted from the database, the one that comes out of the adatabase is approximately half the size of the original. I suspect that the problem lies within the waythat I am uploading the file into the database. No matter what size the file I upload is, the blob field in the database is 64Kb?

      Is there a setting in php.ini or mysql that limits the size of the data that can be held in the blob?

        Think I just found the answer to my own question.

        Blob data types can only hold 216 bytes (64 kb) of data. Anything beyond this is truncated.

        By changing to a medium blob, I should be able to store 224 bytes (16380 kb)

        I haven't tried it yet (away from my computer) but I reckon it ought to work....

        Always the same old problems eh....FTFM.

        CHeers

          2 months later
          Write a Reply...