PHP Beginner: Does standard practice use exec commands?

I am just starting php. I'm just wondering if there is a better way to do this. This displays all my scripts in the same folder as this script.

I am not sure what the standard use of the exec command. It does not seem very portable.

<html> <head> <title>My PHP Practice Scripts</title> </head> <body> <center><h1>PHP s</h1></center> <?php exec("ls -1 *.php", $output); foreach ($output as &$tmp){ echo "<a href=\"$tmp\">$tmp</a><br>"; } ?> </body> </html> 
+4
source share
4 answers

For such operations, there are directory functions: http://www.php.net/manual/en/ref.dir.php

+3
source

"exec" is a portable beacuse - it is an API! :-) What is not portable is a line representing the command line that you call through "exec".

In this case, you can use the PHP API to read the directory. This is portable for every OS you use on your server.

Ex: dir class in PHP

+1
source

Uses the standard practice of the exec command?

No. Using exec to interact with the host operating system is a very non-portable practice. For almost any situation, there is an OS-independent solution; in this particular case, you can find all the files in the current directory with glob , readdir or scandir .

Using eval in a program that takes any form of user input also often leads to serious security risks. Your program does not currently suffer from such risks, but it is also very trivial.

+1
source

Yes, there is a better way to do what you need. There is a glob or readdir for starters.

Regarding the "standard practice", you will see a lot of this in the land of PHP. If the developer does not know PHP a huge code base very well, but knows the shell, they will eventually use exec and backticks (``) everywhere to do their job. This is standard practice that some percent of PHP developers hate, and another percent cannot live without. Get used to it.

0
source

All Articles