Jump to content

exec() Question


Fadion

Recommended Posts

I am making a simple php script using exec() to execute commands in my linux host. When i execute 'ls' it shows the right content on that directory, but what i want to know if there is a way i can distinguish between folders and files. In this way i can lets say make folders show in black and files in grey. Also when i execute a wrong command i wanna get any system error message. If these are possible it would be great if someone helped me.

 

The actual code im using, just to give an idea:

 

if(array_key_exists('command', $_POST) and strlen($_POST['command']) > 0){
$command = $_POST['command'];
exec($command, $arr);
foreach($arr as $value){
	echo $value . "<br />";
}
}

Link to comment
https://forums.phpfreaks.com/topic/62498-exec-question/
Share on other sites

You can use is_file in your foreach statements

 

if(array_key_exists('command', $_POST) and strlen($_POST['command']) > 0){
    $command = $_POST['command'];
    exec($command, $arr);
    foreach($arr as $value){
        if (is_file($value)) {
            echo "<h1>$value</h1>";
        }
        else {
            echo "$value<br />";
        }
    }
}

Link to comment
https://forums.phpfreaks.com/topic/62498-exec-question/#findComment-311083
Share on other sites

is there some particular reason you are executing linux commands instead of using php file handling functions?  they could do what you want to do, but maybe you have a good reason...maybe you can do some kind of explode on the result of the exec command, but I dont' know if you are looking for a purely linux solution here.

Link to comment
https://forums.phpfreaks.com/topic/62498-exec-question/#findComment-311482
Share on other sites

Actually i was experimenting with this script and thought to expand its capabilities a bit. Anyway u guys gave me some great ideas.

 

Thorp, im not a linux pro, but i tried 'whoami' and the user is 'apache' who doesnt have write privileges. Also im not going to realease this script to the end user  :)

Link to comment
https://forums.phpfreaks.com/topic/62498-exec-question/#findComment-311816
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.