EarthCow
EarthCow

Reputation: 67

PHP downloading will randomly give a network error

I currently own and operate an online FTP, off a rented server, running on PHP and I am trying to incorporate downloads (as that is on of the most basic functions of an FTP). I have done this with the code below, however when I try to download my server backups which are usually about 800mb the downloads will only sometimes result in a network error. Which is very annoying and inconvenient. I appreciate any thoughts on why this would be happening. Thanks in advance! I only have 2gb of RAM.

header("Content-Disposition: attachment; filename=\"$name\"");
header("Content-Type: application/octet-stream");
header("Content-Length: ".filesize("ftp://$u:$p@$h".$_GET['data']));
header("Connection: close");
readfile("ftp://$u:$p@$h".$_GET['data']);

The downloads bar in chrome. Different downloads will fail in different places during the download.

Upvotes: 0

Views: 2518

Answers (3)

EarthCow
EarthCow

Reputation: 67

Since you were trying to download multiple files at once and you only have 2gb of RAM, it is likely that the rented server cuts off the connection.

Upvotes: -1

Aabir Hussain
Aabir Hussain

Reputation: 1181

You need to set limits for PHP as well as for web server

IN PHP

// add these in starting of the files
ini_set('memory_limit', '1G'); //according to your requirements
//only for development will show some extra debug info.
ini_set('display_errors', true);
ini_set('max_execution_time', 0); //0=NOLIMIT

ALSO We have to set these settings (time and size) at web server level, I am assuming you are using NGINX

IN WEBSERVER (vi /etc/nginx/nginx.conf)

keepalive_timeout  60; #try this with 600, i thing your issue belongs here
send_timeout 60; #try this with 600, i thing your issue belongs here
client_body_timeout 120; #try this with 600, i thing your issue belongs here
client_max_body_size 5G; #default is 4.5 GB so you dont need this if data is less than 4.5GB

Don't forget to restart NGINX after web server changes.

for more info on nginx limits click here

Upvotes: 2

CloudTheWolf
CloudTheWolf

Reputation: 375

Try putting header('Expires: 0'); and set_time_limit(0); before the readfile() and see if this helps.

As for compressing, I believe you would first need to download the file to the server before compressing it.

Not sure if there are free alternatives but Chilkat has both FTP and File Compression (Zip, GZip or TAR)

Example on Compressing: https://www.example-code.com/phpExt/tar_create_bz2.asp

Upvotes: 1

Related Questions