TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-14-2008, 09:27 PM   #1 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default Backing up a Database NOT via PHPMyAdmin

Hello.

I would like to be able to back up parts of my database by providing a form / link which will give the admin the choice to download the file in a sql format (like in IPB).

I have researched this a little; and been succesful in being able to backup the database into a location on the server where I define, but I'm not sure how I can change it into being download-able (obviously if it is downloaded I don't want it on the server anymore...).

This is what I have so far (I have commented it):

PHP Code:
<?php
include 'conf_global.php'// File with database settings

$tableName  'a_table'// The table which the admin has chosen to backup (Static for exemplory reasons)
$backupFile 'backup/DATE_TABLE.sql'// Where the sql is places
$query      "SELECT * INTO OUTFILE '$backupFile' FROM $tableName"// Back up table into file
$result mysql_query($query); // Query it

if ($result){
    echo 
'Database Backed Up';    // If succesful
}
else {
    echo 
'Database NOT Backed Up'// If not..
}
?>
Can someone help me please?
Much appreciated.
Gareth
Gareth is offline  
Reply With Quote
Old 01-14-2008, 10:01 PM   #2 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Hi Gareth,

Completely un-tested, but something like the following should do the trick

PHP Code:
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="backup.sql"');
readfile($backupFile); 
That should popup a download dialouge (default name of backup.sql - might want to change that to include the date or something).

Alan.

Last edited by Alan @ CIT : 01-14-2008 at 10:04 PM. Reason: Added more info
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-14-2008, 10:18 PM   #3 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

That should work.
__________________

Village Idiot is offline  
Reply With Quote
Old 01-14-2008, 10:39 PM   #4 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

Thanks! Should this go at the top?
Gareth is offline  
Reply With Quote
Old 01-14-2008, 10:58 PM   #5 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

That needs to be present after the query is known to be successful; there shouldn't be any output up to or past that point.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:22 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design