We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51318
    • 9 Posts
    I have created a snippet to download a file (and in future I want to log into a database).
    The problem is the file is not downloaded but the content is display into the browser !!

    <?php
     $file = $_GET['file']; //Get the file from URL variable
     $file_path = "assets/files/".$file; //Set the file path w.r.t the download.php... It may be different for u
    
     if (file_exists($file_path) && ($file != '') && (substr($file,1,1)!='.') && (substr($file,1,1)!='/'))
     {
       $size = filesize($file_path);
       header("Content-Type: application/force-download; name=\"" . basename($file_path) . "\""); 	
     
       header("Content-Transfer-Encoding: binary");	
       header("Content-Length: $size"); 	
       header("Content-Disposition: attachment; filename=\"" . basename($file_path) . "\"");
       header("Expires: 0");
       header("Cache-Control: no-cache, must-revalidate");
       header("Pragma: no-cache");
       flush();	 
       readfile($file_path); 
    	
       echo "$file successfully downloaded<br>";
      return true;
     }
     else 
     {
        echo "Sorry, the file ".$file." does not exist!<br>";
       return false;
     }
    ?>
    


    and the page called "downloadingfile" is
     <p>Downloading file...</p>
     [[download]]
    


    The URL is http://www.mydomain.com/en/downloadingfile?file=xe/app_setup.exe

    Thank for you help
    Franck
      • 47401
      • 295 Posts
      the snippet has to return TRUE regardless of the if/else statement. i would use try and catch, so that you can post any errors to the modx reporting
        • 51318
        • 9 Posts
        Quote from: comp_nerd26 at Nov 06, 2015, 02:04 PM
        the snippet has to return TRUE regardless of the if/else statement. i would use try and catch, so that you can post any errors to the modx reporting
        My snippet returns TRUE after the readfile()... but I have solve this problem by using a external script and then I make a link directly to the php script.

        thanks.