Jump to content

Recommended Posts

Good Friday to everyone! 

 

I am writing some PHP code to find certain files and the search the files for a certain string.  For example, this is what I have. This works in a LINUX shell environment, yet I need to have it work in PHP since the GUI is web based. 

 

Can anyone help?

 

Thanks!

Mike

 


<?php
$output = shell_exec('find /sync/www/html/ohx/products/MEMNPWOHX* -type f -exec grep "HEAT ADVISORY IN EFFECT" /dev/null {} \;');
echo "<pre>$output</pre>";
?>


Link to comment
https://forums.phpfreaks.com/topic/256820-shell_exec-help/
Share on other sites

This is working for me:

 

$output = shell_exec('find ~/Desktop/ -type f -exec grep "secure" /dev/null {} \;');

You should be able to simply copy and paste your command from inside shell_exec directly into a command line, and vice versa.  Does it work if you try it directly?

 

Link to comment
https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316616
Share on other sites

Ok here is what I came up with, Thanks for the huge help from ManiacDan!  Now I have a list of files that should have say HEAT ADVISORY in them, but when I run the script from the form to gather the $pil and $string, it only shows the listing from one file. Not all of them.

 

MEMNPWOHX*HEAT ADVISORY IS IN EFFECTfind MEMNPWOHX* -type f -exec grep "HEAT ADVISORY IS IN EFFECT" /dev/null {} \;

 

MEMNPWOHX.0810:* THE HEAT ADVISORY IS IN EFFECT FOR

MEMNPWOHX.0810:* THE HEAT ADVISORY IS IN EFFECT FOR THAT PORTION OF MIDDLE TN

MEMNPWOHX.0810:* THE HEAT ADVISORY IS IN EFFECT FOR THAT PORTION OF MIDDLE TN

MEMNPWOHX.0810:* THE HEAT ADVISORY IS IN EFFECT FOR THAT PORTION OF MIDDLE TN

MEMNPWOHX.0810:* THE HEAT ADVISORY IS IN EFFECT FOR THAT PORTION OF MIDDLE TN

MEMNPWOHX.0810:* THE HEAT ADVISORY IS IN EFFECT FOR THAT PART OF MIDDLE TN

MEMNPWOHX.0810:* THE HEAT ADVISORY IS IN EFFECT FOR THAT PART OF MIDDLE TN

MEMNPWOHX.0810:* THE HEAT ADVISORY IS IN EFFECT FOR THAT PART OF MIDDLE TN

 

 

<?php
session_start();
$pil = $_POST['pil'];
$pil = strtoupper($pil);
$string = $_POST['string'];
$string = strtoupper($string);
$pil = "$pil"."*";
echo $pil;
echo $string;
$cmd = "find $pil -type f -exec grep \"$string\" /dev/null {} \;";
echo $cmd;
$output = shell_exec($cmd);
echo "<pre>$output</pre>";
?>

Link to comment
https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316664
Share on other sites

This is what I get from the command line in LINUX.  As you will see, I only get one file listed with the php code.... and all of them in a LINUX shell.

 

MEMNPWOHX.0609:...HEAT ADVISORY IN EFFECT FROM 1 PM THIS AFTERNOON TO 8 PM CDT

MEMNPWOHX.0609:...HEAT ADVISORY IN EFFECT FROM 1 PM CDT THIS AFTERNOON TO 8 PM CDT

MEMNPWOHX.0610:...A HEAT ADVISORY IN EFFECT FOR THE WESTERN QUARTER OF MIDDLE TN

MEMNPWOHX.0610:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THIS EVENING...

MEMNPWOHX.0705:...HEAT ADVISORY IN EFFECT FROM NOON SUNDAY TO 10 PM CDT MONDAY...

MEMNPWOHX.0705:...HEAT ADVISORY IN EFFECT FROM NOON TODAY TO 10 PM CDT MONDAY...

MEMNPWOHX.0705:...HEAT ADVISORY IN EFFECT FROM THIS AFTERNOON TO 10 PM CDT MONDAY...

MEMNPWOHX.0706:...HEAT ADVISORY IN EFFECT FROM 1 PM THIS AFTERNOON TO 7 PM CDT

MEMNPWOHX.0710:...HEAT ADVISORY IN EFFECT FROM NOON TODAY TO 7 PM CDT THIS

MEMNPWOHX.0710:...HEAT ADVISORY IN EFFECT FROM NOON TO 7 PM CDT FRIDAY...

MEMNPWOHX.0711:...HEAT ADVISORY IN EFFECT FROM NOON MONDAY TO 7 PM CDT TUESDAY...

MEMNPWOHX.0711:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT TUESDAY...

MEMNPWOHX.0711:...HEAT ADVISORY IN EFFECT FROM NOON WEDNESDAY TO 7 PM CDT

MEMNPWOHX.0711:...HEAT ADVISORY IN EFFECT FROM NOON WEDNESDAY TO 7 PM CDT

MEMNPWOHX.0805:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT SATURDAY FOR MIDDLE

MEMNPWOHX.0805:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT SATURDAY...

MEMNPWOHX.0805:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT SATURDAY...

MEMNPWOHX.0805:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT SATURDAY...

MEMNPWOHX.0807:...HEAT ADVISORY IN EFFECT FROM 1 PM TUESDAY TO 7 PM CDT

MEMNPWOHX.0807:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THURSDAY...

