Save Export CSV to desired location - PHP - php

I have a code like this.
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
header("Pragma: public");
Now, this gets saved in my desktop. If i wish to save the xls file in D:/xls/
then how to add the code.
Please help me.
Thanks -
Haan

If i wish to save the xls file in D:/xls/ then how to add the code.
The server can not dictate to the client where to save a file.
That is a decision only the client can make.
Imagine if the server could tell the client where to save a file. You could overwrite any file on the user's system. That'd be the biggest security breach in the history of poorly designed internet security breaches.

Related

Why is my PDF download corrupted?

I have a script that generates a pdf using fpdf, this file is saved correctly on the server on my computer. In that same php file I run the following code to download the file. When i download the pdf and I check it in notepad, everything in the pdf is the same, except for the fact that it contains a lot of my previous files html at the beginning of it. The file saved to my server doesn't have any of that.
What could cause something like this to happen? I have no idea where to look for the source of this error, can anyone point me in the direction to finding the problem?
<?php
$filename=($name.$ran.'.pdf');
$pdf->Output($name.$ran.'.pdf');
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Type: application/octetstream');
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($filename));
header("Content-disposition: attachment; filename=\"".basename($filename)."\"");
readfile($name.$ran.'.pdf');
?>
I managed to figure things out, thanks to hakre! Essentially I just needed to clear out the output buffer using ob_clean () before using readfile() and the code worked!

Force image download with php using header() in smartphones and tablets

I was searching already for a long time and I havent seen any right answer yet.
I'm trying to create a system in PHP where the user can download a signPicture that I create in JPG.
The program is working fine in all desktop computers. There is not problem at all, even for IE8.
The header that I use:
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="test.jpg"');
in the end i just stream the picture:
imagejpeg($imgSign,NULL,100);
How I said, it's working really good in every browser. But then we get to the mobile devices, where in android for example, download a test.jpg file... but then it cannot open... and the same with ipad (actually doesnt download, it show the image in the browser and than I save it... but it does not open either).
I also try more examples that I saw, but doesnt change anything, like:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Transfer-Encoding: binary ");
Any idea how to sort this out in mobile devices?
Thanks!
I got it!
There were differents problems. I found the clear solution in comments from this post:
http://www.digiblog.de/2011/04/android-and-the-download-file-headers/
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="test.JPG"');
The important steps: I send everything with a form. The form, to make it work in mobiles, needs to have the target='_top' and the method='get'
It also make errors if the extention (jpg) is not in UPPERCASE and the file name is not between " ".
Now it works in all devices that I try by far. :)
Special thanks to Jörg Wagner, author of the post.

How to control the browser progress update in the download process?

I wrote a download script in PHP as specified below, my script is downloading the files correctly, but I am feeling that the browser(chrome) progress bar is not getting updated properly in regular intervals.
My file is of size 320MB, while downloading that file the progress is getting updated randomly as "11MB, 76MB, 200Mb & 320MB" or "70MB & 320MB" etc.
In most of the sites download progress update is happening in constant chunks like after every MB, so I want to know how we can control the progress update intervals, may be by sending some extra headers or something else.
I want to improve the user experience by updating the progress in constant intervals, so anybody please help me to handle this situation in a proper way.
// HTTP Headers for ZIP File Downloads
// http://perishablepress.com/press/2010/11/17/http-headers-file-downloads/
// file variables
$filename = "Movie Tunes.zip";
$filepath = "files/";
// http headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: public,must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer"); // MIME
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary"); // MIME
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
#readfile($filepath.$filename);
Thanks,
Siva
No, you cannot influence when/how the browser updates its download progress bar.

Save pdf to local server

I am creating a PDF file from raw binary data and it's working perfectly but because of the headers that I define in my PHP file it prompts the user either to "save" the file or "open with". Is there any way that I can save the file on local server somewhere here http://localhost/pdf?
Below are the headers I have defined in my page
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/pdf");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Transfer-Encoding: binary");
If you would like to save the file on the server rather than have the visitor download it, you won't need the headers. Headers are for telling the client what you are sending them, which is in this case nothing (although you are likely displaying a page linking to you newly created PDF or something).
So, instead just use a function such as file_put_contents to store the file locally, eventually letting your web server handle file transfer and HTTP headers.
// Let's say you have a function `generate_pdf()` which creates the PDF,
// and a variable $pdf_data where the file contents are stored upon creation
$pdf_data = generate_pdf();
// And a path where the file will be created
$path = '/path/to/your/www/root/public_html/newly_created_file.pdf';
// Then just save it like this
file_put_contents( $path, $pdf_data );
// Proceed in whatever way suitable, giving the user feedback if needed
// Eg. providing a download link to http://localhost/newly_created_file.pdf
You can use output control functions. Place ob_start() at beginning of your script. At the end use ob_get_contents() and save the content to a local file.
After that you can use ob_end_clean() or ob_end_flush() depending on whether you want to output PDF to browser as well, or you would redirect user to some other page. If you use ob_end_flush() make sure you set the headers before flushing the data.

PHP_excel: Export to xlsx gets downloaded successfully but when we open it it shows warning Excel found unreadable content

i am using php_excel to export to xlsx. In my application im making use of template.
When i download, the xlsx file gets downloaded fine, but when we open its showing the following warning:
"Excel found unreadable content in 'project_report(3).xlsx'. Do you want to recover the contente of this workbook? If you trust the source of this workbook, click Yes."
If i click yes it opens the file correctly.
And one more thing is when i attach the downloaded file to the mail. And if i open it sing Google Spread sheet it says bad format unable to open.
so if anyone know the reason please suggest me to solve this.
check your headers, here are mine:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary");
$objWriter->save('php://output');
check for output (spaces before <?php forgotten echo? etc

Categories