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.
Related
im writing a basic script to download csv file based on database information,
in my dashboard/index.php i use GET and switch to include pages
so when i click on the link dashboard.php?link=export.php
i have a table with the all the data , there i have a link that i can download my csv file , my problem is that when i click to export.csv , i have an text output and not download file so i put those code :
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$filename}.csv");
header("Pragma: no-cache");
header("Expires: 0");
but always i see the content in text format and with an error
Warning: Cannot modify header information - headers already sent by (output started at /home/*/public_html/dev/dashboard/index.php:78) in /home/*/public_html/dev/dashboard/component/export.php on line 39
so i ask how can i resolve this issue , can i remove the header for the index in the export.php and set a new one also there to download the file or what extacly
maybe i need to change just in the export.php the Content-Type to be text/csv
but is alrady sent text/html .
please help to resolve this
thank you
As you can see, there is something already sent to output (printed) in your index.php file which is including your export.php file.
Make sure you are not printing anything before the headers. In some cases might be a space between the opening <?php tags or something little like that. btw mind that switch inclusion cases you have.
Other way is to try to use header_remove(); before the statements in export.php
Do not add anything before header in my case i was getting record from data base.
If you have function to download csv the add following header at top in the function like:
function exportCsv($date) {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
.
.
.
.
}
Stuck on what is likely a silly problem and only posting after reading several related threads.
Have a page with a lot going on, one of the form options I'm trying to add is so the user can select to download array results in CSV. Problem is HTML header info is coming through in addition to the CSV data I want.
Code is:
function Array2Csv($result, $filename){
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=' .$filename);
$output = fopen('php://output', 'w');
while($row = mysql_fetch_assoc($result)) {
fputcsv($output, $row,'|','"');
}
}
Problem is the result file includes BOTH undesired markup (headers and scripting references) in addition to the CSV itself. Desired output should only include the CSV data.
You have send the the header before any output was send. Disable view and layout.
See also http://php.net/manual/en/function.header.php
I have written a script using header function that generates CSV file.Let me explain in detail.
Step 1) I am saving records in mysql.Step 2) I am creating CSV file from saved records from database Step 3) I want to attach that CSV in attachments so that i could send it to different recipents!! I have completed the two steps.But i dont want download functionality in CSV file generation process as i dont want this file available to anyone. My code looks like this:
$file_name = "Register_" . date('l');
$file_name.=".csv";
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$file_name");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
Where do I need to make changes?
just remove all calls to header() and the last print
You could do what you try to by using the ob_buffer functions and 'catching' the generated output. However the whole approach is a little strange:
the header functions have nothing to do with creating emails. They serve the purpose to specify how a browser should handle transferred data. 'Cause this is what you do: you send headers and data. Don't.
Instead: write the csv data into a buffer or file and create an email using one of the available email classes (google...). Then take that crafted email and send it. No header() function required for that.
You can use AddStringAttachment:
$file_name = "Register_" . date('l') . ".csv";
$excelContent = "$header\n$data";
$phpmailer->AddStringAttachment($excelContent, $file_name, 'base64', 'application/vnd.ms-excel');
how can i make a function in php that, when a link was clicked it would download a certain cell in a mySQL table and allow the user to download it as a .txt file? When the download is complete, the file is deleted.
I don't think you'd need to create/delete any files...
Say you have a link on your page:
Download this as a txt file
I'm not sure how you're identifying the cells that the user wants to download, but I'll trust that you're doing it securely and efficiently.
Somehow, you have the table name, the row, and the column of the MySQL cell. So you just do a simple query...
$result = mysqli_query('select the cell', $your_db);
$result = mysqli_fetch_assoc($result);
$result = $result['cell_name'];
This gives you the value of that cell as a simple PHP variable. Now all you need to do is output it to the user:
header('content-type: text/plain');
echo $result;
That should print the value of the cell to the screen, as simple text. The user could then save the page. :D
Edit - Force Download
I haven't tested this, but the basic idea would be to add some more headers:
header('content-type: text/plain');
header('Content-Disposition: attachment; filename=whatever.txt');
header('Pragma: no-cache');
echo $result;
Don't create a file in the first place. Force a download, and send the text.
$text = mysql_query("SELECT content FROM texts WHERE id = $id");
$text = mysql_fetch_assoc($text);
$text = $text["content"];
header("Content-type: text/plain");
header("Content-disposition: attachment;filename=\"filename.txt\"");
header("Content-Length: ".strlen($text));
echo($text);
hello
i have tryed a lot of source codes for this but i have the same error in all of them
include 'functii.php';
starts();
opendb();
$query = "SELECT content,`title_real`,`size`,`ext` FROM file WHERE file_id = '3'";
$result = mysql_query($query) or die('Error, query failed');
list($content,$filename,$size,$ext) = mysql_fetch_array($result);
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=$filename.$ext");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$size);
echo $content;
exit;
the problem is that in the downloaded file i have a lot of "\0" that came from nowhere.
the file is well stored in the database. i tested that.
thank you
It sounds like an encoding issue. It could be that the file is encoded in UTF-16 and you are displaying it as if it were ASCII.
Try to:
header("Content-Disposition: attachment; filename = $filename . '.' . $ext");
instead of:
header("Content-Disposition: attachment; filename=$filename.$ext");
3 things :
As Alexander.Plutov said, you got an error in the filename, it should be $filename.'.'.$ext You're missing the "." between the filename and its extension.
As Mark Byers said, it may seem like an encoding issue, you should check your PHP file encoding as well as your DB's one.
Hope you're only stocking text based file, because, any other file content definitely shouldn't be stored in a database! And even for text based file, it shouldn't be used for that, it's not its purpose at all.
In any other case than file generation, files should be stored in your server and may be called from your DB thanks to file path and name.