php - download large file from ftp server and limiting download speed - php

i have website for storing large zip file and upload it to ftp server in php
now i want to download the file from ftp and limiting download speed
and be able to resuming and pause download i have this code :
$file_path = 'ftp://'.$ftp_username.':'.$ftp_password.'#'.$ftp_server.'/'.$ftp_filename_withDiR ;
$filesize_ftp = filesize($file_path);
ob_clean();
$download_rate = $Mbspeed;
header('Content-Description: File Transfer');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Type: application/octet-stream');
header("Content-Length:".$filesize_ftp);
header('Content-Disposition: filename='.$filenamea);
flush();
$file = fopen($file_path, "r");
while(!feof($file)) {echo fread($file, round($download_rate * 1024));flush();usleep(200); }
But i can't pause the download ! any ideas to solve this problem?

Related

Why is the browser running in circles and does not want to force the download of the PDF file in PHP?

I have created a script that allowed me to download docx, srt and pptx files and many others....
But problem, my script does not want to force the download of PDF files. The Chrome browser is running in circles and still doesn't want to launch the download of my PDF file.
Here is my script:
<?php
require_once 'includes/constants.php';
$pathFileUrl = '../'.WEBSITE_NAME_URL_FILES.'/docs/'.urldecode($fileUrl);
if (isset($fileUrl) && !empty($fileUrl) && file_exists($pathFileUrl)) {
$finfo = finfo_open(FILEINFO_MIME);
$mime = finfo_file($finfo, $pathFileUrl);
finfo_close($finfo);
header('Content-Description: File Transfer');
header('Content-Type: '.$mime); //application/octet-stream
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="'.basename($pathFileUrl).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($pathFileUrl));
// Read file and send to browser
readfile($pathFileUrl);
exit;
} else {
redirect(WEBSITE_NAME_URL);
}
Can you please tell me where the error is coming from?
Any help would be appreciated :)

downloading large zip files to the browser in php, chunking

I am trying to download large zip files (800MB to 1GB) containing audio files to the browser. As I have seen so far, chunking seems to be the most popular approach, but I am having zero luck. The code I have been working with is
$filename = $_SERVER['DOCUMENT_ROOT'] . $filepath;
$download_rate = 5000;
$progress = 0;
if (file_exists($filename)) {
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($filename));
header('Content-Disposition: filename='.basename($filename));
flush();
$file = fopen($filename, "r");
while(!feof($file)) {
// send the current file part to the browser
print fread($file, round($download_rate * 1024));
// flush the content to the browser
flush();
// sleep one second
sleep(1);
}
fclose($file);
} else echo 'File does not exist!';
This works for a wide variety of file types and sizes up to a certain point -- I have no problem downloading typical PDFs, etc. But the browser just spins and spins when I try to download large zip files, and eventually dies (around 2 minutes). I am really needing some help here. I've tried a number of code variants for chucking, but something always seems to die and I'm not sure what I'm doing wrong. Perhaps there's a better approach to chunking?
Consider change change for readfile will not present any memory issues, your lost download rate and progress show in browser. your limit download rate in .httaccess.
$filename = $_SERVER['DOCUMENT_ROOT'] . $filepath;
if (file_exists($filename)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: '.filesize($filename));
readfile($filename);
} else echo 'File does not exist!';
https://www.php.net/manual/en/function.readfile.php

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();
}
}
}

Simple PHP script for XZ file download

I'm trying to setup a backup system that will perform a backup, compress the file and make it downloadable from a browser. The file downloads properly but when I try to uncompress it I get:
unxz: backup_2015-08-12.txz: File format not recognized
There is a backup script that will output
/tmp/agribackup.txz
The PHP script is as follows:
<?php
// Create the agribackup.txz file
$e = shell_exec("/usr/local/bin/agribackup.sh");
$file = '/tmp/agribackup.txz';
if(file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/xz');
header('Content-Disposition: attachment; filename=backup_'.date("Y-m-d") . '.txz');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
I think you are using the wrong Content-Type. Refering to 'http://tukaani.org/xz/xz-file-format.txt', the correct MIME-type is 'application/x-xz'.

Categories