Large file download taking more time - php

I have used following code to download approximate 920MB file,
set_time_limit(0);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("X-Sendfile: $zipname"); // For Large Files
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$zipname."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($directory_location . '/' . $zipname));
ob_end_flush();
readfile($directory_location . '/' . $zipname);
Before this code i did some study with the following links Using X-Sendfile with Apache/PHP, Streaming a large file using PHP and Limit download speed using PHP but not much helpful to me because file download still takes more time with just (2MB) file. It's not showing and transfer rate or anything else. I want download start to serve file with around 60Kbps, with all files (Large or small)
UPDATE: One more thing i noticed its not showing any download process just executing and after sometime display the pop-up to choose the location, and after hitting save button its direct save to the computer without any downloading process window :(
Please help me to guide the right way.

Based on above comments there are two solutions:
1) Just download the file directly. You don't appear to be doing any validation, so if not, then just pass the user to the file to download and let apache handle it.
2) If you do need validation / pre-processing, then check mod_xsendfile - adding the header isn't enough, you actually need to add the mod to apache. If you're in Linux then compile from source (https://tn123.org/mod_xsendfile/). If you're not in Linux then mod_xsendfile for Win x64? has a response from the author saying he can provide binaries - but that's in 2010. There's a bit of advice around the web - although it's been a while since I looked at it so can't really help much more.

Related

Random Slow Download Speed with PHP & Apache

So my company store all PDF files in Amazon S3 privately.
When the user request it our system pull it from Amazon S3 and then serve it to the user with following code:
header("Cache-Control: public");
header("Pragma: public");
header("Expires: 0");
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header('Content-Length: ' . strlen($res->body));
header("Content-type: application/pdf");
header("Content-Transfer-Encoding: binary");
header('Connection: close');
echo $res->body;
$res is the respond returned from Amazon with the content from $res->body;
I see random slow download speed when the user try to download the PDF files, especially when the PDF is large (~5mb) compare to the rest that only having 800kb-1.5mb.
Solution tried:
1) Removing the content-length header doesn't help.
2) Remove EnableSendfile off in httpd.conf doesn't help either.
I also checked the server to make sure it wasn't the workload of the server that's causing this.
The speed test of both the server and user's workstation looks good too.
Do anyone of you have any idea what is the reason that's causing this slowness?
It might be that multiple people from your company use the internet.
You should do a full scan of the network, but still even then your not alone on the internet, and their "virtual" server environments are shared as well.
from my understanding the issue is that simply takes time to fetch the file from S3 in order to return it to the user.
Make yourself a favor:
create a signed url for a short period time
redirect the user to such url
dont worry, as creating signed urls doesn't expose any private information that compromises your security (if you do ir correctly)

Firefox downloads(tries to open) .zip file as .HTM

I am creating downloadable zip file, it works fine almost everywhere. But in Mozilla Firefox on save of this zip I get strange message that my_zip.zip is HTM file (sorry for the language, but I hope it is pretty understandable):
If I choose save option it will be saved as normal zip (no sign of HTM at all), but in "open as" section there are only programms for opening HTM
So, the question is How to make Firefox detect this zip as zip?
I am currently using this headers (set by PHP):
header('Content-Description: File Transfer');
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$zipFileName");
header("Content-Transfer-Encoding: binary");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-store");
header('Pragma: no-cache');
header("Content-length: " . filesize($zipFileName));
readfile($zipFileName);
Already tried using header("Content-Type: application/zip"); , does not work; plus application/zip is not standart (as I read here in some headers related question).
I am using Mozilla Firefox v40.0.3, the php project is using Laravel 5.1 (I doubt it has anything to do with this)
UPDATE:
While trying different application\[format]s , I added a dump and die command after headers
//bunch of kosher headers here...
readfile($zipFileName);
dd(headers_list());//dumps and dies
And I get a zip type in download window. Then I figured out that after die or exit I will always get right download type of zip; Then I deleted all dump-and-die sections , but download type remains as zip. I have no idea what i have fixed by this manipulations.
I would love to have an explanation of this strange situation
A quick google search suggests the Content-Type seems to be the culprit
header("Content-Type: application/octet-stream");
Try setting it to application/x-zip-compressed ?
ALso the comments in this bug report may be useful: https://bugzilla.mozilla.org/show_bug.cgi?id=540900

