Large File Downloads in PHP and How to configure xsendfile to work?

160 Views Asked by At

I have been trying to get xsendfile to work for the purpose of allowing people to download larger files from my website. I currently have xsendfile downloading empty 0 bytes files. I'm not sure why it's doing that. When I use the readfile() method everything works fine, only with the limitation of file size.

This is the php

 function download_the_file_now($release_title) {
    $path = 'audio/';
    $name = $release_title.".zip";
    $file = $path.$name;


    if(file_exists($file)) {
        ob_end_clean();

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Content-Length: ' . filesize($file));
        header('Pragma: public');

       
        header("X-Sendfile:". realpath($file));
    }
 }

function get_album_tracks($promo_code, $release_title) {
    include_once "server/connect.php";

   if(isset($_SESSION['currentUser']) === true){ 

            $folder = mkdir('audio/'. $release_title); // create a new folder

            $query1= $conn->prepare('SELECT * FROM `promo_track_details` where `promo_code` = "' . $promo_code . '" ');
            $query1 -> execute();
            $result1 = $query1 -> fetchAll();
        
            foreach($result1 as $r1) {
                $filename = $r1['file_name'];
                $track_title = $r1['track_title'];


                    copy('audio/'.$filename, 'audio/'.$release_title.'/'.$filename); // copy the files we want into the new folder we created.
                    rename('audio/'.$release_title.'/'.$filename, 'audio/'.$release_title.'/'.$track_title.'.mp3'); // Rename the files in the folder (remove numbers)

                    // Get real path for our folder
                    $rootPath = realpath('audio/'.$release_title); // zip this folder

                    // Initialize archive object
                    $zip = new ZipArchive();
                    if($zip->open('audio/'.$release_title.'.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
                             // Create recursive directory iterator
                                $files = new RecursiveIteratorIterator(
                                    new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY
                                );

         
                                foreach ($files as $name => $file) {
                                    // Skip directories (they would be added automatically)
                                    if (!$file->isDir()) {
                                        // Get real and relative path for current file
                                    $filePath = $file->getRealPath();
                                    $relativePath = substr($filePath, strlen($rootPath) + 1);

                                    // Add current file to archive
                                    $zip->addFile($filePath, $relativePath);
                                }
                            }

                            // Zip archive will be created only after closing object
                            $zip->close();
                }

        }

        download_the_file_now($release_title);
}

}

get_album_tracks($_GET['file'], $_GET['title']);

Below is what I have in my httpd.conf file. I don't know how to configure it, and everywhere I look it is done in differently. I have tried many different syntax and none have worked for me.

 # Enable the xsendfile module
 <IfModule mod_xsendfile.so>
   <Files C:/xampp/htdocs/soundphiles-promo/audio>
     XSendFile on
   </Files>
 </IfModule>

Any help would be appreciated.

Note: I have updated the httpd.conf file a bit, but it still doesn't work.

 <IfModule mod_xsendfile.so>
    XSendFile on
    XSendFilePath "C:/xampp/htdocs/soundphiles-promo/audio"
 </IfModule>
0

There are 0 best solutions below