PHP Binary Read / Write Synchronisity Problem - HELP!!!

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
transio
Forum Newbie
Posts: 4
Joined: Wed Sep 25, 2002 10:06 am
Location: miami

PHP Binary Read / Write Synchronisity Problem - HELP!!!

Post by transio »

Hi everyone!

I'm having a serious problem with a PHP + Java application I'm creating. The app is almost entirely created in PHP, but we have a single Java Servlet that is generating PDF files dynamically using FOP.

In order to ensure security, we're reading the PDF contents from the JSP then writing them to the browser directly from a PHP script. We decided to implement it this way for several reasons, which I can't really get in to.

The script was working fine for a few months, but then it stopped working as the server started getting more traffic and the app slowed down. What started happening is that my PHP would output a PDF file with 0 bytes of data - empty page. Seemed to me that feof() might be returning true straight away?

I added a sleep(8) and that fixed it for a week. Then it stopped working again. I upped it to sleep(30). No luck.

Any ideas why this is happening? I was under the impression that fopen() and fread() were synchronous?

Code: Select all

 
<?php
/**
  * I eliminated unnecessary PHP code here...
  */
 
// Sample path for your understanding of what we're doing
$pdfServletPath = "http://localhost:8080/pdf_generator.jsp?data=report1.xml&fo=report.xsl&username=Steven";
 
// Generate and read the contents of the PDF file from the local FO application as binary data
$file = fopen($pdfServletPath , "rb");
 
// Sleep until the PDF generates - This was added after the bugs started happening
sleep(30);
 
// Append the contents of the PDF Servlet to a string / buffer
while (!feof($file)) {
    $fileBuffer .= fread($file, 4096);
}
 
// Close the servlet
fclose($file);
 
// Get the file size
$fileSize = strlen($fileBuffer);
 
// Set the PDF header info
header("Content-type: application/pdf");
header("Content-Length: {$fileSize}");
header("Content-Disposition: inline; filename={$pdfFileName}");
 
// Output the PDF to the browser
print $fileBuffer;
?>
 

Thanks in advance for any help you can offer!!!

- Steve
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: PHP Binary Read / Write Synchronisity Problem - HELP!!!

Post by Eran »

I am simply improving your hack, so you might want to consider attacking the JSP problem instead... here it is (*untested):

Replace:

Code: Select all

 
sleep(30);
 
With:

Code: Select all

 
$filebuffer = '';
while($fileBuffer = fread($file, 4096) != '') {
      sleep(1);
}
 
transio
Forum Newbie
Posts: 4
Joined: Wed Sep 25, 2002 10:06 am
Location: miami

Re: PHP Binary Read / Write Synchronisity Problem - HELP!!!

Post by transio »

Thanks...

I thought about doing that. However, the PDF files are upwards of a megabyte each. Reading 4k per second will amount to a 4+ minute load time.

I was hoping someone might see something I'm doing wrong in the PHP?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: PHP Binary Read / Write Synchronisity Problem - HELP!!!

Post by Eran »

This does not read 4k per second.. it simply sleeps until the file size is no longer 0, which I thought was your problem
transio
Forum Newbie
Posts: 4
Joined: Wed Sep 25, 2002 10:06 am
Location: miami

Re: PHP Binary Read / Write Synchronisity Problem - HELP!!!

Post by transio »

I see. I'm going to try it out. Thanks!
Post Reply