/    Sign up×
Community /Pin to ProfileBookmark

showing download progress while downloading a file using PHP and jquery

I am developing a download manager using php and jquery.

Requested a script that will download a file and also show download progress.

I tried following, but it not works

Jquery

function downloadFile(){
var fileNameDownloading =”somefile.mp3″
var oReq = new XMLHttpRequest();

oReq.addEventListener(“progress”, updateProgress, false);
var params = “filename=”+fileNameDownloading;

oReq.open(“GET”, “scripts/download.php?”+params, true);
oReq.responseType = “blob”;//blob arraybuffer

function updateProgress (e) {
console.log(e.loaded/e.total);
}
oReq.send();

}

PHP

<?php
$file = $_GET[‘filename’];
$fileSize = get_size($file);
$packetSize = 262144;//2000
if($packetSize > $fileSize){
$packetSize = $fileSize;
}
download($file,$packetSize);

function download($file,$chunks){
set_time_limit(0);
header(‘Content-Description: File Transfer’);
header(‘Content-Type: application/octet-stream’);
header(‘Content-disposition: attachment; filename=’.basename($file));
header(‘Cache-Control: must-revalidate, post-check=0, pre-check=0’);
header(‘Expires: 0’);
header(‘Pragma: public’);
$size = get_size($file);
header(‘Content-Length: ‘.$size);

$i = 0;
while($i<=$size){
//Output the chunk
get_chunk($file,(($i==0)?$i:$i+1),((($i+$chunks)>$size)?$size:$i+$chunks));
$i = ($i+$chunks);
}

}

//Callback function for CURLOPT_WRITEFUNCTION, This is what prints the chunk
function chunk($ch, $str) {

print($str);
return strlen($str);

}

//Function to get a range of bytes from the remote file
function get_chunk($file,$start,$end){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_RANGE, $start.’-‘.$end);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, ‘chunk’);
$result = curl_exec($ch);
curl_close($ch);
}

//Get total size of file
function get_size($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
return intval($size);
}

?>

Looking for a suggestion, how we can download a file and also show download progress using PHP and javascript. Thanks

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @bindasho spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...