• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Why is this not working?

Ive got this simple code:

$id="info.gif";
$file = '/home/server/files/76/'.$id;

header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Length: ' . filesize($filename));
header('Content-Disposition: attachment; filename=' . basename($file));
readfile($file);


It seems to find the file fine, but it prints out the entire contents.

If I put

$file = '/home/server/files/76/info.gif";

it works fine.

Anyone?
0
fox_statton
Asked:
fox_statton
1 Solution
 
BatalfCommented:
Both of them should have worked, since it's actually the exact same thing.

This is the code I have used for this:
   
$id="info.gif";
$file = "/home/server/files/76/".$id;

$mimetype = "image/gif"; // You should have a switch() block which sets the mimetype for different extensions.
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");    
header("Content-Type: ".$mimetype);
header("Content-Length: ".filesize($filename));
header("Content-Disposition: attachment; filename=\"$id\"");

Batalf
0
 
prsupriyaCommented:
try this....

$id="info.gif";
$file = '/home/server/files/76/'.$id;

header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header('Content-Type: image/gif');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="'. basename($file)."\"");
readfile($file);
exit();
0
 
designbaiCommented:
<?
$id="info.gif";
$file = '/home/server/files/76/'.$id;
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");
header("Content-Disposition: attachment; filename=".basename($file)." )";
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
readfile("$filename");
?>

hope this helps.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now