PHP problem handling large PDF files

Problem downloading large PDF files. The download starts but does not complete and only downloads partial fle(Ex: 543KB file only downloads 512KB) and error message received “the file is damaged and cannot be repaired”. Smaller files download with no issues. Code attached.

Thank you,

Well, I normally do not save PDF files inside a database. It uses extra overhead and runs into many issues. But, let’s think of your code as-is. You load a resource object rsd, but is it not a normal variable. Next, you ENCODE it part of it’s data into another variable. Next, you DECODE that same data back into what it was to start with and save in another variable. So much work, tons of wasted server processing. Makes little sense to me to handle simple PDF files that way.

Anyway, my comments aside, my assumption is that you overflowed the allotted memory allowed. It might that you simply need to add some extra ram to the process. Not physical ram, but, code wise to allow for more than the default amounts allowed. Max allowed can be 80Megs, 128meg, 256meg, etc.
This depends on how your server is set up and how PHP has be set up. After some minor research,
it appears that the most common default is 128megs per running script. It will alter if using multiple
threads. You can set it like this at the top of the page. ini_set(‘memory_limit’, ‘512MB’);
But, one other issue is that you can drop the extra ENCODE/DECODE lines and just use the already
encoded versions. That might solve it on it’s own. (Variables are held in memory!)
Good luck!

Thank you for your suggestions. I removed the ENCODE/DECODE lines and added memory limit and set_time_limit. The actual pdf file sizes are 1093KB and 779KN but only 512KB is downloaded.
Any suggestions! Thanks.


$check_sql = "SELECT

SAS_REP_PDF_STATEMENTS.rep_id = ‘[rep_id]’
SAS_REP_PDF_STATEMENTS.comm_period = ‘[comm_period]’";

sc_lookup(rsd, $check_sql);

$statement_pdf = {rsd[0][0]};
//$pdf_data = base64_encode($statement_pdf);
//$data = base64_decode($pdf_data);
header(‘Content-Type: application/pdf’);
header(‘Content-Disposition:attachment;filename=statement.pdf;’ );
//echo $data;
echo $statement_pdf;

Well, again, most programmers would not save PDF files inside a database. PHP has extensive file handling functions and therefore you are making it more complicated for yourself and the server.
Also, the rsd[0][0] is already a variable, just use that instead. Next, you can not set content disposition
the way you did. You need to do it like this:
header('Content-Disposition:attachment; filename=“statement.pdf” '); *** Note the double quotes around
the filename! ***
Lastly, it might be your MySQL settings. In your PHP ini files, you will have a section that is for the DB.
In that area, you need to check for " max_allowed_packet ". You should set that to something like =16M.
To review your server’s settings, create a PHP file with just this one line: phpinfo(); And run it on your
server. It will display all of the currently live values. Look down the list for your database section and see
what it is currently set to.

Hope one of these helps… Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service