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.
Related
I created a simple file to download files.
I have the following code:
function DownloadFile($file) { // $file = include path
if(file_exists($file)) {
header('HTTP/1.0 200 OK', true, 200);
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
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');
ob_clean();
flush();
readfile($file);
exit;
}
}
The problem is if I try to download files over 40-50MB the file is downloaded blank(0KB) but if I try below 40MB it is working perfectly.
What is the problem?
Like explained in this article https://www.sitepoint.com/community/t/php-file-size-download-limit/6541 you need to set
memory_limit = 128M
post_max_size = 300M
in php.ini file
The solution:
function DownloadFile($file) { // $file = include path
if(file_exists($file)) {
header('HTTP/1.0 200 OK', true, 200);
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: chunked');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
ob_end_flush();
if ($fd = fopen ($file, "r")) {
set_time_limit(0);
ini_set('memory_limit', '1024M');
while(!feof($fd)) {
echo fread($fd, 4096);
flush();
}
}
}
}
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);
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.
The following code works in Safari but not Firefox or Chrome
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));
ob_clean();
flush();
readfile($file);
ob_end_flush();
exit;
The webpage at XXXX might be temporarily down or it may have moved permanently to a new web address.
Error code: ERR_INVALID_RESPONSE
$file variable links to a file outside of the public_html directory.
I can't seem to find a fix, any ideas?
I've had issue with filesize() on chrome and firefox in the past. This was my fix.
<?php
Function real_filesize($file) {
$fmod = filesize($file);
if ($fmod < 0) $fmod += 2.0 * (PHP_INT_MAX + 1);
$i = 0;
$myfile = fopen($file, "r");
while (strlen(fread($myfile, 1)) === 1) {
fseek($myfile, PHP_INT_MAX, SEEK_CUR);
$i++;
}
fclose($myfile);
if ($i % 2 == 1) $i--;
return ((float)($i) * (PHP_INT_MAX + 1)) + $fmod;
}
$filename = "somefile.ext";
$filepath = "some/dir/path/to/file/".$filename;
$filesize = real_filesize($filepath);
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream');
header("Content-Type: application/download");
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);
ob_clean();
flush();
readfile($filepath);
exit();
?>
Just removed the line ob_clean() worked for me.
I'm trying to use PHP to serve up a file download. I can get the file to begin downloading, but it has only ever been able to download about 280MB of a 1GB file. I'm on a shared host so this could be the issue, if it's not an issue with my code:
function downloadFile($file){
if (file_exists($file)) {
if(is_dir($file)){return false;}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
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: '.sprintf("%u", filesize($file)));
ob_clean();
$handle = fopen($file, "rb");
$chunksize=(sprintf("%u", (filesize($file))/1024));
set_time_limit(0);
while (!feof($handle)) {
echo fgets($handle, $chunksize);
flush();
}
fclose($handle);
die;
}else{ echo "file not found";}
return;
}