0

There is a file on server : files/1 Its name and extension is stored in a database. (For example: a.jpg) Is it possible to download that file as a.jpg not as 1? If yes, how?

In other words:

<a href='/files/1'>file</a>

should download file 1's content with the name a.jpg

2 Answers2

1

If I interpreted your question well, then:

HTaccess:

RewriteEngine On
RewriteRule ^([^/]*)\$ /getfile.php?fn=$1 [L]

Then getfile.php reads which file corresponds to $_GET["fn"] and outputs that.

axiomer
  • 2,088
  • 1
  • 17
  • 26
  • So what should getfile.php contain? –  Feb 29 '12 at 11:56
  • It should have a database select where the pseudo file name is $_GET["fn"] so it gets the real one and then read the file and wcho it. – axiomer Feb 29 '12 at 12:13
  • I have updated my answer. That way I have tested but it does not work. –  Feb 29 '12 at 12:25
  • Noo. `echo file_get_contents($result);` also the link isn't getfile?fn=1 but Pseudo-filename, which redirects to getfile.php via htaccess – axiomer Feb 29 '12 at 12:28
1

example from php.net manual:

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>
Vytautas
  • 3,509
  • 1
  • 27
  • 43
  • Thank you, this works fine, I have updated it with the example variables if you don't mind. –  Feb 29 '12 at 13:03