PHP - in-data from AJAX call will not work in SYSTEM()
Posted: Wed Jun 25, 2014 2:58 pm
This is driving me crazy!! Below are two scripts both use system() to: 1. unzip a data file and 2. to restore a MySql database. I have given all data files 777 priveleges so no permission problems. Here are the two scripts. In the case of the called script all "echo's" are commented out except for the last one (it being the return to AJAX sucess).
(I can't find anywhere what return values of system() are. )
Run from command line: Return values 0 and 0.
This works fine.
Called from AJAX script inside .html program: Return values 1, 2
This does NOT work.
(I can't find anywhere what return values of system() are. )
Run from command line: Return values 0 and 0.
This works fine.
Code: Select all
<?php
// Test program to exercise system() function
set_include_path( '../include' );
error_reporting (E_ALL ^ E_NOTICE);
//$myBakup=trim($_POST['bakup']); // bakup file to restore - WILL NOT WORK IF INPUT FROM AJAX CALL IN SCRIPT
//$myDbase=trim($_POST['dbase']); // Mysql dbase - WILL NOT WORK IF INPUT FROM AJAX CALL IN SCRIPT
$myDbase="pizzidb"; // MySql database to restore
$hostname=gethostname();
$usr=substr($hostname, 0,strpos($hostname,"-") ); // whose computer is it?
$myDir="/home/".$usr."/DB-Web/".$myDbase."/"; // create absolute dir address
chdir($myDir); // go there to access bakup files
$myBakup="PIZZIDB_DUMP_06.20.14_17.15.00.sql.gz"; // the actual file to be restored input as string here for test purposes.
// if gzip file unzip it
if(preg_match('/.gz$/', $myBakup, $newList)) {
$command ="/bin/gunzip ".$myBakup;
echo $command."\n\n";
$bak_result = system($command, $retval); // bash command
echo "bakresult\n".$retval."\n\n"; // if 0 OK
$myBakup=substr($myBakup, 0, -3); // strip ".gz" from end of string($myBakup) since we just unzipped it
}
// Now open MySql with input being our $myBakup file to restore the datebase
$command ="/usr/bin/mysql --host='localhost' --user='rick' --password='rick' < ".$myBakup; // regardless of computer mysql user is 'rick'
echo "\n".$command;
$bak_result = system($command, $retval); // bash command
echo "\nbakresult2\n\n".$retval."\n\n"; // if 0 OK
echo $myBakup; // return text to calling script (AJAX call)
?>This does NOT work.
Code: Select all
<?php
set_include_path( '../include' );
error_reporting (E_ALL ^ E_NOTICE);
// Called from DataBaseRestore.html via AJAX
$hostname=gethostname();
$usr=substr($hostname, 0,strpos($hostname,"-") ); // whose computer are we on
$myBakup=trim($_POST['bakup']); // bakup file to restore
$myDbase=trim($_POST['dbase']); // mysql database
$myDir="/home/".$usr."/DB-Web/".$myDbase."/";
chdir($myDir); // change to dir the file is in
// if gzip file unzip it
if(preg_match('/.gz$/', $myBakup, $newList)) {
$command ="/bin/gunzip ".$myBakup;
echo "command ".$command."\n\n";
$bak_result = system($command, $retval);
echo "returnval ".$retval."\n\n";
$myBakup=substr($myBakup, 0, -3); // take ".gz" from end of $myBakup since we just unzipped it
}
// Now open mysql with input being our $myBakup file thus restoring the datebase
$command ="/usr/bin/mysql --host='localhost' --user='rick' --password='rick' < ".$myBakup; // regardless of computer mysql user is 'rick'
echo "command ".$command."\n\n";
$bak_result = system($command, $retval);
echo "returnval ".$retval."\n\n";
echo $myBakup;
?>