Run unix command in php

In php, I need to find if a directory exists. IF it does not exist (I will show a hyperlink for this using the name dirname)

Here is an example where I need help.
dir_name is the name of the directory

$url = system(~'ls -d /home/myapps/rel/".$dir_name"');
        echo $url;(this does not work)
        if(preg_match("/No such/",$url)) {
                echo'Ther is no match'
        }
        else{
        }

In my code, the if block is never executed. (it should be executed if the directory does not exist); (

+3
source share
3 answers

Why aren't you using is_dir()?

http://php.net/manual/en/function.is-dir.php

+8
source

As others said, the is_dirright way.

I will indicate why your existing program is not working. This will be useful in cases where you want to run an external command and then analyze its output.

  • ~ system.
  • . - :

    system('ls -d /home/myapps/rel/'.$dir_name);

    system("ls -d /home/myapps/rel/$dir_name");

  • , , ls "....not found" stderr, stdout, stdout. stderr of stdout :

    system("ls -d /home/myapps/rel/$dir_name 2>&1");

  • , system last line. , , / system , exec, . - :

    exec("ls -d /home/myapps/rel/$dir_name 2>&1",$output_arr);
    err $output_arr

, ( ) $url, "No such".

:

  • bash , . /foo/bar not found, .
  • , $dir_name No such ( ) .
+4

http://php.net/manual/en/function.file-exists.php

bool file_exists  ( string $filename  )

Returns true if a file or directory exists. If you then need to find out if its directory or file is usingis_dir()

+2
source

Source: https://habr.com/ru/post/1765945/


All Articles