PHP exec function

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
Post Reply
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

PHP exec function

Post by mudasir »

AOA,

Dear LP members,

I am trying to write a PHP script that will generate directory list using a command

Code: Select all

<?php
$cmd="ls -la /sharing/ | awk '{ print $9}'";
$files = array();
exec($cmd, $files);
echo '<pre>';
print_r($files);
echo '</pre>';
?>
This code works almost fine for me except it gives output like this
Array
(
[0] =>
[1] => .
[2] => ..
[3] => Anti-ARP
[4] => Connection-Maker
[5] => SXE
[6] => cbts
[7] => dgb
[8] => diglinux.com.tar.gz
[9] => forum.sql
[10] => forum.tar.gz
[11] => games
[12] => islam
[13] => movies
[14] => rhel
[15] => softwares
[16] => songs
[17] => stage-show
[18] => videos
[19] => voi-vidios
)
Can anyone help me out in removing this ARRAY and the ARRAY numbers and "=>" sign. The output should only be the names of directories.

Looking for a positive and quick reply.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
mrkkhattak
Site Admin
Posts: 285
Joined: Wed Aug 07, 2002 8:00 pm
Location: Karachi
Contact:

Post by mrkkhattak »

Assalamualaikum,

Please try it as the following

Code: Select all

$cmd="ls -la /sharing/ | awk '{ print $9}'";
$files = array();
exec($cmd, $files);
/*echo '<pre>';
print_r($files);
echo '</pre>';*/

foreach ($files as $file){
	echo '<pre>';
	print_r($file);
	echo '</pre>';
}
It will print the names without those array signs.

Let me know if it doesn't work and you need further help.
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

Post by mudasir »

AOA,

Thanks.

One more thing, how will i be able to ignore the first two entries which are "." and "..".
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

you're doing it wrong. you're supposed to use the dir functions.
Watch out for the Manners Taliban!
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
mrkkhattak
Site Admin
Posts: 285
Joined: Wed Aug 07, 2002 8:00 pm
Location: Karachi
Contact:

Post by mrkkhattak »

You are right Lambda, I personally prefer to use dir.

And Mudasir you could ignore the . and .. using the following from the list:

Code: Select all

$cmd="ls -la /sharing/ | awk '{ print $9}'";
$files = array();
exec($cmd, $files);
foreach ($files as $file){

	// it checks for empty space or . or .. and ignore them.
	if (($file === "") || ($file === ".") || ($file === "..")){
		//echo "don't print it.";
	}
	else {
		echo '<pre>';
		print_r($file);
		echo '</pre>';
	}
} 
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

just print the <pre></pre> tags around the entire block, not around each item.
Watch out for the Manners Taliban!
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

Post by mudasir »

AOA,

Thanks allot for the help. I have finally made what i was looking for.

Code: Select all

<?php
$cmd="ls -la /sharing/ | awk '{ print $9}'";
$files = array();
exec($cmd, $files);
foreach ($files as $file){
   if (($file === "") || ($file === ".") || ($file === "..")){
      //echo "don't print it.";
   }
   else {
        echo '<a href=http://172.16.254.2/sharing/' . $file . ' target=_blank>';
      print_r($file);
        echo '</a>';
      echo '<br>';
   }
}
?>
I am not an expert in php, but this is what i made with help from you people.

Thanks again

Now i am making a search script. I will ask again if i get in trouble again :D
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

use dir instead of exec.
Watch out for the Manners Taliban!
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

Post by mudasir »

AOA,

By using DIR function, i have come around with this code.

Code: Select all

<?php
$d = dir("/sharing");
        while (false !== ($entry = $d->read())) {
                if (($entry === "") || ($entry === "." ) || ($entry === "..")){
                //echo "dont print.";
                }
                else {
                echo '<a href=http://172.16.254.2/sharing/' . $entry . ' target=_blank>';
                echo($entry);
                echo '</a>';
                echo '<br>';
                }
}
$d->close();
?>
One more thing, i am making a search script.

This code is of search.php

Code: Select all

<?
if(isset($_POST['submit'])) {
$SEARCH = $_POST['search'];
$CMD = 'find /sharing/ -iname' . $SEARCH;
exec($CMD, $RESULTS);
foreach ($RESULTS as $result){

                echo '<pre>';
                print_r($result);
                echo '</pre>'
}
}
?>
This code is of search.html

Code: Select all

<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="search.php">
Search:<input type="text" size="12" maxlength="12" name="search"><br />
<input type="submit" value="submit" name="submit"><br />
</form><br />
Please tell me what i am doing wrong in this, its not giving me any reply.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

php's documentation page for dir has an example for doing a recursive listing. use that.
Watch out for the Manners Taliban!
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

Post by mudasir »

AOA,

Dear Lambda bhai,

I dont want to do recursive listing, i want to search for a particular string that will be entered by the user. I searched google but found nothing related to this.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

Post by mudasir »

AOA,

I made the search script, but i am facing some issues...The script is below

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
	"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Contact Form</title>
<meta http-equiv="Content-Type" 
   content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
   if ($_SERVER['REQUEST_METHOD'] != 'POST'){
      $me = $_SERVER['PHP_SELF'];
?>
   <form name="form1" method="post"
         action="<?php echo $me;?>">
Search:<input type="text" size="12" maxlength="12" name="Search"><br>
<input type="submit" name="Submit" value="Send"></td>
   </form>
<?php
   } else {
      error_reporting(0);
	$myvar = stripslashes($_POST['Search']);
	$cmd = 'find /sharing/ -iname ' .$myvar. '*';
        $results = array();
	exec($cmd, $results);
	foreach ($results as $result){
	echo '<pre>';
	echo '<a href=http://172.16.254.2' . $result . ' target=_blank>';
	print_r($result);
	echo '</a>';
	echo '</pre>';
}
}
?>
</body>
</html>
The issue is that is shows the complete path, is it possible that is will show only the file name not the complete path.

Second issue that i am facing is that, the space in the path or in the file name is creating problems, it is breaking the path in the link.

Please tell me how can i over come these issues.

Looking for a quick and positive response.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re:

Post by LinuxFreaK »

Dear
Salam,

I am not a php programmer but i think explode funcation will help you.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
   "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Contact Form</title>
<meta http-equiv="Content-Type"
   content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
   if ($_SERVER['REQUEST_METHOD'] != 'POST'){
      $me = $_SERVER['PHP_SELF'];
?>
   <form name="form1" method="post"
         action="<?php echo $me;?>">
Search:<input type="text" size="12" maxlength="12" name="Search"><br>
<input type="submit" name="Submit" value="Send"></td>
   </form>
<?php
   } else {
      error_reporting(0);
   $myvar = stripslashes($_POST['Search']);
   $cmd = 'find /sharing/ -iname ' .$myvar. '*';
        $results = array();
   $far = exec($cmd, $results);
   $fars = explode("/", $far);
   foreach ($fars as $result){
   if($result === "home")
   {
   }
   else
   {
   echo '<pre>';
   echo '<a href=http://172.16.254.2/' . $result . '/ target=_blank>';
   print($result);
   echo '</a>';
   echo '</pre>';
   }
}
}
?>
</body>
</html>
Farrukh Ahmed
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

Post by mudasir »

AOA,

Dear Farrukh bhai,

Its not what i am looking for. I tried the EXPLODE function, but not working out for me. Lets see if i find anything.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
Post Reply