Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.