Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exec

Status
Not open for further replies.

YoungManRiver

IS-IT--Management
Feb 9, 2004
220
US
All,

The PHP users manual says the way to call an external routine is one of the following:
Code:
exec()
system()
passthru()
escapeshellcmd()
pcntl_exec()
I've tried all these for the following code:
Code:
   if (isset($HTTP_POST_VARS['fixs'])) {
      $drvstr = 'D: E:'
      for ($i = 0; $i < words($drvstr); ++$i) {
          $tstfil = word($drvstr,$i)."\Local Files\Projects\Bible Topics\SQL & Queries\files_out.rex"
          if (file_exists($tstfil)) {
             $usedrv = word($drvstr,$i);
             $execln = "rexx $tstfil";
          }
      }
      $fil_in = addslashes($usedrv.'\Local Files\Projects\Bible Topics\SQL & Queries\files_out.txt');
      $rc = exec(addslashes($execln));
      $rc = add_files($fil_in);
      $execln = 'rexx '.$usedrv.'"\Local Files\Projects\Bible Topics\SQL & Queries\fix_lines.rex"';
      $rc = exec(addslashes($execln));
   }          // end if isset $HTTP_POST_VARS['fixs']
Nothing seems to actually cause the command to execute, as return is immediate, but when run from cmd line this process takes 5-10 minutes.

Oh, the "D: E:" check is because this process is installed on different drives on different machines. The command "rexx" calls OO Rexx (free download from Also tested the DOS cmd "dir *.* /b /s > dir_list.txt" and no action/reaction.

What do I need to do in order to get my external command to run?

YMR
 
a few things to think about

the backslash is an escape character in php. convert them to double backslashes or to forward slash.

spaces in directory names sometimes don't work. try enquoting the relevant uri segment.

not sure what the word function is. is there definitely a function called words (plural) as well as word (singular)?

your code overwrites the value of $execln each iteration of the loop but the execution of this line is not done until after the loop. is this intended?

addslashes to an exec string is unlikely to work. why not use escapeshellcmd() or escapeshellarg() (as the case may be)?

why are you using the (very) deprecated $HTTP_POST_VARS globals? why not $_POST?

why do you think that the script is finished? it looks to me that your script (if it worked) would not print anything to the screen anyway.
 
J,
the backslash is an escape character in php. convert them to double backslashes or to forward slash.
That is what the "addslashes" routine does, corrects this problem.
not sure what the word function is.
This is in my REXXforPHP addin functions module, it's a REXX command.
your code overwrites the value of $execln each iteration of the loop but the execution of this line is not done until after the loop. is this intended?
No but quess I need to put in a break to escape the loop when value is valid, but will revisit, as I forgot original intent.
why are you using the (very) deprecated $HTTP_POST_VARS globals? why not $_POST?
I found that older machines with 4.x or 3.x will not work with the abbreviated version so still write out the full var name so all work.

Will have to revisit code for the rest. Forgot where I was on this.

YMR
 
[0] >Also tested the DOS cmd "dir *.* /b /s > dir_list.txt" and no action/reaction.[/i]
It needs all the same to read attentively the documentation.
[tt]
<?php
$execln="dir *.* /b/s";
$rc=exec($execln,$a,$r);
echo implode($a,"\n")."\n";
echo $r."\n";
echo '$rc (last line of the output) = '.$rc."\n";
?>
[/tt]
[1] Taken for granted functions provisioned by the rexx extension, I would change the script to look something like this to make more sense to me.
[tt]
if (isset($HTTP_POST_VARS['fixs'])) {
$drvstr = 'D: E:'
for ($i = 0; $i < words($drvstr); ++$i) {
$tstfil = word($drvstr,$i)."\Local Files\Projects\Bible Topics\SQL & Queries\files_out.rex"
if (file_exists($tstfil)) {
$usedrv = word($drvstr,$i);
//watch the position of the quotes
$execln = 'rexx [red]\"[/red]'.$tstfil[red].'"'[/red];

[red]//these lines should be in the for loop?
//where $usedrv is still properly defined
//and in particular within the if conditional postive?[/red]

//note the position of the quotes
$fil_in = addslashes([red]'"'.[/red]$usedrv.'\Local Files\Projects\Bible Topics\SQL & Queries\files_out.txt'[red].'"'[/red]);
//depends on if you want to use the outparams
$rc = exec(addslashes($execln),$a,$r);
$rc = add_files($fil_in);
//again watch the position of the quotes
$execln = 'rexx [red]"[/red]'.$usedrv.'[highlight]\[/highlight]Local Files\Projects\Bible Topics\SQL & Queries\fix_lines.rex[red]"[/red]';
//depends on if you want to use the outparams
$rc = exec(addslashes($execln[red],$a,$r[/red]));
} [red]else {[/red]
//no action needed?
[red]}[/red]
}
} // end if isset $HTTP_POST_VARS['fixs']
[/tt]
 
Hi

$execln="dir *.* /b/s";
$rc=exec($execln,$a,$r);
I never tried that from PHP, but for example in Java will not work. Because it expects to find a [tt]dir[/tt] executable, but no such thing exists.
Code:
$execln="$_ENV[COMSPEC] /c dir *.* /b/s";
Anyway, this is only for your test case.

Feherke.
 
I have no object to doing that, no - just as well and maybe more comforting.

ps There's a (harmless) typo in my prev post, the corresonding line should be read like this.
[tt] $execln = 'rexx [highlight]"[/highlight]'.$tstfil.'"';[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top