Jump to content

PHP file download doesn't work


karozans

Recommended Posts

Hi everyone, can someone please look at my code and tell me why my download isn't working. I see no errors in IE7 or FF2 or FF3. I have apache running on linux.

 

Basically I have a form.

 

<body dir="LTR" onload="javascript:doWork();" id="main">
  <form action="sl_capture_ivt_state_view.htm" enctype="multipart/form-data" method="POST" name="form_out1" target="download">
  </form>
  <iframe title="download" frameborder="no" scrolling="no" marginheight="0" marginwidth="0" name="download">
  </iframe>
</body>

 

Here is the javascript...

 

function doWork()
{
  document.form_out1.submit();
}

 

then here is the code from sl_capture_ivt_state_view.htm

 

<?
      $fileName = "/tmp/IVT.tar";
      $sumFile = "/home/user/IVTStatus.txt";
      $detFile = "/home/user/IVTLog.txt";

      $script = "tar -cf $fileName $sumFile $detFile";
      system($script);
  
      if (file_exists($fileName))
      {
      $size = filesize($fileName);
          header("Pragma: ");
          header("Cache-Control: ");
          header("Content-Type: application/bin");
          header("Content-Length: $size");
          header('Content-Disposition: attachment; filename="' . basename($fileName) . '"');
          header("Content-Location: $fileName");
          header("Cache-Control: private");
          $handle = fopen($fileName, "rb");
          fpassthru($handle);
          unlink($fileName);
      }
?>

 

Again like I said, I do not get any browser errors the code simply runs through and stops. I do notice one error in the apache error logs but I am not sure that they have to do with this problem.

 

tar: Removing leading `/' from member names

sh: /usr/sbin/sendmail: not found

 

Can anyone help?

 

thanks

 

Link to comment
https://forums.phpfreaks.com/topic/112826-php-file-download-doesnt-work/
Share on other sites

No it doesn't stop at a blank page, it actually behaves like it should but I get no download prompt and no errors.  I should mention this.  If I bring up the error log for apache at the same time I bring up the page.  Then I hit refresh the page so that the doWork() function runs, if I refresh the error log at the same time as the page is refreshing, once in a while the download prompt will open and work properly.  It is like the server is going to fast for the client or something.

Okay I found something interesting here.  If I have this code.

 

<?
   if ($_SERVER['REQUEST_METHOD'] == "POST")
   {
      $fileName = "/tmp/IVT.tar";
      $sumFile = "/home/user/IVTStatus.txt";
      $detFile = "/home/user/IVTLog.txt";

      $script = "tar -cf $fileName $sumFile $detFile";
      system($script);
      
      if (file_exists($fileName))
      {
          $size = filesize($fileName);
          header("Pragma: ");
          header("Cache-Control: ");
          header("Content-Type: application/bin");
          header("Content-Length: $size");
          header('Content-Disposition: attachment; filename="' . basename($fileName) . '"');
          header("Content-Location: $fileName");
          header("Cache-Control: private");
          $handle = fopen($fileName, "rb");
          fpassthru($handle);
          unlink($fileName);
      }  
}     
?>

 

If I navigate directly to the page.  myserver/sl_capture_ivt_state_view.htm.  Nothing happens as it should because of the...

 

 if ($_SERVER['REQUEST_METHOD'] == "POST")

 

but if I remove the if statement and navigate directly to the page the download works beautifully.

 

So if you look at my previous code I am submitting the form as a POST.  So I wonder why I am not getting through the if statement.

 

No it doesn't stop at a blank page, it actually behaves like it should but I get no download prompt and no errors.

 

If it doesn't prompt but it goes to the page, wouldn't it just be a blank page? I don't understand how it can go to the page like it should and not be blank, since you have no output on that page.

 

try putting echo "test"; right underneath if ($_SERVER['REQUEST_METHOD'] == "POST"){ and see if it does anything.

DOH!!!  Okay I swear it worked for 3 times on IE.  I then went in and cleared my browser cache and now it doesn't work.  I have access to about 14 computers and all of them are experiencing the same behavior.  So I don't believe that my browser is having issues.

 

Hi Lemmin, I just got it to work.  I did what you said at gave the iframe an id.  Here is what else I had going on.  I had two iframe, and two hidden forms.  In the java code I had...

 

document.form1.submit;
document.form2.submit;

 

form1 was going to iframe1 and form2 was going to iframe2

 

Somehow the php was continueing on before it had a chance to display the dialog box for download.

 

so to fix it I did this.

 

document.form1.action = "iframe1";
document.form1.submit;
document.form2.actoin = "iframe2";
document.form2.submit;

 

Again thanks for the help, you set me on the right track to find out what was going on.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.