I work on a website, and up until Monday we were able to download a custom excel file sheet and display it. At this point, whenever we try to open it, it launches a blank, grayed-out workbook if Excel is not currently running, and nothing if it is. I have tried editing the content type to no avail. I have tried creating documents with older data that worked at the time and it doesn't work. Nothing has changed on the file system regarding this in a month, and it only just started. Here is the current code used to generate the file:
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=' . $fname);
ob_clean();
ob_flush();
echo $somecontent;
exit;
Here is what happens when I try to open the created file.
The only way I have found that fixes this currently is to open the file in Notepad++, edit it, and save it, which then allows it to open properly in Excel.
Any help would be appreciated.
EDIT 1: So because the comments below didn't help much, I took a look around and found this video, and upon disabling those three options I could open the files again without having to go through Notepad++ first. I then narrowed down the field that was causing issues to "Protect files originating from the internet" even though before they worked fine.
Related
I am asking this question because i found it impossible as far my knowledge stands. However i believe here on stackoverflow a lot of genius persons visits so maybe someone can give a good advice / trick.
My problem is, I am downloading a audio/mp4 file of youtube hosted on googlevideo.com's server.
My PHP code for this purpose:
$mp3path is url of video
header('Content-Description: File Transfer');
if(strpos($mp3path, "https://") === false) {
header('Content-length: ' . size($mp3path)); //size is custom function
}
header("Content-Type: audio/MP4A-LATM, audio/MP4A, audio/m4a, audio/mp4, audio/mp4a, audio/mp4-audio, audio/mpeg");
header('Content-Type: application/force-download');
header("Accept-Ranges: bytes");
header('Content-Transfer-Encoding: binary');
header('Content-disposition: attachment; filename="'.$title.'.mp3"');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
header("Cache-Control: private", false);
header('Pragma: no-cache');
readfile($mp3path);
exit;
I can download this audio file but few mp3 players are not able to play it when i did some research on by using mp3val [mp3val.sourceforge.net]. I found this file do not contain sample-rate, bit-rate or some other required codecs inside the file.
Error received from mp3val: Unknown file format
I know one possible way of doing this which is ffmpeg but i am looking for a less time consuming option of doing this because first ffmpeg download the whole file and then convert it to mp3 and save it on server. It takes a lot of time.
I am looking for a easiest solution in which, i dont want to save the whole file on my server. I want to call this file from remote server and want to add sample rate, bit-rate in the file and then just somehow with php i want to start download on browser.
All i mean to say i need a faster solution. I have seen 2 yt to mp3 converter, They are doing the same thing, giving instant download. i dont know how?
Please if you think its a stupid question or not possible then dont report to stackoverflow. I am trying to figure it out that's why i am asking this question here. I hope you'll understand my curiosity.
Thanks,
When I launch second time the application trying to make a new download then it is the previously downloaded content data which is downloaded ! Here is code :
$output = RP_MAIN . 'docbook/data/myfile.pdf';
header('Content-Type: application/x-download');
header('Content-Disposition: attachment; filename="'.'manuel.pdf'.'"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ob_clean();
readfile($output);
So how to clear the download cache before downloading ?
ctrl+F5 will force a browser refresh and clear the cache.
However to make the page load without using the cache each time, you can try this -
$output = RP_MAIN . 'docbook/data/myfile.pdf?'.rand();
This generates a random number on the end of the file name each time the page is loaded, so the browser will think it is a new file each time and not use the information stored in the cache.
Add a random parameter onto the file URL so your browser thinks its a new file:
$output = RP_MAIN . 'docbook/data/myfile.pdf?version=1.5';
I add ?nocache to the end of CSV file URLs. It doesn't have to be that word specifically, but it works for opening files in Excel. It does NOT seem necessary to have a random number there, at least for Excel.
I have a really weird problem regarding a small piece of code in a CodeIgniter application. Basically, there's a page with links to various PDF files. When a user clicks on the link, the request is parsed by PHP, an observer is notified, writing the click event in the database (activity log), and then the file is outputted by using readfile().
So far, so good. Tested it, it works like a charm. The PDF is outputted for download, and the event is written in the database as it should.
The problem comes when a user clicks on such link, then cancels the download and clicks on another link no later than 9-10 seconds. When that happens, the event is registered in the database twice.
I did triple check of the observers that record the event, but they appear to be fine. Besides, there's a similar function for a video links, only it redirects to another page instead of outputting the file directly, and it works just fine.
After a few hours of scratching my head, I figured there's an issue with the readfile() function, because, if I put a var_dump();die(); or anything that outputs some text before the download and force it to come as text, the download event is recorded only once.
Here's the code in question:
public function downloadPDF($id = NULL)
{
if (($id == NULL) OR (!$this->validateId($id))) {
// redirect with error
}
$item = // code for fetching the PDF properties from the DB
$this->notify('ActivityObserver'); // writes the download event in the DB
$file = '.' . urldecode($item['link']);
$size = filesize($file);
$name = urldecode(basename($file));
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header("Content-Disposition: attachment; filename=\"$name\"");
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));
readfile($file);
exit();
}
Tried to test it with different browsers, the behaviour is the same. All inspector tools show only 1 request being made on click.
What am I missing in this big ugly picture? Why could it sometimes write twice instead of only once?
Thanks for your time to read this wall of text.
I have a PHP script that uses PHPExcel to open a template, insert values from a database query then return the result to the browser. It works perfectly in Firefox, but in Internet Explorer (8), when it attempts to open the file, it breaks with:
Internet Explorer cannot download generate.php from my.domain.
Internet Explorer was not able to open this Internet site. The
requested site is either unavailable or cannot be found. Please try
again later.
The code that I'm using (generate.php) is as follows:
// Open template
$xlRead = PHPExcel_IOFactory::createReader('Excel5');
$xl = $xlRead->load('Template.xls');
$xl->setActiveSheetIndex(0);
// Write data
$xl->getActiveSheet()->fromArray($dbOutput, null, 'A1');
// Output to browser
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename=MyReport.xls');
$xlWrite = PHPExcel_IOFactory::createWriter($xl, 'Excel5');
$xlWrite->save('php://output')
EDIT Seems like this problem only affects IE when behind SSL. As such, this is an identical problem to several similar SO questions. People's advice is to tweak the headers, but so far, no combination of what I've seen as solutions has worked...
If none of the above works for you, and you would still like to run under SSL, you could always write the file to disk and provide a download link, or you could prompt the user for an email address and email the file as an attachment. If you go the email route, PHPMailer has a fairly easy way of sending attachments. I'm not 100% for sure, but I think the file needs to be written to disk before it can be attached using PHPMailer, but you can always unlink the file immediately after send.
It all comes down to IE's handling of the headers you set. Microsoft's own answer to this known problem should resolve things for you.
Summary of their solution: "remove the no-cache header or headers."
I need to know if a user selected download then clicked the cancel button, which is not the same as readfile having an error. I have inspected the count returned by the readfile function, but it shows the bytes in the file even if the user canceled the download from the Save As dialog.
The reason this is needed is because my site has a one-time download, where a member gives permission for another use to download their file one time, then the permission goes away. But if a member clicks the download button then decides not to download it right then, I dont' want my database to get updated to show they got the file.
This deals with intellectual property protection since the files are the property of the member who uploaded them, and I need to keep an audit trail of exactly what other members downloaded the file in case they start floating around the internet. But if the readfile function always reflects the filesize (meaning those bytes were transferred in some way), I have no way to know if the file was actually downloaded.
I have seen a number of posts about this subject, but no real solutions to what has to be a frequent need - did they download it or not? Just knowing that they clicked the download button doesn't really say whether they decided to go through with it since the Save As dialog box allows someone to cancel the actual completion of the download.
For completeness, here is my download code up until the readfile function:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=$download_name");
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("sub/$doc_file"));
ob_clean();
flush();
$wasdownloaded = readfile("sub/$doc_file");
I fear the correct answer is "Impossible" - let me explain: You might be able to correctly figure out, when the file has crossed the wire, but you can't figure out reliably, whether the client threw it away or not.
Example (chronological sequence):
A user on MSIE clicks download and is presented with the "Save where" Dialog.
While this dialog is open, the download is started in the background.
The user navigates around in the dialog or simply does nothing (phone rang, he talks)
The background download is finished, your script sees the download as complete
The user clicks on "cancel"
MSIE deletes the tempfile, the download is never stored in a user-accessible form
Result:
The user sees the file as "not downloaded" - and he is correct
Your app sees the file as "correctly downloaded" - and it is correct
You would first need ignore_user_abort().
This would allow your script to continue on after the user has hit cancel, or escape.
You would then have to print out the file and continuously check with connection_aborted().
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=$download_name");
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("sub/$doc_file"));
ob_clean();
flush();
$fp=fopen("sub/$doc_file","rb");
while(!feof($fp))
{
print(fread($fp,1024*8));
flush();
ob_flush();
if( connection_aborted() )
{
//do code for handling aborts
}
}
Use this comment on php.net : http://www.php.net/manual/en/function.fread.php#72716
On fclose you would be able to determine if file has been downloaded successful, because your are checking if user aborted connection with connection_status()