Page 1 of 1

Save contents of a database as an XML or CSV file

Posted: Tue Jun 22, 2004 5:05 am
by canobi2004
Hi,

I would like to know how to save the contents of a database to an XML or a comma-delimited file using only PHP. Below is the basic process

1. Select required data from a table in the database
2. Format the data to the required specification e.g. XML, CSV, etc
3. Create the file
4. Prompt user for a place to save the file e.g. My Documents
5. Save the file

I believe I can do the first two but I can't figure out what I need to do to create the file with PHP, prompt user, then save.

I would really appreciate your assistance.

Posted: Tue Jun 22, 2004 8:52 am
by protokol
Creating the file is simple. You would basically use the [php_man]fopen[/php_man], [php_man]fwrite[/php_man], and [php_man]fclose[/php_man] functions to write the file. Then you use headers to prompt the user to save the file:

Code: Select all

<?php
$mime_type = shell_exec("file -bi "$filename"");
$file_size = filesize($filename);

header('Content-Type: '.$mime_type);
header('Content-Disposition: attachment; filename="'.$mime_type.'"');
header('Content-Length: '.$file_size);
@readfile($file);
exit;
?>

Posted: Tue Jun 22, 2004 12:26 pm
by feyd
removed quadruple post by protokol

Posted: Wed Jul 21, 2004 1:16 pm
by juline
hi protokol,

i tried out ur script, but it is not working for me somehow. the result is the following warning:
Warning: Cannot modify header information - headers already sent by (output started at /srv/www/htdocs/intranet/phpFunctions/constants.inc.php:61) in /srv/www/htdocs/intranet/timetablePlanning/export.php on line 105
....
what do i do wrong ?

Posted: Wed Jul 21, 2004 1:21 pm
by feyd
you have text output that started in "constants.inc.php" line 61... header calls must occur before any text output.. this includes whitespace, html, anything.

Posted: Wed Jul 21, 2004 1:33 pm
by juline
thanx :lol: