Page 1 of 1

php/mysql: Open file by name reference

Posted: Sat Sep 29, 2012 7:02 pm
by cjkeane
Hi everyone,

in a mysql database, filenames are stored in a table like so: ODm0QCVFEsAOHriuFvfS.pdf[test.pdf]
each file represents the actual file name saved and in brackets the filename to display on the page.

my php script then lists all the filenames on a page and when a file is clicked on, it opens the file with a save as prompt with the actual filename (ODm0QCVFEsAOHriuFvfS.pdf). Is there a way to modify my code to display the filename in the [], for example test.pdf in the save as dialogue box?

Code: Select all

<?php // $attachments = "ODm0QCVFEsAOHriuFvfS.pdf[test.pdf],XHprKsLP0J1EddvUGgpa.doc[test.doc]";
preg_match_all('/([^\[]+)\[([^\]]+)\],?/', $attachments, $matches, PREG_SET_ORDER);
$attachments = array();
foreach ($matches as $file) {
   $attachments[$file[1]] = $file[2];
}

foreach ($attachments as $file => $filename) {
    echo '- <a href="mail/attachments/' . htmlspecialchars($file, ENT_QUOTES) . '"target="_new" name="' . htmlspecialchars($filename, ENT_QUOTES) . '">' . htmlspecialchars($filename, ENT_QUOTES) . '</a><br />' . PHP_EOL;
} ?>
thanks.

Re: php/mysql: Open file by name reference

Posted: Sat Sep 29, 2012 8:39 pm
by requinix
Why aren't you storing the two names in separate fields?

Re: php/mysql: Open file by name reference

Posted: Sun Sep 30, 2012 12:00 pm
by cjkeane
i found it easier to have both parts ODm0QCVFEsAOHriuFvfS.pdf (the encrypted filename) and test.pdf (the filename given to the email attachment by the uploader) in the same field.

This is why I had to write that php code. The script works fine, except when you right click on any attachment filename displayed on the page, the filename displayed in the save as window appears as ODm0QCVFEsAOHriuFvfS.pdf; but i want it to appear as the reference filename such as test.pdf. How would you suggest I resolve this issue? Thanks.

Re: php/mysql: Open file by name reference

Posted: Sun Sep 30, 2012 3:08 pm
by McInfo
You will need to write a PHP script to serve the file instead of having the HTTP server do it directly. Use the readfile() function to output the file data. The script will need to output the correct HTTP headers to mimic those sent when the file is served directly. Use Wireshark or Tamper Data for Firefox to see the headers. Of particular importance are the Content-Type, Content-Length, and Content-Disposition headers. Pay attention to cache control headers if you want to minimize throughput.