Simple page download file using php and jquery
If you want to hide the download links at the same time requires users to wait 5 seconds or more (depending on you). Users can download a file via a php script to hide the real path, web address or URL to that file. many sites today use a download manager such way. This article will show you how to create a script to download it on your website.
To do page download file you need to follow these steps ?
First we create three folders: contain_folder, include, and css
In the folder: include
– config.php: First line enter the name of the directory to upload files UPLOAD_DIR. here I enter the folder name is “contain_folder”. Second line enter the random string SECURITY_CODE.
1 2 |
define('UPLOAD_DIR','folder contain'); define('SECURITY_CODE','kythuatmang.net-1235434543523233453Q@#$$#'); |
– function.php: includes of two functions is makeHash() used to create the download url and verifyHash() is used to check the hash of the url.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php function makeHash($fileName) { global $_SERVER; return md5($_SERVER['REMOTE_ADDR'].$fileName.date('Y-m-d,h').SECURITY_CODE); } function verifyHash($fileName,$hashCode) { global $_SERVER; return $hashCode==makeHash($fileName); } ?> |
In the folder: contain_folder (Folder in your web server that contains the content to be downloaded)
– .htaccess: inside this protected folder containing the content for downloading , it prevent direct file downloading and any forms of hot linking
First check if mod_access in installed to apache, then add the following to your .htaccess
1 |
deny from all |
In root folder: the root directory of your website
– x.php: check the hash is created from the function.php file in the folder include and return url download
– .htaccess: add RewriteRule to file .htaccess
1 2 |
RewriteEngine On RewriteRule ^download/([a-f0-9]{32})/(.+)$ x.php?file=$2&hashCode=$1 |
– download.php The page where you will need to show the download link should execute a PHP script or have a .php extension. You can create download link as below:
PHP download code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php header('Content-Type: text/html;charset=UTF-8'); include 'include/config.php'; include 'include/function.php'; $fname=$_GET['f']; $download=1; if(!file_exists(UPLOAD_DIR.'/'.$fname)) { $download=0; } $downloadLink='download/'.makeHash($fname).'/'.$fname; function file_size($url){ $size = filesize($url); if($size >= 1073741824){ $fileSize = round($size/1024/1024/1024,1) . 'GB'; }elseif($size >= 1048576){ $fileSize = round($size/1024/1024,1) . 'MB'; }elseif($size >= 1024){ $fileSize = round($size/1024,1) . 'KB'; }else{ $fileSize = $size . ' bytes'; } return $fileSize; } ?> |
HTML download file code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<div class="container"> <span class="filename" id="fileinfo-filename" title="<?=$fname?>"><?=$fname?></span> <span class="fileinfo" id="fileinfo">File Size: <span id="fileinfo-filesize"><? if($download!=0){ echo file_size(UPLOAD_DIR.'/'.$fname); }else{ echo 'N/A'; } ?></span> - Your IP: <span id="fileinfo-views"><?=$_SERVER['REMOTE_ADDR'];?></span></span> <div id="btnX" class="btn btn-blue"> <span class="text1" id="countdown-info">PREPARERING DOWNLOAD...</span> <span class="timedown" id="countdown-time"></span> </div> <div id="<?=$download?>" class="loading" style="display: none;"></div> <input id="download" type="text" style="display: none;" value="<?php echo $download ?>"> <a class="btn btn-green" id="btn-download" style="display: none;" href="<?=$downloadLink?>"> <span class="text2">DOWNLOAD</span> </a> </div> |
Here I show information iP visitors
HTML jquery code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<script type="text/javascript"> https://visofts.com/wp-admin/post-new.php# var countDown; jQuery(document).ready(function(){ countDown=function(start){ if(start==0) { jQuery('#countdown-time').html(''); jQuery('#countdown-info').html('PREPARERING DOWNLOAD...'); bla = $('#download').val(); if(bla==0){ jQuery('#countdown-info').html('ERROR: Press F5 to try again. <br></br>404: File not found!'); }else{ jQuery('#btnX').css('display','none'); jQuery('#btn-download').css('display','inline-block'); } return true; } jQuery('#countdown-time').html(start+'s'); start--; setTimeout('countDown('+start+')',1000); } countDown(5); }); </script> |
The following code will give the download can not detect the folder containing the file to be downloaded. and wait 5 seconds to show the download file links. or you can also count the number of downloads of using mysql database, file size, ip, etc…
Place all three PHP scripts and the .zip file into the same directory on your server and Change the file permission of the directory to 644. Hope that this will help you.
A piece of erduition unlike any other!