If you want to force download, you can use something like the following:
<?php // Fetch the file info. $filePath = '/path/to/file/on/disk.jpg'; if(file_exists($filePath)) { $fileName = basename($filePath); $fileSize = filesize($filePath); // Output headers. header("Cache-Control: private"); header("Content-Type: application/stream"); header("Content-Length: ".$fileSize); header("Content-Disposition: attachment; filename=".$fileName); // Output file. readfile ($filePath); exit(); } else { die('The provided file path is not valid.'); } ?>
If you just link to this script using a regular link, the file will be downloaded.
By the way, the code snippet above should be executed at the beginning of the page (before any headers or HTML output appear). Also take care if you decide to create a function based on this for downloading arbitrary files - you need to make sure that you prevent directory traversal ( realpath , this is convenient, only allows downloading from a specific area, etc. if you accept input from $ _GET or $ _POST.
John parker
source share