Excel export: browser hangs up - php

I use this http://phpexcel.codeplex.com/ to export to excel.
need to export 11000 - 65000 rows...
but when trying to do this browser hangs up.
it do not answer with errors or other messages. just show that request in progress.
top shows that apache finished work.
even apache log says that all done:
x.x.x.x - - [28/Jul/2011:12:28:38 +0300] "POST /doctor/lab/statistic/ HTTP/1.1" 200 2773504 "http://x.x.x.x:82/doctor/lab/statistic/" "Mozilla/5.0 (X11; Linux i686; rv:5.0) Gecko/20100101 Firefox/5.0"
but still no results.
system:
Linux Eearth 2.6.34-12-desktop #1 SMP PREEMPT 2010-06-29 02:39:08 +0200 i686
PHP Version 5.3.5
Apache/2.2.15 (Linux/SUSE)
so the question
EDIT 1
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
$writer = PHPExcel_IOFactory::createWriter($xls, 'Excel5');
$writer->save('php://output');
exit;
EDIT 2
i tried to save data to file like this $writer->save('test.xls'); filesize 2.7MB
then if i do this:
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
include 'test.xls';
exit;
download starts immediate...

It's possible that your web server is not emitting error messages, so I'd say check your error.log (should be hanging around in /var/log/apache2 or similar.) It might well be something trivial like not being able to write to a temporary file.
If that turns out as a dead-end, consider programmatically creating CSV files with echo statements - Excel will open those just fine.
EDIT
To produce CSV, do your headers
header('Content-Type: text/comma-separated-values);
header('Content-Disposition:attachment;filename="'.$filename.'.csv"');
header('Cache-Control: max-age=0');
Then output your column headers:
echo "col1,col2,col3,col4,..."
followed by a new line. Then output your data:
while(/*Still got some rows*/) {
echo $row[0] . "," .$row[1] "," + ...
}
Have a look at: http://en.wikipedia.org/wiki/Comma-separated_values, it's a really simple file format and Excel will open it without difficulty.
The other thing, which I hadn't thought of, is if you've got access to a Windows Server, you can produce Excel files directly via COM interop.

Related

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

Large .zip files download incomplete

I am a severe novice, but I did quite a lot of research before posting, so I hope you can help.
I'm trying to serve a large .zip file that is hosted on an Apache server, about 6.4 Gb. The file shows complete download in browser, but in fact only about 500Mb have been downloaded. This seems like a very common problem and have found a lot of other posts and information on the web, but the problem has been persistent for me.
Large Zip file offered for download using php
IE download incomplete even though it claims success
Large zip downloads failing only in IE
I have been testing with Chrome 39.0.2171.71, but I get the same problem with FireFox and IE. I think my file is much larger than what others have been posting about, therefore perhaps their solutions helped the situation, but didn't fix the root problem. I have a second .zip file that is about 400Mb, and I use the same http headers with success.
The most useful article I have found is this: http://perishablepress.com/http-headers-file-downloads/ and I have copied much of the php shown below from that source, as it appears other posters on this website have done.
I have also tried using X-SendFile, but I don't think my webhost has the appropriate Apache module installed. I've spent all day working on this, and have run out of ideas! I have used a download manager with success, I don't know if this was just by chance or what, but I don't want to require my clients to have to download and install a separate program just to get the .zip file.
<?php
// HTTP Headers for ZIP File Downloads
// set example variables
$filename = "huge.zip";
$filepath = "****";
// http headers for zip downloads
// header("X-Sendfile: $filepath$filename");
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("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
set_time_limit(0);
ob_end_flush();
#readfile($filepath.$filename);
?>
Here are the response headers when I run the php above
Remote Address:76.162.142.242
Request URL:****/GetSW.php
Request Method:GET
Status Code:200 OK
Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Authorization:Basic ******
Connection:keep-alive
Cookie:_ga=GA1.2.1176828605.1417985823
DNT:1
Host:www.teamursa.org
Referer:http://www.teamursa.org/****.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36
Response Headers
Cache-Control:public
Connection:Keep-Alive
Content-Description:File Transfer
Content-Disposition:attachment; filename="huge.zip"
Content-Length:6720560824
Content-Transfer-Encoding:binary
Content-Type:application/octet-stream
Date:Sun, 07 Dec 2014 22:16:57 GMT
Expires:0
Keep-Alive:timeout=3, max=120
Pragma:public
Server:Apache
X-Powered-By:PHP/5.2.17
You might need to increase the memory limit on the server. Try the below just after the opening PHP tag
ini_set('memory_limit','16M');
And just keep increasing from 16M
I recommend you try using a web browser to download file directly, meaning you go to the address that file is stored at. E.g www.example.com/downloads/download.zip Hope this helps

Slower downloads through Apache than with PHP readfile

I've set up a Download-Script with PHP on my server, which checks some details before letting the user download files via Apache (X-Sendfile). The Files are outside the Document-Root.
The code for downloading with Apache and the Module X-Sendfile is:
header("X-Sendfile: $fullpath");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$link_file\"");
When using Apache and X-Sendfile i have a download-speed of 500 kB/s with my client. I also tested it with Apache and without X-Sendfile with the same file within the Document Root - same thing here!
So I tested downloading the same file, with the same client, the same infrastructure on both sides and the same internet-connection a few seconds later via PHP with readfile:
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/octet-stream");
header("Content-Length: ".(string)(filesize($fullpath)));
header("Content-Disposition: attachment; filename=\"$link_file\"");
readfile($fullpath);
This time the download-speed was 9.500 kB/s!
I repeated this test using both options more than a multiple times and the result was the same every time trying. The only difference besides the download-speed was a waiting time of a few seconds (depending on the size of the file which was downloaded), when trying with the PHP readfile method. When repeating the PHP readfile method instantly, the waiting time didn't appear again. Most likely because it was stored in the memory after the first time.
I'm using a professional HP Raid-System on the server, which has an average local Speed of 800 MB/s, so the reason for this can't be the Diskspeed. Also i didn't find any compressing- or bandwith-settings in the httpd.conf of Apache.
Can anyone of you explain why there is such a great difference of the download-speed and how this can be changed?
Thank you in advance.
Server: Windows Server 2008 R2 Apache/2.2.21 (Win32) PHP/5.4.20
Client: Windows 7 Ultimate x64 Google Chrome 30.0.1599.101 LAN >100 Mbit/s
SOLUTION:
httpd.conf, turn on the line "EnableSendfile off"

pdf generator from html in php using Wkhtmltopdf

On my windows with xamp local server I follow pdf generator from html
step 1 & 2 (wkhtmltopdf http://www.test.com test.pdf) goes fine but step 3
3. In php you should execute the tool using shell_exec:
shell_exec("/usr/local/bin/wkhtmltopdf-i386 http://test.com test.pdf");
//for Linux 32 bits operating system
//for Linux 64 bits operating system use wkhtmltopdf-amd64
//for windows just put the path of the exe file.
$allfile = file_get_contents("test.pdf");
header('Content-Type: application/pdf');
header('Content-Length: '.strlen($allfile));
header('Content-Disposition: inline; filename="test.pdf"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression','0');
does not do any thing... :(
I installed at e drive and used:
exec("e:\wkhtmltopdf\wkhtmltopdf.exe http://www.google.com/ test.pdf");
Actually I creates PDF correctly but does not send output to browser..
any idea what I had missed ...?
I'm guessing but the issue might be file locations. Does file_get_contents actually find the file?
I'm not very sure where the pdf file gets generated during the exec, try to use full paths for both the output pdf location and the file_get_contents location, they might not be correct by default. Maby even check somehow that the files exist and what folder are they both using (I cant remember how to do this in PHP :))

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.

Categories