472,368 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,368 software developers and data experts.

Save file in locaation automatically

178 100+
Hi,

I have this script for backing up a database that when it is run the option comes up to save the file. Can anyone show me how I would change this script so that the script automatically saves the file in a specified folder?

[HTML]<?php
set_time_limit(0);
$mysqldump_version="1.02";


$output_messages=array();

$mysql_host= 'localhost';
$mysql_database= '**';
$mysql_username= '**';
$mysql_password= '**';

_mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password);



//ob_start("ob_gzhandler");
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="'.$mysql_host."_".$mysql_database."_".da te('YmdHis').'.sql"');
echo "/*mysqldump.php version $mysqldump_version */\n";
_mysqldump($mysql_database);


function _mysqldump($mysql_database)
{
$sql="show tables;";
$result= mysql_query($sql);
if( $result)
{
while( $row= mysql_fetch_row($result))
{
_mysqldump_table_structure($row[0]);

_mysqldump_table_data($row[0]);

}
}
else
{
echo "/* no tables in $mysql_database */\n";
}
mysql_free_result($result);
}

function _mysqldump_table_structure($table)
{
echo "/* Table structure for table `$table` */\n";
if( isset($_REQUEST['sql_drop_table']))
{
echo "DROP TABLE IF EXISTS `$table`;\n\n";
}
if( isset($_REQUEST['sql_create_table']))
{

$sql="show create table `$table`; ";
$result=mysql_query($sql);
if( $result)
{
if($row= mysql_fetch_assoc($result))
{
echo $row['Create Table'].";\n\n";
}
}
mysql_free_result($result);
}
}

function _mysqldump_table_data($table)
{

$sql="select * from `$table`;";
$result=mysql_query($sql);
if( $result)
{
$num_rows= mysql_num_rows($result);
$num_fields= mysql_num_fields($result);

if( $num_rows > 0)
{
echo "/* dumping data for table `$table` */\n";

$field_type=array();
$i=0;
while( $i < $num_fields)
{
$meta= mysql_fetch_field($result, $i);
array_push($field_type, $meta->type);
$i++;
}

//print_r( $field_type);
echo "insert into `$table` values\n";
$index=0;
while( $row= mysql_fetch_row($result))
{
echo "(";
for( $i=0; $i < $num_fields; $i++)
{
if( is_null( $row[$i]))
echo "null";
else
{
switch( $field_type[$i])
{
case 'int':
echo $row[$i];
break;
case 'string':
case 'blob' :
default:
echo "'".mysql_real_escape_string($row[$i])."'";

}
}
if( $i < $num_fields-1)
echo ",";
}
echo ")";

if( $index < $num_rows-1)
echo ",";
else
echo ";";
echo "\n";

$index++;
}
}
}
mysql_free_result($result);
echo "\n";
}

function _mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password)
{
global $output_messages;
$link = mysql_connect($mysql_host, $mysql_username, $mysql_password);
if (!$link)
{
array_push($output_messages, 'Could not connect: ' . mysql_error());
}
else
{
array_push ($output_messages,"Connected with MySQL server:$mysql_username@$mysql_host successfully");

$db_selected = mysql_select_db($mysql_database, $link);
if (!$db_selected)
{
array_push ($output_messages,'Can\'t use $mysql_database : ' . mysql_error());
}
else
array_push ($output_messages,"Connected with MySQL database:$mysql_database successfully");
}

}
?>[/HTML]
Nov 3 '08 #1
1 1834
dlite922
1,584 Expert 1GB
A website cannot access your file system. If it was possible to do that, I could give you a website to go to and if I was an evil hacker the website would delete all the files on your computer or automatically implant a virus.

PHP can however save files on the server through the user apache (if it's a linux machine) but if you're just trying to back up your mysql database, you do not need PHP.

You can run a command from the command line, or better yet, create a cron job or scheduled task that will back up your database regularly, like so

mysqldump -uuser -ppassword databaseName > /path/to/file.sql

if you put that line into the a cron job. If you need that file on your own machine, You can FTP and download it or write a script to automatically download it every day or week for you.

Google is your best friend. Do some research and come back with questions.





Dan
Nov 3 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: john | last post by:
All, I am writing a small application that allows users to create a new HTML file automatically, by entering just a few elements (page title, body text, etc). I can create the new page and...
1
by: Tommy | last post by:
Is it possible to write asp file which create some text file and then save in local drive automatically without human interaction? I haven't any idea. Thx.
3
by: cathywigzell | last post by:
I have a web page which allows a user to download a file directly to their computer, using IE. I present a file name in a table and the user can then right click on the file name to get a popup...
3
by: Jay | last post by:
Hey there, Is there a way to have php automatically save a file that is being downloaded without having the annoying "Save As.." dialog box that appears? I would like to be able to create a txt...
5
by: Carl | last post by:
Please can anyone tell me how I can create a macro to save the results of a query as an excel file? The query is called Student List and I would like to save it to "My Documents". We have had...
4
by: Richard | last post by:
Hi I'm new to ASP/Web programming so any help would be appreciated... Situation: On my web page I would like to present a link {or button} that would allow the user to download a large file. ...
4
by: sunilj20 | last post by:
Hello, I have a requirement wherein, a user clicks on a file name in an ASP.NET web application, and the file should automatically be downloaded (Without showing the "Open", "Save As") in the...
4
by: Matt | last post by:
Hi, I would like to save a file locally (on the client computer) in an ASP.net application. It is like the server would return some data that the user can save to a file on his local PC. The ASP...
0
by: blackjackkiller | last post by:
Hi Need help in automating the report.Let me give the explanation what i need that might help u in understanding it. 1 need excel macro to run a query to pull data from sql server for specific...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.