Imytnotbedumb
Imytnotbedumb

Reputation: 21

Downloading pdf file directly from database

Im trying to allow my user to download the file that they select in my system. The code i have used downloads something but it says failed forbidden. I haven't used directories to store the files but rather stored the file in a longblob data type directly in the database. If someone could please show me how to get it to download successfully I would appreciate it.

<tbody>
      <?php
                    require_once 'dbcon.php';
                    $query = $conn->query("SELECT fileID, filename, file, dateuploaded from empfiles WHERE eemail = '$_SESSION[login_employee]'");
                    $count = 1;
                    while($fetch = $query->fetch_array()){
            
                ?>
                <tr>
        
                    <td style="height:50px;width:100px"><?php echo $count++?></td>
                    <td style="height:50px;width:200px"><?php echo $fetch['filename']?></td>
          <td style="height:50px;width:100px"><?php echo $fetch['dateuploaded']?></td>
         
                    ?>
          </td>

          <td colspan="2">
          <td>         
                    echo"<a download href=\"file/{$row['file']}\">Download this file</a>";          
                </td>
                </tr>
                <?php
                    }
                ?>
            </tbody>

Upvotes: 0

Views: 322

Answers (1)

Quentin
Quentin

Reputation: 943134

The value of the href attribute needs to be a URL.

You can't just take a blob of PDF out of the database and coerce it into a string.

You need to either base64 encode it and then convert it to a data URL (noting that there is a good change this will result in a URL that is too long for the browser to accept).

or

Create a new webservice that extracts the PDF from the database and generates the download based on, for example, the row id. Then link to that webservice with the row id in the query string.

Upvotes: 1

Related Questions