How do I know how many times my file has really downloaded? - php

The task is to create a controller to count the number of file downloads. It also must be able to account for failed or cancelled downloads. I was wondering if anyone knew the best way to go about accomplishing this.
$file = $_GET['download'];
if (ob_get_level()) {
ob_end_clean();
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
return filesize($file);`
$size = filesize($file);
Then if the number of bytes given is approximately equal to the file size:
if ( $size < given bytes) {
$handle = fopen("counter.txt", "r");
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
$buffer=$buffer+1;
}
fclose($handle);
$fp = fopen("counter.txt", "w");
$test = fwrite( $fp, $buffer);
fclose($fp);
}
How to know the number of bytes sent by server to the user after clicking on link?

I'll start with what should be comments:
You've tagged this as javascript, but your quesiotn appears to have nothing to do with javascript. Please don't do that.
I assume you are aware of the gaping security hole exposed by your script / that you are not concerned about it.
Your handling of output buffering is wrong.
return filesize($file);
What is this line of code supposed to do?
header('Expires: 0');
no.
header('Pragma: public');
again, no.
As to your question - its all covered in the manual:
<?php
ignore_user_abort(1);
$file = $_GET['download'];
if (!is_readable($file)) {
die "No such file";
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: max-age=0; must-revalidate');
header('Content-Length: ' . filesize($file));
$count=0;
$ih=fopen($file, 'r');
while (CONNECTION_NORMAL==connection_status() && !feof($ih)) {
print fgets($ih, 4096);
}
log_completion(feof($ih));
BTW: This does not give an accurate record if the file was downloaded - you can only tell if the content has left PHP land.

Related

php file transfer from one server to another

I need to transfer a file from server1 to server2 using html-php. Right now from server1 the download file works fine with following php code, but instead of downloading to client machine, I need this file file to upload to another server, how can I do this.
Note that the file is not in web directory, I have to read the file from no web directory and transfer to server2.
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
flush(); // Flush system output buffer
readfile("/home/user/file.ini");
Method 1: Serve File to Download
Server 1
$file_name = 'usb.zip';
$file = '/usr/download/' . $file_name;
if (file_exists($file))
{
if(false !== ($handler = fopen($file, 'r')))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
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($file));
//Send Chunks
while(false !== ($chunk = fread($handler,4096)))
{
echo $chunk;
}
}
exit;
}
Server 2
file_put_contents("usb.zip", fopen("http://xxx/file.php", 'r'));
Method 2: Upload via SFTP
$resFile = fopen("ssh2.sftp://{$resSFTP}/".$csv_filename, 'w');
$srcFile = fopen("/home/myusername/".$csv_filename, 'r');
$writtenBytes = stream_copy_to_stream($srcFile, $resFile);
fclose($resFile);
fclose($srcFile);

Downloading file from another website in PHP creates the file but doesn't download any bytes

I'm trying to download this file from DropBox, i tried the same code with a file on the same website that running the PHP script and it did work but now when i add a full ULR, it doesn't.
The problem is that the QR-Kodite.rar file is created in my Windows downloads folder but its size is 0 and Chrome says that the download has finished.
This is my code :
if(isset($_GET['App'])){
if(basename($_GET['App']) == $_GET['App']){
$path = 'https://dl.dropboxusercontent.com/s/pdg8bnpmbgvcsmd/QR-Kodite.rar';
$size = filesize($path);
header('Content-Type: application/octet-stream');
header('Content-Length: ' . $size);
header('Content-Disposition: attachment; filename=' . $path);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
$file = # fopen($path, 'rb');
if($file){
fpassthru($file);
$_SESSION["ITA"] = "No";
exit();
}
}
}

PHP headers to serve ZIP. It downloads but is corrupt... why?

Good morning,
I'm working on serving a zip file through the PHP headers, which downloads, but every time I try to extract it shows that the zip is corrupt... I need a 2nd pair of eyes to look over this... what am I missing? Thank you so much!
$file_download = 'example';
if (isset($file_download)) {
$file = 'path/to/file/'.$file_download.'.zip';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
} else {
echo 'File does not exist!';
}
}

How to properly send file to browser?

I want to send a file directly to browser:
<?php
#ini_set('error_reporting', 0);
#apache_setenv('no-gzip', 1);
#ini_set('zlib.output_compression', 'Off');
#ob_implicit_flush();
$file = 'ORG.iso';
$quoted = sprintf('"%s"', addcslashes(basename($file), '"\\'));
$file_size = filesize($file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $file_size);
#ob_end_flush();
set_time_limit(0);
$f = fopen($file, 'r');
while (!feof($f))
{
$buff = fread($f, 204800);
echo $buff;
sleep(1);
}
fclose($f);
It shows correct file size in Firefox and Chrome right after visitor enters the link. However it does not work in jDownloader. It seems like jDownloader is waiting for the whole script to end.
When I try a normal, static file from the same webserver- jDownloader shows file size instantly.
So obviously something is wrong with my script.

PHP Zip File Empty?

I have a script that creates a zip files from files in a certain directory. After Download, for a lot of users - the zip file is empty. However, for other users - the file isn't empty. Is it something I'm doing wrong?
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$id.'.zip"');
header('Cache-Control: private');
ob_clean();
flush();
readfile("".$id.".zip");
exit;
Better add some error control:
$file = "{$id}.zip";
if (!is_readable($file))
{
die('Not accessible.');
}
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Cache-Control: private');
ob_clean();
flush();
readfile($file);
exit;
Additionally triple check your output buffering configuration vs. readfile.
I recommended this as a suggestion earlier, here is a skeleton of what I am talking about. It is untested as I don't have access to php at the moment.
$filename = $id . '.zip';
$handle = fopen($filename, 'r');
if ($handle === false)
{
die('Could not read file "' . $filename . '"');
}
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Cache-Control: private');
while (!feof($handle))
{
echo fread($handle, 8192);
}
fclose($handle);
exit;

Categories