php/mysql: Open file by name reference

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

php/mysql: Open file by name reference

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php/mysql: Open file by name reference

Post by requinix »

Why aren't you storing the two names in separate fields?
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: php/mysql: Open file by name reference

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: php/mysql: Open file by name reference

Post 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.
Post Reply