Jump to content

[SOLVED] Please help URGENT - ZIP file download problem using PHP 5.2.5


kra1978

Recommended Posts

Hello Everyone,

 

I have a site where I can create .zip file using PHP programming on the server. It is working fine. On Server, PHP 5.2.3 is installed. It is working fine for last 6-7 months.

 

Yesterday I needed to switch the site from existing to server to new dedicated server. New server has PHP 5.2.5

 

Now, I can create zip file on the new server without any problem. But when I try to download it form the website, it starts downloading and suddenly shows that download is completed and when I try to unzip file then it is giving error message that file is corrupted/damaged. Yes, it is really corrupted because it's size (KB) is only 5-10% of actual file size.

 

I am using following code for downloading .zip file.

=====================================

$str_filename="";

$str_filepath="";

$str_ext="";

 

$str_filename=trim($rs_download_file->fields("photosetfilename"));

$str_filepath=trim($UPLOAD_ZIPFILE_PATH.$str_filename);

header("Content-Type:application/octet-stream");

header("Content-Disposition:attachment;filename=".$str_filename);

$fp=fopen($str_filepath,'rb');

header("Content-Length:".filesize($str_filepath));

fpassthru($fp);

exit();

=====================================

 

Please help me as soon as possible because I am in real trouble.

 

Thanks in advance,

KRA

Link to comment
Share on other sites

I checked all settings and they are same except one.

 

output_buffering

 

In old server, it is set output_buffering=4096 and in new server it is set output_buffering=no value

 

Please let me know whether this may be problem

 

Thanks again,

KRA

Link to comment
Share on other sites

Yes,

 

There is .zip file created successfully on the server and I also downloaded it using FTP and then unzip...it is working!!!

 

Dedicated Server Providers are saying that you need to change the script. I am not sure whether PHP 5.2.3  and PHP 5.2.5 can have different scripts to download .zip file from the website.

 

 

Please advice,

KRA

Link to comment
Share on other sites

theirs nothing in that code thats would be affect from the minor version upgrade.

 

humm try this

<?php
   $str_filename="";
   $str_filepath="";
   $str_ext="";
   
   $str_filename=trim($rs_download_file->fields("photosetfilename"));   
   $str_filepath=trim($UPLOAD_ZIPFILE_PATH.$str_filename);
   header("Content-Type:application/octet-stream");
   header("Content-Disposition:attachment;filename=".$str_filename);
//LITTLE TEST
if(!file_exists($str_filepath))
{
die("Bad file path~".$str_filepath);
}
//END
   $fp=fopen($str_filepath,'rb');
   header("Content-Length:".filesize($str_filepath));
   fpassthru($fp);
   exit();
?>

 

Link to comment
Share on other sites

The code you provided is not working. The file is downloaded only 152 KB each time instead of original 1.85 MB file size.

 

I found following code from the internet. Please advice whether it can be helpful or not.

 

==========================

$str_filename="";

$str_filepath="";

$str_ext="";

 

$str_filename=trim($rs_download_file->fields("photosetfilename"));

$str_filepath=trim($UPLOAD_ZIPFILE_PATH.$str_filename);

// $str_ext=getextension($str_filename);

header("Pragma: public"); // required

header("Expires: 0");

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

// header("Cache-Control: private",false); // required for certain browsers

header("Content-Type: application/force-download");

header("Content-Type: application/octet-stream", FALSE);

header("Content-Type: application/download", FALSE);

// header("Content-Type:application/octet-stream");

header("Content-Disposition:attachment;filename=".$str_filename);

header("Content-Transfer-Encoding: binary");

$fp=fopen($str_filepath,'rb');

header("Content-Length:".filesize($str_filepath));

fpassthru($fp);

exit();

 

==========================

Link to comment
Share on other sites

Okay well its a valid zip file, with the index of 2 files

photo_1.jpg

photo_2.jpg

but its truncated (only has the first part of the file)..

 

try this revised version

 

<?php
$str_filename="";
$str_filepath="";
$str_ext="";
   
$str_filename=trim($rs_download_file->fields("photosetfilename"));   
$str_filepath=trim($UPLOAD_ZIPFILE_PATH.$str_filename);

header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($str_filename));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($str_filepath));
readfile($str_filename);
exit();
#$fp=fopen($str_filepath,'rb');
#fpassthru($fp);
?>

Link to comment
Share on other sites

try this just posted to me from a friend that got the same setup as you........

<?php

$filename = $_GET['file'];

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');

// addition by Jorg Weske
$file_extension = strtolower(substr(strrchr($filename,"."),1));

if( $filename == "" ) 
{
  echo "<html><title>eLouai's Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>";
  exit;
} elseif ( ! file_exists( $filename ) ) 
{
  echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>";
  exit;
};
switch( $file_extension )
{
  case "pdf": $ctype="application/pdf"; break;
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "doc": $ctype="application/msword"; break;
  case "xls": $ctype="application/vnd.ms-excel"; break;
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();

?>

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.