Save contents of a database as an XML or CSV file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
canobi2004
Forum Newbie
Posts: 1
Joined: Tue Jun 22, 2004 5:05 am

Save contents of a database as an XML or CSV file

Post 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.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post 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;
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

removed quadruple post by protokol
juline
Forum Commoner
Posts: 37
Joined: Thu Jul 15, 2004 9:05 am

Post 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 ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
juline
Forum Commoner
Posts: 37
Joined: Thu Jul 15, 2004 9:05 am

Post by juline »

thanx :lol:
Post Reply