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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

renaming a file for download

Status
Not open for further replies.

sera

Technical User
Jun 29, 2000
360
US
This is on LAMP system.
When files are uploaded i cat a timestamp on them because i can have many files with the same filename (revisions). when i goto download the file it looks like 1108591785filename.ext
i would like to strip off the timestamp when someone downloads it. but leave the timestamp on the filename in the system.

i'm looking for suggestions.

Thanks

Sera
I often believe that...
My computer is possessed!
 
the filename that is presented to the user in the download is a function of what is sent in the header. just parse the filename to remove the timestamp before passing the information to the header.
 
i see but am unclear as to how to do that.
all i currently do is <a href="path/to/filename.ext">Download Me</a>

how would i interact with the header being sent?


Sera
I often believe that...
My computer is possessed!
 
instead point the href to a php file and add a parameter with the filename you want to download.

then serve the file using php.

sample code below:
Code:
<a href "fileserver.php?filename=test.doc">Click here to download</a>

Code:
<?
//sample script for fileserver.php
if (isset($_GET['filename']))
{
	$filename = addslashes($_GET['filename']);
	$path = ""; //path to file download directory
	if (!file_exists($path.$filename))
	{die("file not found");}
	else
	{
		$fd = fopen($path.$filename,"rb");
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");
		header("Content-type: application/octet-stream");
		header("Content-length: " .filesize ($path.$filename));
		fpassthru($fd);
	}
} else {
	die ("no filename variable");
}
?>
 
Thanks for the code. I ended up fixing it before i saw your final responce. For future people with this problem here is what i ended up doing...

Code:
<form name='form2' action='filedload.php' method='post'>
<input type='submit' value='Download $trimmedfilename'>
<input name='filename' type='hidden' value=$ogfilename>

Code:
<?
$ogfilename = $_REQUEST['filename']; //ex 1108592457Logo.jpg
$trimed = substr($ogfilename, 10);  

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

readfile('/path/to/file/'. $ogfilename);

?>

Worked great

Sera
 
$path='/tmp/file';
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '"');
@readfile($path);
exit();


Note: Don't forget the "exit()" statement or PHP continue to execute your file.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top