cakePHP force download of ical - php

I have the following in a layout file;
<?php
$Filename = "TheEvent.vcs";
header("Content-Type: application/force-download");
header("Content-Disposition: inline; filename=$Filename");
echo $content_for_layout;
?>
Now in the controller I have this layout defined and it is working as I can see the text of my ical file. The only problem is the download isn't being forced on the client.
I followed the following guide though I am using cakePHP 2.0
http://labs.iamkoa.net/2007/09/07/create-downloadable-ical-events-via-cake/
Thanks
Ash

you can use the new 2.0 request features to download right away:
$this->response->download($filename);
http://www.dereuromark.de/2011/11/21/serving-views-as-files-in-cake2/
You can use file() and the other documented ways of serving files - see http://book.cakephp.org/2.0/en/controllers/request-response.html#cakeresponse

This is because the "Content-Type: application/force-download" is used and not recognized by your HTTPd server.
Thus your php script is an unknown type of content and your browser got the signal to download the file and save it :)

The accepted answer got me Method download does not exist.
Here's what worked for me:
$this->response->download($filename);
Note request replaced with response.
Source: class-CakeResponse.html.

Related

md5 check sum does not match when downloading file

I cannot seem to figure this issue out. I'm downloading a file (Moodle .mbz extension) using PHP with HTTP headers. The file is first downloaded from Amazon S3 to a server using the aws php sdk, which works fine. The issue is downloading the file from the server to another workstation. No matter what I seem to change, the md5 checksums for the file on the workstation and the file on the server do not match. Clients cannot restore the .mbz file they have downloaded to their workstation. It seems something is happening to the file to change it in some way but I cannot figure out what.
I have referenced:
1. this tutorial
2. This similar SO question
and various other resources via google. I'm desperate, please help if you can. I have ensured compression is off in the httpd.conf file. Turned debugging completely off in php.ini. I can replicate the issue in both my development and staging environment.
Here's the code for the direct download:
<?php
if(!file_exists($filename))
{
// file does not exist
die('file '.$filename.' not found');
} else
{
$this->load->model('Job_model', 'job');
$save_as = $this->job->get_file_name_from_key($filename);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$save_as\"");
header("Expires: 0");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . filesize($filename));
readfile($filename);
}
Using PHP with codeigniter framework, if that's relevant.
Thank you!
Edit: An octal dump of the first 16 bytes shows that 4 spaces are being appended to the beginning of the file downloaded using the above code vs the file on the server.
This is not so much an answer as a suggestion for an experiment.
I'm wondering if there is a mime problem. I found this file which, on line 145, describes the mime type for 'mbz' as 'application/vnd.moodle.backup'. Maybe by changing the Content-Type header, you can get better results.
As I understand it, 'mbz' is basically a zip file. So try adding the following to the bottom of /application/config/mimes.php.
'mbz' => array('application/vnd.moodle.backup', 'application/zip'),
If you want to cover all the bases you could expand the definition to
'mbz' => array('application/vnd.moodle.backup', 'application/x-zip', 'application/zip', 'application/x-zip-compressed', 'application/s-compressed', 'multipart/x-zip'),
Which takes all the possibilites for a zip type and makes the "default" 'application/vnd.moodle.backup'. I'd try the simpler version first.
Try this header
header("Content-Type: application/vnd.moodle.backup");
So I found this question, specifically the answer from MrPanda. I went through all the helpers and libraries (that I had written) I called/initialized in my controller and deleted any white space after the closing php tag. Problem solved. Thank you to the users who tried helping me! Two days of frustration finally come to a close.

Wrong Response Body while trying to download photo using Retrofit2

I'm using Retrofit 2.0.0-beta2 and I need to download some files from my PHP server. My first approach which worked was to directly use the GET method from its relative server path and I was getting the correct bytes.
Now I've tried something more secure that delivers the file to me based on some checks. It automatically fetches the file path from the DB and checks if the user session is correct. This works in browser tests, both Chrome PC and Chrome from Android correctly download some photos.
I'm serving the file using the X-Sendfile header like so:
header("X-Sendfile: $file_name");
header("Content-type: image/jpeg");
header('Content-Disposition: attachment; filename="' . basename($file_name) . '"');
The Android-side call looks like this:
#Streaming
#GET("/card/download")
Call<ResponseBody> getCard(#Query("filename") String filename);
All I'm getting when opening the files is the echoed text response from server. Is there any way I can receive the "correct" files?
Apparently there was some sort of problem installing the mod.
I also updated OkHttp to version 2.7.0

Generated xml file is harmful for computer?

I did an xml file and force download it by these headers:
header('Content-disposition: attachment; filename="export.xml"');
header('Content-type: application/xml; charset=utf8');
readfile('export.xml');
But before the download I see a dialog that this file can be harmful for my computer? How to get rid of this dialog? Maybe my headers is wrong?
upd Well, can do nothing, I did a test on my test-hosting, u can check it here: site with generation link, and an xml file as is: export.xml
Try changing application/xml to text/xml. Probably your browser thinks that application means executable.
Try this :
<?php
header('Content-disposition: attachment; filename="export.xml"');
header('Content-type: "text/xml"; charset="utf8"');
readfile('export.xml');
?>
Note: This does not solve your issue, however it did solve an issue I had on my computer giving that notice (windows, chrome, apache webserver, PHP 5.4.10). I leave it here for future visitors.
Some browsers do not only look for the headers but also for the "filename" in the URL.
For example if you download a PHP file that contains XML, the browser might identify it as a dangerous file (because it can be executed on your system or is not within some whitelist or what not):
http://example.com/xml-download.php
A simple solution is to make this file not end with .php any longer, for example by adding a ?:
http://example.com/xml-download.php?
And continue with that to even signal the filename that way:
http://example.com/xml-download.php?export.xml
(the last one is not necessary but can be useful especially with some older browsers)

Force file download of unknown file type with PHP

I am looking to force the download of a file from an external source using PHP headers. However, the file type could be ANYTHING. How can I force the download of all file types?
I hope you can understand my question and what I am trying to describe.
You mean HTTP headers, and you're looking for RFC 2616, http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html "Content Disposition".
Send the following headers to force a download of ALL files and types:
header('Content-Disposition: attachment; filename="name.ext"');
header('Content-Type: application/octet-stream'); // or application/force-download
echo $the_file_content;
exit;

Google Chrome renames file .xml to .download

i have this very simple download page to get an xml file.
the script works ok in firefox/IE. but chrome renames the extension of the file to ".download".
and this happens only to .xml, when you use another extension like .txt it does it without problems.
the body of the html is this:
<body>
descarga
</body>
and the php is this:
header('Content-type: "text/xml"; charset="utf8"');
header('Content-disposition: attachment; filename="example.xml"');
echo "that's it";
its very strange. any solution for this??
This is not a definite answer, just some information for you.
From the bug report:
The downloaded file may get a different name if it is considered potentially dangerous
for your computer (e.g. exe). You should then get an UI prompt in the download shelf
asking you to confirm the download (with the file still downloading in the background).
try removing 'echo "that's it";'
it makes the xml invalid and might confuse the browser.
if it doesn't help, check the actual http headers of both request and response.

Categories