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
Share on other sites

This is apache server running on a linux box.

 

This code is only a small portion of a larger project.  I am planning on adding much more hidden fields to post data.  I just wanted to get this portion working first.

 

thanks for the reply.

Link to comment
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.

Link to comment
Share on other sites

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.

 

Link to comment
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.

 

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.

Link to comment
Share on other sites

Wow! that works.  This is very strange because I have about 200 files that are .htm and they all work with php html and javascript but this one only wants to work with a .php extension.  Strange???  Any guesses as to why?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

Okay guys, I did some more testing and it looks like _blank, _parent, _top works for the target, but when I try to target the iframe that is where it freaks out.  I will try to do something else so I don't waste your time.

 

thanks for the help.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.