0

I am trying to make a simple program in Laravel that would allow the user to click a button and a backup SQL for their database would be generated locally.

$command = "/usr/local/mysql/bin/mysqldump --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . "  > " . storage_path() . "/" . $filename;

$returnVar = NULL;
$output = NULL;
exec($command, $output, $returnVar);

This is my current code and as you can see I have specified the entire path of mysqldump, if I don't it just returns an empty .sql. The same command (without the entire path) runs flawlessly when I run it in terminal, now this code is going to run on locally different environments (Windows, Linux, Mac) and the path to mysqldump would be different in each.

Is there a way I can make this happen without specifying the entire path?

Don't Panic
  • 13,965
  • 5
  • 32
  • 51
ashish_arma
  • 165
  • 2
  • 8

1 Answers1

0

You could use another env variable with a default to 'mysqldump'.

However, you shouldn't use env variables directly in your code, load them into config parameters in your config files. env() won't work once the config is cached.

The underlying problem, however, is that /usr/local/mysql/bin is not in your PATH variable for the web user. PHP exec $PATH variable missing elements

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
  • when you say another env variable with a default to 'mysqldump'. do you mean an env variable that would direct to mysqldump? that would defeat the purpose right? id be specifying the entire path in env, i have tried with config aswell, same thing happens, but yes ill change to config in the final code – ashish_arma Sep 07 '18 at 18:26
  • Why would it defeat the purpose? Environmental variables are where you'd specify settings that change or are unique to the environment where the app is running. – Devon Bessemer Sep 07 '18 at 18:32
  • i wish i could do it that way, but i cannot ask my user to locate the mysqldump file on their computer – ashish_arma Sep 07 '18 at 18:41
  • I'm not sure I understand your code then. Why would this run on their computer? You're already using environmental variables to define the database, if their path to mysqldump lies outside their environment's PATH, it seems logical to use one there. – Devon Bessemer Sep 07 '18 at 18:43