symfony download a file - php

i have a project with symfony 1.4, in an action a export a mysql table to a .csv file and next download it from the server, i'm using Wampserver for local developement, and everything is working ok, but, when i uploaded to the production server, now, if you try to export the data, the .csv file is automatically open in the browser without asking if you want to saved or open it, the code i use after creating the .csv is this
$this->setLayout(false);
sfConfig::set('sf_web_debug', false);
$this->getResponse()->setHttpHeader('Content-Type', 'text/csv');
$this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="file.csv"');
$this->getResponse()->setHttpHeader('Pragma', 'no-cache');
$this->getResponse()->setHttpHeader('Expires', '0');
readfile(sfConfig::get('sf_upload_dir') . "/export/file.csv");
return sfView::NONE;
i repeat, in my local server this work ok, the browser give me the options of open or save, now, is this a problem of the code, a setting in the server or anything else??
the file is saved with utf-8 encode, is this the problem??
thanks

Thanks to the comments, i could get it work, what i did was change the content-type as #j0k suggested, and redirect to the file instead of using the readfile function
$this->getResponse()->setHttpHeader('Content-Type', 'application/csv');
$this->redirect('/uploads/export/file.csv');
now it is working
thanks

Another way of doing it without redirecting is as follows:
$this->getResponse()->clearHttpHeaders();
$this->getResponse()->setHttpHeader('Content-Description','File Transfer');
$this->getResponse()->setHttpHeader('Content-Type', 'application/csv');
$this->getResponse()->setHttpHeader('Content-Disposition','attachment;filename='.$file_name);
$this->getResponse()->setHttpHeader('Pragma', '');
$this->getResponse()->setHttpHeader('Cache-Control', '');
$this->getResponse()->sendHttpHeaders();
$this->renderText($fwrite('php://output',$stuff));
Where you save the file or write the file to 'php://output'.

Related

Downloading a PDF in chrome intermittently tries to save page instead of PDF

I'm outputting a PDF via PHP with the following code. $file is an object that contains data pertaining to the file being displayed.
header('Content-type: application/pdf');
header('Content-disposition: inline; filename="'.$file->name.'"');
#readfile($file->ServerPath());
My issue is that when I go to download the file in Chrome it will occasionally try to save the page instead of the PDF.
For example, say the URL that is displaying the file is mywebsite.com/file?file_id=1234. Most of the time it will try to save the file correctly as "file_name.pdf". However, sometimes chrome will try to save the file as "file" with no extension. This seems to happen randomly.
If it makes any difference the page displaying the file is being opened in a new tab. The issue happens regardless of whether I redirect via PHP or Javascript.
I really need to resolve this issue, as these PDFs will be accessible by users.
Thanks in advance.

Issues with exporting html to excel

For exporting our reports, we set the header to 'Content-Disposition: attachment;filename=fileName.xls' and dump the html to the output buffer and it downloaded the excel file nice and clean.
This was used to work until recently and now the downloaded file does not open in Excel. It doesn't give any error, just shows the blank gray screen similar to the screen when you close all the worksheets.
Strange enough, if you just open the file in Notepad++, add a space and save it, Excel WILL open it.
I suspect it to be something to do w/ the encoding of the file which is not acceptable to excel when the file is downloaded but something changes after you edit it in Notepad++ and excel would open it after the edit.
I have tried using ob_clean(), trim and converted the content to utf8 before dumping but nothing seems to work, what am I doing wrong?
Here is a sample code that recreates the issue I have:
ob_clean();
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=filename.xls");
header('Cache-Control: max-age=0');
header_remove('Pragma');
// simple html table
echo '<table><thead><tr><td>header 1</td><td>header 2</td></tr></thead><tbody><tr><td>content 1</td><td>content 2</td></tr></tbody></table>';
The above code snippet downloads the file 'filename.xls', excel initially does not open it, but after editing it the way I described earlier, excel WILL open it.
Thanks.
I had the exact same problem, then searching i found this!
"The Excel team has made a change in the behavior of certain file types to increase security." On: https://support.microsoft.com/en-us/kb/3181507
You can try one of the possible Solutions/Workaround.
The problem is the exel is blocking the unsafe document which downloads from internet.
To avoid this, you have change some settings in excel.
Options -> Trust Center -> Trust Center Settings -> Protacted View
Untick the Enable Protected View for files originated from the internet.
Save it
That's it.
You can open the files.

How to download a file without an extension in PHP

