I'm trying to count total bandwidth usage per download.
For a example if I'm sending a 100 mb file to a user and the user disconnects/stops downloading half way so actually only 50 mb being used. Is there a way to track this?
I found this example in a tutorial and in a question on sof.
if ($fd = fopen ($fullPath, "r")) {
//the next part outputs the file
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=".$path_parts["basename"]."");
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
and
PHP - determine how many bytes sent over http
Related
Okay , I know it sounds like duplicate but it's not.
there's a lot of questions and answers about downloading a mp4 file instead of playing it by browser.
my issue is my mp4 has 1GB size and my server has 512 ram and 1 CPU so , this methods are not working for me.
Here's my current code :
<?php
ini_set('memory_limit', '-1');
$file = $_GET['target'];
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>
Is there any way to make it happen on a huge file ?
Have you tried downloading the file in chunks, e.g.:
$chunk = 256 * 256; // you can play with this - I usually use 1024 * 1024;
$handle = fopen($file, 'rb');
while (!feof($handle))
{
$data = fread($handle, $chunk);
echo $data;
ob_flush();
flush();
}
fclose($handle);
I'm having an issue when trying to make a download script on my website. What ends up happening is that it only downloads between 160B to 310B instead of the whole 170MB that the file is. I'm not sure why it's happening, and if anyone can help that would be greatly appreciated. Also, I'm not sure if it's an issue with the code itself or that my host doesn't allow big files to be downloaded, as I get this message on net2ftp: "Files which are too big can't be downloaded, uploaded, copied, moved, searched, zipped, unzipped, viewed or edited; they can only be renamed, chmodded or deleted."
Here is my code:
ignore_user_abort(true);
set_time_limit(0);
$path = "/versions/";
$dl_file = $_GET['file'];
$fullPath = $path.$dl_file;
if ($fd = fopen($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
header("Content-length: $fsize");
header("Cache-control: private");
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose($fd);
exit;
So here i'm trying to send to the client some files received in a array from javascript. My problem is that i cannot redefine the filename (since its a header). So for example, i wanna send 3 files to the client, the first one is sent, then i got a error for the 2 last. Does anyone have idea how to fix it? Here is my php code:
PHP
<?php
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script
$path = "../temp/"; // change the path to fit your websites document structure
$data = escapeshellarg($_GET['File']);
$data = str_replace("\\","",$data); // remove useless characters inserted
$data = str_replace("\"","",$data); //
$dl_file = explode(",", $data);
header("Content-type: application/octet-stream");
header("Cache-control: private"); //use this to open files directly
header('Pragma: private');
for($i = 0; $i < count($dl_file); $i++)
{
$fullPath = $path.$dl_file[$i];
$path_parts = pathinfo($fullPath);
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"",true);
if ($fd = fopen ($fullPath, "r"))
{
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
ob_clean();
flush();
readfile($fullPath);
}
fclose ($fd);
}
exit;
?>
Here is the error i got in logs file of the server :
Warning: Cannot modify header information - headers already sent in (php file path)
I'm trying to download a simple text file from my server to the client in php. Unfortunately, when i try to download, a new white page appear and the download isn't ask to the user. But, if i refresh the page, the download is now ask and the user can accept to download the file like usual. Do anyone know why?
P.S. If anyone knows about how not opening another blank page to show the download and just show it on the page i clicked the link, any information would be appreciated.
Here is the code :
HTML
Download file
PHP
<?php
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script
$path = "toDownload/"; // change the path to fit your websites document structure
$dl_file = $_GET['download_file']; // simple file name validation
$fullPath = $path.$dl_file;
if ($fd = fopen ($fullPath, "r"))
{
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext)
{
case "txt":
header("Content-type: application/txt");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
break;
// add more headers for other content types here
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
header("Content-length: $fsize");
header("Cache-control: public"); //use this to open files directly
while(!feof($fd))
{
$buffer = fread($fd, 2097152); // 2097152 Octet = 2Mo
echo $buffer;
}
}
fclose ($fd);
exit;
?>
I'm currently using this code to hide download link but its giving uncompleted download for some users. I don't know why, but too many users report this problem to me.
My current code is:
file = "../".$realFileName;
$fakeFileName= 'Upbaz.ir-'.base64_decode($_GET['ffname']);
$fp = fopen($file, 'rb');
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$fakeFileName");
header("Content-Length: " . filesize($file));
fpassthru($fp);
Anyone know other way to hide download link?
This script will handle the downloading of the file and it includes a buffer/ different ext types (if you desire). A good way to hide links is to put files in a protected directory and have links stored in a database. The user sees an ID or a session tied to a file and the server finds the file and serves it.
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "txt":
header("Content-type: application/txt"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
default:
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
.htaccess to protect directory (you should have a dir for only these files
deny from all
Above script modified to yours:
$file = "../".$realFileName;
$fakeFileName= 'Upbaz.ir-'.base64_decode($_GET['ffname']);
if ($fd = fopen ($file, "r")) {
$fsize = filesize($file);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$fakename\"");
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
Increase script time run eq
#set_time_limit(120);