PHP - in-data from AJAX call will not work in SYSTEM()

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
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

PHP - in-data from AJAX call will not work in SYSTEM()

Post by pizzipie »

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.

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)
                                           
?>
Called from AJAX script inside .html program: Return values 1, 2
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;
                                           
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP - in-data from AJAX call will not work in SYSTEM()

Post by requinix »

Command-line PHP does not always behave the same as PHP run from a web server.

Try first with the proper error reporting settings:

Code: Select all

error_reporting(-1);
ini_set("display_errors", true);
Anything less may cause PHP to hide important error messages.
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: PHP - in-data from AJAX call will not work in SYSTEM()

Post by pizzipie »

Thanks for the reply,

I changed my PHP.INI file and now am seeing permission errors. I changed my sql,gz files so they all have 777 permissions. I am wondering if I should add my user name to the www-data group.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP - in-data from AJAX call will not work in SYSTEM()

Post by requinix »

First, change all the files back to 0644. That means read/write by the owner and only read by everyone else, and that's all that should be necessary. 99% of permissions issues come from trying to write to a directory by a different user than the owner, and even that has better solutions than changing permissions.

Now, what are the errors?
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: PHP - in-data from AJAX call will not work in SYSTEM()

Post by pizzipie »

Here are the current errors. I chmod 644 all datafiles. In this case I am trying to create a bakup file with similar results as restoring a bakup file. I neede more data files to test so tried the bakup program. Please note these programs work well on my computer. I'm trying to add this stuff to my wifes computer. That is where I'm having all the trouble.

[text]polly@polly-Inspiron-1525:~/DB-Web/pizzidb$ ls -l PIZ*
-rw-r--r-- 1 polly polly 1366 Jun 20 17:15 PIZZIDB_DUMP_06.20.14_17.15.00.sql.gz
-rw-r--r-- 1 polly polly 1366 Jun 20 17:23 PIZZIDB_DUMP_06.20.14_17.23.50.sql.gz
-rw-r--r-- 1 polly polly 1365 Jun 21 09:06 PIZZIDB_DUMP_06.21.14_9.06.01.sql.gz
-rw-r--r-- 1 polly polly 1364 Jun 25 20:20 PIZZIDB_DUMP_06.25.14_20.19.17.sql.gz
-rw-rw-r-- 1 polly polly 366 Jun 26 13:30 PIZZIDB_DUMP_06.26.14_13.29.23.sql.gz
-rw-rw-r-- 1 polly polly 545 Jun 26 13:32 PIZZIDB_DUMP_06.26.14_13.31.53.sql.gz
polly@polly-Inspiron-1525:~/DB-Web/pizzidb$ tail /var/log/apache2/error.log
sh: 1: cannot create /home/polly/DB-Web/pizzidb/PIZZIDB_DUMP_06.26.14_13.16.02.sql.gz: Permission denied
mysqldump: Got error: 1049: Unknown database 'pizzidb' when selecting the database
sh: 1: cannot create /home/polly/DB-Web/pizzidb/PIZZIDB_DUMP_06.26.14_13.22.50.sql.gz: Permission denied
mysqldump: Got error: 1049: Unknown database 'pizzidb' when selecting the database
sh: 1: cannot create /home/polly/DB-Web/pizzidb/PIZZIDB_DUMP_06.26.14_13.29.23.sql.gz: Permission denied
mysqldump: Got error: 1049: Unknown database 'pizzidb' when selecting the database
sh: 1: cannot create /home/polly/DB-Web/pizzidb/PIZZIDB_DUMP_06.26.14_13.31.53.sql.gz: Permission denied
mysqldump: Got errno 32 on write
gzip: PIZZIDB_DUMP_06.26.14_13.29.23.sql: Permission denied
sh: 1: cannot open PIZZIDB_DUMP_06.26.14_13.29.23.sql: No such file
polly@polly-Inspiron-1525:~/DB-Web/pizzidb$[/text]
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP - in-data from AJAX call will not work in SYSTEM()

Post by requinix »

Apache is running as a different user than "polly", which means the mysqldump and other commands being executed will run as that user. Which doesn't have write permissions to the pizzidb directory.

chmod 0777 pizzidb.
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: PHP - in-data from AJAX call will not work in SYSTEM()

Post by pizzipie »

[SOLVED} :D :D

Thanks requinex,

That fixed it.

So simple for causing me all that grief.

R
Post Reply