Retrieving Multiple attachments by their path from Database

I have a simple html form, that saves the email message and multiple attachments path. I am saving the files on the server and their path to the database filed.

Now how can i retrive the files from its path, and then show them to user, when click on Download, I am using the following code for getting the file, but this is not working

[php]$query = "
SELECT type, name, size, file1,file2,file3,file4,file5
FROM upload WHERE id = {$id}";
$result = $dbLink->query($query);

if($result) {
     if($result->num_rows == 1) {
         $row = mysqli_fetch_assoc($result);
        header("Content-Type: ". $row['type']);
        header("Content-Length: ". $row['size']);
        header("Content-Disposition: attachment; filename=". $row['name']);

        $path = $row['file1']; 
        $dir = opendir($path);
         echo $dir;
      while ($dir && ($file = readdir($dir)) !== false) {
         echo $file;
            }[/php]

The echoed file doesnot contain any data.

This i am doing now for only one file, whose path is at “file1”. Similarly i have 5 attachments path, and i have to retrive them all in this code. Please how can i do it.

Well, do you mean you want the user to select a file to download?

If so, it is MUCH easier to just display the link to the file instead of a button calling code.
Once the file is on the server, you can display it to the user in any form you wish.
Just mark around that with an anchor and HREF. Then, when the user clicks on it,
it downloads or opens the file. Did that make sense?

So, in your code, you show headers. You can NOT send those headers to the browser if you have already displayed a page or even HTML headers. I think you can send them if it is coming from a PHP page without any HTML in it. But, I have found that tricky! Much easier to just show the message and have a button for each file which is just a hidden HREF. Works good.

Something like this displayed in the HTML would work:

(Just a rough layout…) This “wraps” the “file” button inside the “link” to the actual file. In the code that displays this page, it would create the link from the filenames that were saved in the database…

Not sure if that is what you are looking for. Hope it helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service