WomenInTech
WomenInTech

Reputation: 1141

EXEC function not working in desired manner

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

Answers (4)

Halil Özgür
Halil Özgür

Reputation: 15945

  1. As @Kalai pointed out, try other simple commands to see if exec is working at all.

  2. 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

Kalaiyarasan
Kalaiyarasan

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

JvdBerg
JvdBerg

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

Meraj Rasool
Meraj Rasool

Reputation: 751

The code is correct. Make sure the user which is executing PHP file has permission to write the specified folder.

Upvotes: 1

Related Questions