php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29807 forces .php extension in the download dialog
Submitted: 2004-08-24 06:58 UTC Modified: 2004-08-24 07:02 UTC
From: diptimca at yahoo dot com Assigned:
Status: Not a bug Package: HTTP related
PHP Version: 4.3.8 OS: linux red had 9
Private report: No CVE-ID: None
 [2004-08-24 06:58 UTC] diptimca at yahoo dot com
Description:
------------
hi,
 i tried running the following code for downloading files.

<?php 
// downloading a file use http://somewhere.com/download.php/?filename=name of file 

$filename = $_GET['filename']; 

if(!$filename){ echo "ERROR: No filename specified. Please try again."; } 
else { 
     
// fix for IE catching or PHP bug issue 
header("Pragma: public"); 
header("Expires: 0"); // set expiration time 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
// browser must download file from server instead of cache 

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

// use the Content-Disposition header to supply a recommended filename and 
// force the browser to display the save dialog. 
header("Content-Disposition: attachment; filename=".basename($filename).";"); 

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

$root_path = "root path to image"; 

$myfile = $root_path . $filename; 

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

readfile("$myfile"); 
exit(); 
} 

?> 

It works well, but in the download dialog, it shows the filename of the php file in which this code is written.


Even though i specify a txt file in myfile.
please help me for the same . its urgent

Reproduce code:
---------------
<?php 
// downloading a file use http://somewhere.com/download.php/?filename=name of file 

$filename = $_GET['filename']; // a txt file or doc file 

if(!$filename){ echo "ERROR: No filename specified. Please try again."; } 
else { 
     
// fix for IE catching or PHP bug issue 
header("Pragma: public"); 
header("Expires: 0"); // set expiration time 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
// browser must download file from server instead of cache 

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

// use the Content-Disposition header to supply a recommended filename and 
// force the browser to display the save dialog. 
header("Content-Disposition: attachment; filename=".basename($filename).";"); 

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

$root_path = "root path to image"; 

$myfile = $root_path . $filename; 

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

readfile("$myfile"); 
exit(); 
} 

?> 

Expected result:
----------------
in the download dialog, the default file name should be the one got form get method i.e "hello.txt"



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-24 07:02 UTC] tony2001@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.

.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Jul 31 20:01:24 2021 UTC