Welcome to FindNerd. Today we are going to discuss the concept to download a file securely in php. We all knows, we can not share a adjust file location to end user.
We can set the header for file name, type and size. Please check the below code
$filename = urldecode($_GET['f']);
$filepath = 'wp-content/package/';
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
@readfile($filepath.$filename);
In above example we will build url like this. http://www.demo.com/package?f=lib.zip
We passed the name of the file as the parameter and got it in our page to process. we set the header for different requirements like cache-Control,content type and
content-transfer etc. In end we read the file to download.
0 Comment(s)