Returning file with different name

Hi Folks,

I need help with a problem, and general internet searches turn up a sea of general file transfer scripts, but nothing that matches what I need.

Basically, I’m coding a simple engine to browse an old forum data set. It is full of files, including pictures, videos, PDF documents… all sorts. However, they are all stored in a format like… “files/45/93/1884763” which makes no sense (but I can understand it’s easier on the file system, especially when files were uploaded with character sets from different languages)

I have a table that contains the original file name for each file on disk, however, I need to write some PHP code that, when someone clicks on a link with “files/38/74/888374645” … will return, say, “ServiceManual.pdf” to the browser.

Can anyone help me on this please?

You would need a reference point. Is there a database involved that links the two?

Yes, I have the names.

In fact, I’m looking at this - php.net/manual/en/function.readfile.php - (sorry, less than 10 posts so I can’t post links)

…and if I can present the original file name here…

header('Content-Disposition: attachment; filename="'.basename($file).'"');

…then I should be golden. - I hope.

You want the used name, not the original name. To get the original name, if you have the link, is to look it up in the database.

Or, maybe I am not understanding the problem?

I think I might not have explained myself properly… but that script actually did the trick for me.

I can’t believe it… I battled this for days, then asked here and came across it! I should ask for help more often :slight_smile:

My big problem was being able to pick one file up off disk, but give it to the user with a different file name.

Thanks for taking the time to help me out. Much appreciated.

Sponsor our Newsletter | Privacy Policy | Terms of Service