Reputation: 1141
I am trying to use exec()
in php but looks like all my efforts are in vain !!!
Please help me : What is wrong with my code?
$cmd = "mysqldump -u root my_db table1 table2 > /home/aditi/Desktop/databasedump.sql";
$res = exec($cmd , $ot , $res);
But I don't see this file. But when I do this on console there is no problem.
Any help will be appreciated.
Upvotes: 0
Views: 85
Reputation: 15945
As @Kalai pointed out, try other simple commands to see if exec
is working at all.
sudo
as the PHP user and execute the same line from the shell to see if it works outside the PHP script. Related: https://serverfault.com/questions/252552/sudoers-syntax-to-run-shell-script-as-php-user
Upvotes: 1
Reputation: 13394
Have you checked other simple commands using exec like echo exec("pwd");. If that is not working means please check whether safe mode has on for your domain. If yes please turn off the safe mode.If that code is running please check the path and permission of the folders.
Upvotes: 1
Reputation: 21856
A few possible causes:
the password for the dump is missing: $cmd = "mysqldump -u root -pPassword my_db table1 table2 > /home/aditi/Desktop/databasedump.sql";
mysqldump is not in the path when you run a shell: $cmd = "/usr/bin/mysqldump -u root -pPassword my_db table1 table2 > /home/aditi/Desktop/databasedump.sql";
Do not forget that the webserver process runs as another user as when you run the code in a shell. Make sure the webserver user id has access to the folder for writing the dump.
Upvotes: 1
Reputation: 751
The code is correct. Make sure the user which is executing PHP file has permission to write the specified folder.
Upvotes: 1