phpreadfiledwg

readfile() returns empty file while trying to force download


I'm trying to force download of .dwg files with the following code:

$filename = $_GET['f'];
$upload_path = wp_upload_dir();
$src = $upload_path['basedir'] . '/' . $filename;

header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header("Content-disposition: attachment; filename=\"{$filename}\"");
header("Content-Type: application/octet-stream");
header('Content-Transfer-Encoding: binary');
readfile($src);
die();

The path to the file is correct, but it only returns a file that is 6 byte and that is not possible to open. It should be 754 bytes. Anyone who can help me out here?

//Example file
$src = "/var/www/wp/wp-content/uploads/Glasparti-modell-Silence.dwg";

Solution

  • I solved this by the following in .htaccess instead:

    <FilesMatch "\.(dwg)$" >
        ForceType application/octet-stream
        Header add Content-Disposition "attachment"
    </FilesMatch>