user3296793
user3296793

Reputation: 270

PHP File Download Failure

I am trying to get my system to allow for a file to be downloaded when a hyperlink is clicked. The code below displays information about the file that is to be downloaded, this information is gathered from the database.

The code is presented within a table, the download should occur when the hyperlink is clicked. The variable "$file" contains the file path stored in the database. This, when clicked, should kick off the download.php file which should allow for the file to be downloaded. However a white page opens instead and nothing happens.

YourPurchases.php

if (count($reports) != 0) {    
    foreach ($reports as $report) {     
        $title = $report['reportID'];
        $rep_ID = $report['reportName'];
        $reportSubCat = $report['subcategoryName'];
        $uploadedBy = $report['userID'];
        $numPages = $report['pageTotal'];
        $file = $report['location']; // this is the file path stored in the database

        $purchasedreportstable .= '<table>
                                       <tr>
                                           <th>Report ID &nbsp</th>
                                           <th>Report Name &nbsp </th>
                                           <th>Report SubCategory &nbsp </th>
                                           <th>Uploaded By &nbsp </th>
                                           <th>Page Total &nbsp</th>
                                           <th>Download </th>
                                       </tr>
                                       <tr>
                                           <td>'.$title.'</td>
                                           <td>'.$rep_ID.'</td>
                                           <td>'.$reportSubCat.'</td>
                                           <td>'.$uploadedBy.'</td>
                                           <td>'.$numPages.'</td>
                                           <td> <a href="download.php file='.$file.'">Download</a>
                                       </tr>';
    }
    $purchasedreportstable .= '</table>';      
               ;                                            
    echo $purchasedreportstable;

download.php

<?php
    function downloadFile($file,$speed=1024){
        if (file_exists($file)) {
            if(is_dir($file)){return 'isdir';}
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename="'.basename($file).'"');
                header('Content-Transfer-Encoding: binary');
                header('Connection: Keep-Alive');
                header('Expires: 0');
                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                header('Pragma: public');
                header('Content-Length: '.sprintf("%u", filesize($file)));

                ob_clean();
                $handle = fopen($file, "rb");
                $chunksize=(sprintf("%u", filesize($file))/$speed);

                set_time_limit(0);
                while (!feof($handle)) {
                    echo fgets($handle, $chunksize);
                    flush();
                }
            fclose($handle);
            die;
        }else{
                return false;
        }
        return;
    }
?>

The file path within the database looks like reports/upload1_60b7d515219902288b.pdf

Any help would be greatly appreciated,

Thanks

Upvotes: 1

Views: 135

Answers (2)

scrowler
scrowler

Reputation: 24405

Judging by what you've posted, a blank screen with nothing happening is expected because your download.php code is a function that is never called.

Try putting this at the bottom:

downloadFile($_GET['file']);

You'll probably also want to do some checking for the return value in case it returns false:

if(!downloadFile($_GET['file']))
    echo 'Error encountered when trying to download file!';

Upvotes: 2

larsAnders
larsAnders

Reputation: 3813

In download.php, you need to get the filename from the URL first. You are generating a link to that page, but never snagging the file name from the URL or triggering the function. This code goes below the last bracket of the function.

if (isset($_GET['file'])) {
    $file = $_GET['file']
}

$result = downloadFile($file);

if ($result == FALSE) {
    echo "Sorry, file does not exist.";
}

Upvotes: 1

Related Questions