MEMNPWOHX.0808:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT THIS AFTERNOON...

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM 2 PM THIS AFTERNOON TO 7 PM CDT

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM 1 PM TUESDAY TO 7 PM CDT

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THIS EVENING...

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FOR THE FAR NORTHWEST PORTION OF MIDDLE

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THIS EVENING...

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM NOON TODAY TO 7 PM CDT THIS

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM NOON TO 7 PM CDT TUESDAY...

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM NOON TO 7 PM CDT TUESDAY...

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT WEDNESDAY...

MEMNPWOHX.0811:...HEAT ADVISORY IN EFFECT FROM NOON WEDNESDAY TO 7 PM CDT

MEMNPWOHX.0811:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THURSDAY...

MEMNPWOHX.0811:...HEAT ADVISORY IN EFFECT FROM 7 PM THIS EVENING TO 7 PM CDT

 

Link to comment
https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316667
Share on other sites

You ran this command on the command line?

find MEMNPWOHX* -type f -exec grep "HEAT ADVISORY IS IN EFFECT" /dev/null {} \;

And that command produced the list in your last post?  That command is not correctly formatted.

 

Also, you absolutely must run these strings through escapeshellarg (which automatically quotes them for you).

 

Link to comment
https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316684
Share on other sites

Yes that worked.  When I run the command in a terminal shell like you listed, this is what I get:

 

[mikey@ohx-lw-ostrich products]# find /sync/www/html/ohx/products/ -name MEMNPWOHX*  -exec grep "HEAT ADVISORY IN EFFECT"

find: paths must precede expression

Usage: find [-H] [-L] [-P] [path...] [expression]

 

[mike@ohx-lw-ostrich products]# find MEMNPWOHX* -type f -exec grep "HEAT ADVISORY IN EFFECT" /dev/null {} \;

MEMNPWOHX.0609:...HEAT ADVISORY IN EFFECT FROM 1 PM THIS AFTERNOON TO 8 PM CDT

MEMNPWOHX.0609:...HEAT ADVISORY IN EFFECT FROM 1 PM CDT THIS AFTERNOON TO 8 PM CDT

MEMNPWOHX.0610:...A HEAT ADVISORY IN EFFECT FOR THE WESTERN QUARTER OF MIDDLE TN

MEMNPWOHX.0610:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THIS EVENING...

MEMNPWOHX.0705:...HEAT ADVISORY IN EFFECT FROM NOON SUNDAY TO 10 PM CDT MONDAY...

MEMNPWOHX.0705:...HEAT ADVISORY IN EFFECT FROM NOON TODAY TO 10 PM CDT MONDAY...

MEMNPWOHX.0705:...HEAT ADVISORY IN EFFECT FROM THIS AFTERNOON TO 10 PM CDT MONDAY...

MEMNPWOHX.0706:...HEAT ADVISORY IN EFFECT FROM 1 PM THIS AFTERNOON TO 7 PM CDT

MEMNPWOHX.0710:...HEAT ADVISORY IN EFFECT FROM NOON TODAY TO 7 PM CDT THIS

MEMNPWOHX.0710:...HEAT ADVISORY IN EFFECT FROM NOON TO 7 PM CDT FRIDAY...

MEMNPWOHX.0711:...HEAT ADVISORY IN EFFECT FROM NOON MONDAY TO 7 PM CDT TUESDAY...

MEMNPWOHX.0711:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT TUESDAY...

MEMNPWOHX.0711:...HEAT ADVISORY IN EFFECT FROM NOON WEDNESDAY TO 7 PM CDT

MEMNPWOHX.0711:...HEAT ADVISORY IN EFFECT FROM NOON WEDNESDAY TO 7 PM CDT

MEMNPWOHX.0805:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT SATURDAY FOR MIDDLE

MEMNPWOHX.0805:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT SATURDAY...

MEMNPWOHX.0805:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT SATURDAY...

MEMNPWOHX.0805:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT SATURDAY...

MEMNPWOHX.0807:...HEAT ADVISORY IN EFFECT FROM 1 PM TUESDAY TO 7 PM CDT

MEMNPWOHX.0807:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THURSDAY...

MEMNPWOHX.0808:...HEAT ADVISORY IN EFFECT UNTIL 6 PM CDT THIS AFTERNOON...

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM 2 PM THIS AFTERNOON TO 7 PM CDT

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM 1 PM TUESDAY TO 7 PM CDT

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THIS EVENING...

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FOR THE FAR NORTHWEST PORTION OF MIDDLE

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THIS EVENING...

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM NOON TODAY TO 7 PM CDT THIS

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM NOON TO 7 PM CDT TUESDAY...

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT FROM NOON TO 7 PM CDT TUESDAY...

MEMNPWOHX.0810:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT WEDNESDAY...

MEMNPWOHX.0811:...HEAT ADVISORY IN EFFECT FROM NOON WEDNESDAY TO 7 PM CDT

MEMNPWOHX.0811:...HEAT ADVISORY IN EFFECT UNTIL 7 PM CDT THURSDAY...

MEMNPWOHX.0811:...HEAT ADVISORY IN EFFECT FROM 7 PM THIS EVENING TO 7 PM CDT

 

Link to comment
https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316701
Share on other sites

The PHP script is not in this folder, is it?  You're running a find command (incorrectly, still) inside a folder, and it's only working because `find` will return all the files in the current folder if you use it wrong.  Since the PHP file is NOT in this folder, when it tries to run `find` it will behave wrong.

Link to comment
https://forums.phpfreaks.com/topic/256820-shell_exec-help/#findComment-1316718
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.