download mp3 from web url with android failed

I have a link to download mp3 on all mobile device they can read them.
I also have an Acer E320 with Android 2.3.4 and I want to download my mp3 by clicking my link (web server Apache; this is some PHP source behind that link).
I tried with a BlackBerry, and I got the file.
I tried with my Firefox browser with a user-agent of a Samsung Galaxy S with Android, and I got the file.
I tried with my Acer E320 with the same Android 2.3.4, and... I didn't get the file.
I am obviously sure that comes from the OS, I'll try with a Windows Phone tomorrow but I'm pretty sure this will works.
Here are my sources for the download:
<?php
// assume that all variables are correctly set
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: audio/mpeg");
header('Content-Disposition: attachment; filename="'.$filename.'";');
header("Content-Transfer-Encoding: binary");
header('Content-Range: ' . $download_range);
header('Content-Length: ' . $download_size);
readfile($filename);
?>
Thank you if you have any idea why this code doesn't work on an Android phone.
EDIT: I tried with an HTC with Windows Phone OS. The download didn't start but I can see my hit on the apache log... It seemes that Android is not alone...
2nd EDIT: I finnaly find it: there's a pre-fetch done with Android that kill my download because of a one-use token.
Thanks all
I finally found it:
There's a pre-fetch done with Android that kills my download, because of a one-use token.
Thanks all.

Script to Download File from Android Browser

I am not a web developer by all means. But I am trying to help someone deliver a video from their server. Basically when the user clicks a button on the website, we want the user to be prompted to download the video. So after Googling for a while I figured out how to write a short php script using content-disposition:
<?php
$fn = 'videoFile.mp4';
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header('Content-Description: File Transfer');
header('Content-type: video/mp4');
header('Content-Length: '.filesize($fn));
header('Content-disposition: attachment; filename='.$fn);
readfile($fn);
?>
So the button on the website points to this script. PC browsers seem to start the download with no problem. But the main focus of this is for Android phones. When you click to download the file on the default Android Browser, it fails and all it says is "Download Unsuccessful".
Another thing is that on my computer (using Firefox), the download starts and at about 200MB, it just stops. It doesn't show any signs of failure, it just looks like it finished downloading. The actual file size is about 1GB.
I have played around with the content-type quite a bit, hoping that was the problem. I used "video/mpeg" and "application/force-download" and neither of them worked.
Is there something I'm not doing write? Could the 2 problems be related? Should I be going about this a different way? Thanks for any input.
The Android browser does not appear to like Content-disposition: and related headers. I recommend just a plain redirect to the file in question.

Forced downloading large file with php

Many users of my site have reported problems downloading a large file (80 MB). I am using a forced download using headers. I can provide additional php settings if necessary. I am using the CakePHP framework, but this code is all regular php. I am using php 5.2 with apache on a dedicated virtual server from media temple, CentOS Linux. Do you see any problems with the following code:
set_time_limit(1500);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . basename($file_path) . "\"");
header("Content-Length: ".$content_length);
header("Content-Transfer-Encoding: binary");
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Pragma: public');
header('Expires: 0');
//Change this part
$handle = fopen($file_path, 'rb');
while (!feof($handle))
{
echo fread($handle, 4096);
ob_flush();
flush();
}
fclose($handle);
exit;
Basically, the problem being reported is that the download starts and then stops in the middle. I was thinking it was a problem with the time limit, so I add the set_time_limit code. I was using the php readfile function before, but that also did not work smoothly.
The problem with PHP-initiated http transfers is that they seldomly support partial requests:
GET /yourfile HTTP/1.1
Range: bytes=31489531-79837582
Whenever a browser encounters a transmission problem, it will try to resume the download. Your php script does not accomodate for that (it's not trivial, so nobody does).
So really avoid that. Redirect users to a static file and let your webserver handle it. If you need to handle authorization, use tricks like symlinks or rewriterules that check for session cookies or even a static permission file (./allowed/178.224.2.55-file-1). Any required extra HTTP headers can be injected likewise, or with a .meta file.
I don't see any trouble, but for S&G's try placing the set_time_limit inside the while loop. This ensures they don't hit a hard limit and (as long as the client's taking the information) the time-limit gets extended.

Categories