Generate 3 CSV files from 1 - php

I have one CSV file. From which i want to create 3 different CSV file which diff name , headers and data.
Lets suppose i have data.csv with headers (data1,data2,data3,data4).
Now i want to create 2 different CSV based on data.csv. Both may name "data-next.csv" & "data-next-next.csv".
When i am trying to set html headers (for assigning name) then its only setting for onetime.
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="data.csv"');
Is there any other way around.

Related

Generate a CSV file from XML stream for MYSQL Import

I am trying to get data from a online XML file and save in CSV format. I was able to save the CSV file but the data is not saved properly as a comma separated CSV and due to which the data has no delimiter and is not getting imported in mysql properly. It imports in mysql in one column itself where there are 8 columns and it should be imported in different columns as per CSV data.
I am using
$report = stream_get_contents($request->getReport());
// output headers so that the file is downloaded rather than displayed
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="data.csv"');
// do not cache the file
header('Pragma: no-cache');
header('Expires: 0');
$file = fopen('data.csv','w+');
fwrite($file,$report);
fclose($file);
You are writing the lines without formatting them properly first. You should use fputcsv instead of fwrite.

PHP Postgtres Generate CSV File for Download

I have a postgresql database table and using PHP for the backend. On the user interface, I provide users with a way of generating reports. What I want to do is that when a user wishes to generate a report, a CSV file should be provided for download.
I already know how to generate a CSV file for results of a query, but now in this case I don't want the file to be saved on disk. Instead, it should be downloaded by the browser.
You simply need to provide a page for download...
#downloadCSV.php?reportID=1
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="yourfile.csv"');
// do your postgresql query and put the result into $csv
echo $csv;
In this way you don't have to create a real file on your server
The second header Content-Disposition: attachment; forces the browser to download a file instead to show the content in a page

Rename a file with PHP right before it downloads without saving it to the server?

I have an Adobe Illustrator file (AI) that we currently have a link to on a website which then downloads the file to your computer.
The link looks something like this...
http://domain.com/crm/index.php?entryPoint=fileupload_download&id=22440435-e8ee-bd6f-7612-533b2cd7690f&field=fuaifile_c&type=D1_Designs
What I need to do is rename this file as it downloads.
So I am asking if it is possible to pass this download through another PHP file right before it downloads which would allow me to change the filename on the fly that the user downloads. I cannot change the filename on the server but when it downloads I would like to be able to add some ID numbers to the filename on the fly if this is possibble? Any ideas how to accomplish this without having to resave the image on the server with a new name?
What you are looking for is the Content-Disposition header, as specified in RFC 2183:
Content-Disposition: attachment; filename=example.ai
You can set this header using the PHP header() function.
It's ugly, and assumes these aren't "large" files that would exceed your memory_limit, but
$data = file_get_contents($original_url);
header('Content-disposition: attachment; filename="new name with id numbers');
header("Content-type: application/octet-stream");
echo $data;
You could always enhance this to do byte serving - suck 10k from original url, spit out 10k to user, etc...
Just set the Content-Disposition:
header('Content-Disposition: attachment; filename="downloaded.pdf"');
(Example taken from PHP docs: http://www.php.net/manual/en/function.header.php).
Adding id:
$id = generateIdFromSomewhere();
header('Content-Disposition: attachment; filename="downloaded'.$id.'.pdf"');

Download with smartphone generate xls file with PHPExcel

I have a big problem when I want to download on my smart phone a xls file generated by PHPExcel it me download a php file named my page, while on my pc it load properly.
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition:inline;filename=fichie.xls');
$writer->save('php://output');
exit();
There's two problems with your line that sets the content disposition.
It should have spaces between the words, which is a standard requirement for headers.
It should be attachment if you want the file to download, as opposed to display inline.
e.g.
header('Content-Disposition: attachment; filename=fichie.xls');

My PHP generated CSV file is saving as a PHP file

I've learned how to create CSV files from MySQL data from another StackOverflow question. My problem is, for some reason when I call this code, it tries to save a file called index.php (which is the current page). Inside the index.php file my data from the table is there, separated by commas. I'm guessing I have a small typo somewhere, but after playing with the code I cannot find it. Thanks to anyone who can help.
$result=mysql_query("SELECT * from tbl_email");
if(mysql_num_rows($result)) {
header ("Content-type: application/csv Content-Disposition:\"inline; filename=messages.csv\"");
echo "REF #,Company,Name,Email,Message,Date\n";
while($row = mysql_fetch_row($result)) {
$companyname = mysql_query("SELECT company FROM tbl_users WHERE user_id ='$row[1]'");
$datname = mysql_fetch_array($companyname);
echo"$row[7],$datname[company],$row[2],$row[4],$row[5],$row[6]\n";
}
die();
}
You need multiple header() calls rather than one call which supplies multiple headers on a single line, and I believe the most appropriate mime type for a CSV is text/csv.
header("Content-type: text/csv");
header("Content-Disposition: inline; filename=messages.csv");
And more commonly, we would use Content-Disposition: attachment to force a download.
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=messages.csv");
It should be:
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="messages.csv"');
Notice that the value for filename is not encased correctly with double quotes. Try to use single quotes in php, this will save you alot of trouble. ;)
Have a look at http://www.techcoil.com/blog/php-codes-to-tell-browsers-to-open-the-download-dialog-box-for-users-to-download-a-file/ to learn more about telling browser to download your file.

Categories