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

Serving a different filename than the actual file

Status
Not open for further replies.

jimoblak

Instructor
Oct 23, 2001
3,620
US
I may be off but I believe there was a PHP function that would take a file on your server and dish it out as another filename. Am I too off? What is this function (or what method could be used to do this on Apache/Linux?)

Example:
a file on the server as
'user/htdocs/pdf/01.pdf'

would be called/served as
'

'01.pdf' is changed to 'Monkey_Catalog.pdf'


I'm looking to convert machine-authored filenames of machine-authored PDFs to something readable by the end user when they save/download.

- - picklefish - -
Why is everyone in this forum responding to me as picklefish?
 
You could do it using mod_rewrite and mod_proxy on Apache, and using a PHP script that examines $_SERVER['REQUEST_URI'] to see what file was requested.

On my test system, my web site is configured with:

Code:
RewriteEngine on
RewriteRule ^/phony_files(.*) /phony_script.php

phony_script.php reads:
Code:
<?php
$file_map = array
(
	'foo.pdf' => '123.pdf',
	'bar.pdf' => '456.pdf',
	'baz.pdf' => '789.pdf'
);

$requested_filename = preg_replace ('/\/phony_files\//', '', $_SERVER['REQUEST_URI']);

if (isset($file_map[$requested_filename]))
{
	print '/real_directory/' .$file_map[$requested_filename];
}
else
{
	header ('HTTP/1.0 404 Not Found');
}
?>

Of course, your script will probably want to actually stream the file out with the appropriate "Content-Type" and "Content-disposition" headers, and will probably have a machine-readable to human-readable filename conversion that's more significant than mine.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Sorry, I misspoke. You won't need mod_proxy, as you're not proxying the request internally, just redirecting the request to the script. In fact, if you use mod_proxy, Apache will rewrite $_SERVER['REQUEST_URI'] to the name of your script and the requested filenames will be lost.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top