PHP Download File

In some cases, we really do not want to show the URL of the file to be downloaded. I can be related to the file/directory security or privacy of the file. This tutorial will help to hide the real file URL by just connecting a PHP file with an ID of the row then the push-download will occur.

INSTRUCTION

MySQL Database

CREATE TABLE IF NOT EXISTS ` tblsong` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
  `file_name` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
  `category` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `n_down` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

This tutorial works with database containing the required data like id, filename, category, etc. Let’s suppose that we have a song data table as shown above.

PHP File (download.php)

$song_id = $_GET["id"];	
$downloadNum = 0;$cateName = '';$filename='';
$song_qry = exec_query_utf8("select * from tblsong where id = $song_id limit 1");
while($song_row= mysql_fetch_assoc($song_qry)){
$cateName = $cate_row['category'];	
$downloadNum = $song_row['n_down'];	
$filename=$song_row['file_name'];						
}
$song_url = 'song/'.$cateName.'/'.$filename;
	
if(file_exists($song_url)){
//update n_download
exec_query_utf8("update tblsong set n_down=".($downloadNum+1)." where id=$song_id");					
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($song_url));
ob_clean();
flush();
readfile($song_url);
exit;
}else{
//return something here
}

This download.php file contains the major functions for pushing up the download. In this file, the ID is needed to query for the other needed data from the database like category name, total download, and filename. Having had all these data, the file will have the full URL to be downloaded. I may need to do a condition just to verify whether the filename with URL exists or not. If the file exists, the URL is able to be pushed to the download. And, we can also do the update to the total download just for updating the download information using the UTF8 function exec_query_utf8. To push the download, we use PHP header function as shown in above and use function readfile() to start downloading.

Main File (songlist.php)

<a href="download.php?id=[songid]" target="_new"><img src="download.png" /></a>

This is the main list page where downloading button is placed somewhere within it. It can be a HTML file if we want to do the static list or it can be a PHP file if we want to do the dynamic list. Either static or dynamic, we have to put the link to the file download.php and identify the ID or song ID to download its file.

Similar Tutorials

Comments