1

When i try to extract the zip folder that i downloaded from the server forcefully, i get the message, 'no files to extract'. But when i download the zip folder directly from the server, i am able to extract it properly. Here is the code that i used to download forcefully.

$file = 'absolute/path/to/the/zip/folder';
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".basename($file));
header( "Content-Description: File Transfer");
@readfile($file); 

Does anyone know why this is happening? Any help would be appreciated. Thanks in advance

1
  • Suppressing errors on the readfile probably isn't wise. Feb 15, 2011 at 9:04

3 Answers 3

1

I cannot answer the question, but here are a few ideas to try out:

  • Try a different mimetype, such as the generic application/octet-stream.
  • Attach Fiddler (or any other HTTP debugger) and look at the transfer. What do you see? Is the file being downloaded? Maybe there is an invalid Content-Length header for some reason?
  • As noted by @middaparka in comments, don't suppress errors from readfile().
2
  • I tried with 'octet-stream', it didn't help to find any solution. As @middaprka said, i removed the suppression. Neither helped. Thanks for your answer.
    – ksun
    Feb 15, 2011 at 9:56
  • @ksun - what do you see in Fiddler? Maybe you can send us the saved "session" from it?
    – Vilx-
    Feb 15, 2011 at 11:02
0

It's tricky to tell if it's due to the headers you're using or a server issue. However, I generally use the following without any problems:

header("Cache-Control: private");
header("Content-Type: application/stream");
header("Content-Length: ".filesize($file));
header("Content-Disposition: attachment; filename=".basename($file));

readfile($file);
exit();

As such, you might want to try the above and see if that makes any difference. (At a guess, if your connection is slow the fact that you're not supplying a Content-Length header might not be helping the situation.)

3
  • I tried your code, but the problem wasn't solved. I think its not a server issue, since i am able to download the zip directly from the server. Thank you for your answer.
    – ksun
    Feb 15, 2011 at 9:53
  • @ksun My code works fine on a variety of servers, hence I have to wonder if it's a server issue. Have you checked the headers you're getting back from the response as @Vilx- suggested? (You can use Firebug, etc. to do this.) Feb 15, 2011 at 9:56
  • I shall check with fiddler and let you know
    – ksun
    Feb 15, 2011 at 10:04
0

Thank you everybody for your support. I found the solution. I had used mkdir function before headers were sent. It seems this was generating some error, so error was outputted before the headers and thereby causing the problem. When i suppressed the errors using '@' operator, the problem got solved. :). Earlier, i got the following message from fiddler: "Fiddler has detected a protocol violation in session #4. The Server did not return properly formatted HTTP Headers. Maybe missing altogether (e.g. HTTP/0.9), maybe only \r\r instead of \r\n\r\n?"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.