I have a file with no extension on it, but I know it's a tiff. I want to be able to download this file via PHP.
I created a page with a link to another php page, which has the following content:
<?php
$imgPath = 'http://server/23700-b074137f-eb5c-45d6-87c2-13c96812345b';
header("Content-disposition: attachment; filename=invoice.tiff");
header("Content-type: image/tiff");
readfile($imgPath);
?>
When I click the link, I get a prompt to download invoice.tiff, but it's 0 bytes.
However, if I rename the file on the server to 23700-b074137f-eb5c-45d6-87c2-13c96812345b.tiff (and change the $imgPath), it works.
How do I accomplish this without renaming the file to include the extension?
It's possible the 'tiff' extension is registered as a known file type on the server, so when you rename and request the tiff it's permissions will allow you to open it. However, with no extension, the security is probably stopping you from reading it, as mentioned by 'Mike B' above. To check this try just entering the file name in your browser address bar and see if it opens, both with and without the 'tiff' extension. There is no workaround for getting past the security issue, short of changing the severs security which would be very bad.
You are retrieving the file from a URL, therefore activating the 'fopen wrappers' in readfile. In general, you should not do this, especially when working locally since it invokes a lot of unnecessary overhead and (in this case) unwanted 'magic' behaviour.
Just use readfile on the local path to the file, and it'll be fine, or use die(file_get_contents($imgPath)) instead of the last line to circumvent PHP's native behaviour.
It works for me:
$imgPath = 'http://server/23700-b074137f-eb5c-45d6-87c2-13c96812345b';
$f = fopen($imgPath, "r");
header("Content-disposition: attachment; filename=invoice.tiff");
header("Content-type: image/tiff");
fpassthru($f);
You should also add the content-length header like so:
// untested code
header('Content-Length: '.strlen(stream_get_contents($imgPath)));

Link to a PDF in html, the file has no extension, but I know it is pdf how to make it open appropriately

First post. I'm working on a project for a client where they have pdf files uploaded to a file structure (LAMP Stack) but the files have no extensions on them. Under the assumption that those files have to be PDF how would I get the browsers to understand that, and open them accordingly? Obviously with adding the file extensions this would suddenly work but I can't change the way their system works, it would result in too many changes and they are on a tight deadline. As for saving a temporary copy somewhere, I could do that, but I was hoping for a better solution. Is there a way to suggest to the browsers that they open a file a certain way?
Any thoughts guys/gals?
You just set the application type and file name in the headers, like so:
// This points to the file in question, note that it doesn't
// care whether it has an extension on the name or not.
$filePathOnDisk = '/path/to/your/pdffile';
// You can make this whatever you like, it doesn't have to
// be the same as the file name on the disk! This is the name of the file your end
// user will see when they are asked if they want to save. open, etc in the browser.
$fileName = 'file.pdf';
$data = file_get_contents($filePathOnDisk);
header("Content-type: application/pdf");
header("Content-disposition: attachment;filename=$fileName");
echo $data;
See PHP: stream remote pdf to client browser and Proper MIME media type for PDF files for reference as well.
Tested
You can use the following which will prompt the user to save the (PDF) file on their computer.
Notice the different file names.
One is the file that will be uploaded/prompted to the user download_example.pdf, while the other is the file without an extension as set in readfile('example');
<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="download_example.pdf"');
readfile('example');
?>

Php Download script isn't working

I have a script which uploads files into an online directory and stores the file details in a database. The files when stored are renamed to the id of the entry in the database. Whenever a user requests a download, a simple SQL statement retrieves the file details from the database, the contents of the file are read from the database, and the file is prompted for download. The following is my code:
$one_file = $FILE_OBJECT->get($_GET['id']); // this is an object which just grabs the file details from the database
header("Content-type: ".$one_file['type']); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$one_file["filename"]."\""); // use 'attachment' to force a download
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$one_file["filename"]."\"");
readfile(_config('files_path').$_GET['id']);// reading the actual raw file stored in my online directory
Problem is that Im testing using a word document and its uploading perfectly - I've even checked the raw file being uploaded by manually changing its extension and it's uploading perfectly. The problem is that when it's downloaded using the code above, the Word file seems corrupted or something, because when I try to open it, it's all mumbled and jumbled. What's happening? I've used this snippet on a few other sites I've worked on, and they work perfectly fine... Help please!
By default PHP's header function will replace previous headers with the same name, so your first two headers are being overwritten by the second two. Delete the second two and see if that works.
See if this helps:
Webkit and Excel file(PHPexcel)
I was having the same problem: every time I downloaded a file, it was supposedly "corrupt". Turns out I had made a stupid directory path mistake, but the php error was being written into the downloaded file. Which, of course, made it "corrupt".
Actually I solved by reading Ian Wetherbee's comment about testing with a plain text file. Thanks Ian!

Categories