In php code, use shell_exec correctly in my case

Viewed 54

I have question about how to save log in correctly directory. My php code:

public static function log(test $testination) {

    echo($testination->getDetails()."\n");
    
     $log_filename = shell_exec("cd; ~/log/");
     $log_filename2 = "log";
 
    if (!file_exists($log_filename2))
    {
        mkdir($log_filename, 0777, true);
    }

    $log_file_data =$log_filename.'/logs_' . date('d-M-Y') . '.log';
    file_put_contents($log_file_data, var_dump($testination) . "\n", FILE_APPEND);

}

in this code, I create a folder in the same place as my PHP file, then create folder log if this not exist.

I want to save in begining (just cd) then create log folder if this not exist I try this code

 $log_filename = shell_exec("cd; ~/log/");

i get warning/error

PHP Warning:  file_put_contents(/logs_18-Aug-2020.log): failed to open stream: Permission denied in /test/test/Helper.php on line 318

Any idea how to do it? Please im stuck.

Any help will be appreciated.

1 Answers

That's not how you should use file_put_contents: it does not know anything about that attempt to change the directory, as this will not affect the execution of PHP. If you want to get the directory of the current PHP file, you can use basename(__DIR__)

Additionall, cd; ~/log/ won't work in your shell as well.

Related