Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a php function to download a file from the file system

Status
Not open for further replies.

amir4lamp

Programmer
Apr 28, 2007
8
CA
Is there a php function to download a file from the file system, NOT simply by using anchor. Because i have to first copy the file into a seperate folder then rename it and then download it.

Thanks
 
I have to download the file thats the final goal.

The only reason why i have to copy and rename the file, is bcz the file name is encrypted, which does not make any sense to the end-user.

and also bcz i want to keep the original encrypted copy of the file.

and then will delete all the files at the end of the month using cronjobs etc.
 
Hi

Do you mean that you have to download it, or just to server it to the visitor who downloads it ?

For the later case this could be a sample solution :
Code:
<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$_GET["file"]);
readfile("secret_download_dir/".$_GET["file"]);
?>
And offer a link to an URL like the below to the visitor :


Feherke.
 
remember to enclose the filename in double quotes in the header

Code:
header('Content-Disposition: attachment; filename="FILENAME"');
 
You can use the header jpadie suggested to have the save as dialog automatically populated with a filename of your choice:

PHP online manual said:
If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the » Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.

Using jpadie's suggestion:

Code:
<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename="[red]suggestedfilename.ext[/red]");
readfile("secret_download_dir/[red]encryptedfilename.ext[/red]");
?>



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
the quoting is slightly wrong in vacunita's example
Code:
header("Content-type: application/octet-stream");
header([red]'[/red]Content-Disposition: attachment; filename="suggestedfilename.ext"[red]'[/red]);
readfile("secret_download_dir/encryptedfilename.ext");

or you can use double quotes around the outside and escaped double quotes inside.
 
Sorry forgot to fix the quotes. jpadie is right either single quotes surrounding double quotes or viceversa.


Sorry, my bad.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top