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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to exec foxpro app from within PHP 1

Status
Not open for further replies.

CajunCenturion

Programmer
Mar 4, 2002
11,381
US
I'm trying to execute an external program from within a PHP script, and the PHP scrips hangs up on the exec command.

Details:
Windows 2008 R2, Apache 2.4, and PHP 5.2.17
The executable was building Visual FoxPro 9.0, SP2.
The exe currently resides in the Windows\System32 directory. It's only there for now to try to take permissions out of the equation.

Here is a snippet of the code:
Code:
$filename = trim(strtolower($fld[1]->value)) . trim(strtolower($fld[2]->value)) . "." . trim(strtolower($fld[3]->value));
print nl2br ($filename . "\n");
$filename = addslashes($filename);
$pagenum = 1;
$cvtcmd = chr(34) . 'webimage ' . chr(39) . $filename . chr(39) . ' ' . chr(39) . $pagenum . chr(39) . ' > c:\\progout.txt' . chr(34);
print nl2br ($cvtcmd . "\n");                                  

// This is the where it fails
$cvtimg = exec($cvtcmd, $result, $error);

$execln="dir *.* /b/s";
$rc=exec($execln,$a,$r);
print nl2br ( implode($a,"\n")."\n");
print nl2br ( $r."\n");
print nl2br ( '$rc (last line of the output) = '.$rc."\n");

The values of $filename and $pagenum are correct, and the program itself has been verified. It's works as expected when invoked from Start->Run.

I've tried various combinations of single and double quotes, and I've tried both with and without the addslashes function, all to no avail. I've tried exec, system, and shell_exec functions, and I've also included a dir command exec which works fine.

When the PHP script is executed, it hangs up on the $cvtimg = exec($cvtcmd, $result, $error); line, and the browser reports "transferring data from <IP address>" and just waits. I can find no indication of any error on the server.

Any help is much appreciated.



--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
[link]http://php.net/manual/en/function.exec.php[/url]
PHP Online Manual (Notes section) said:
If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

In other words you need to make sure the exec is quick enough to not time out, or not require any output from it.

If you need to wait for output from the executable you'll need to increase the script timeout in PHP if the exec is taking too long to return.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks, but the output is the creation of a file and I need the PHP script to wait until the file has been created. That being said, the file takes roughly 1/2 a second to build.

I do know the program is being fired off (it's listed in the Task Manager), but the program is being hung for some reason. The program does not hang when running as a stand alone program; it only hangs when fired off from within the PHP script.

--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
One possibility that we've considered is that it's getting hung up on the "Publisher cannot be verified" dialog. We've added the .exe to the inclusion list, but that didn't solve the problem.

--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
Does it close itself after the file has been created? PHP will wait until its closed.

But again, if you are creating a file, then you don't need to return anything to PHP, so simply don't ask for output in the script.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
==> Does it close itself after the file has been created? PHP will wait until its closed.
Yes, the program will close itself when it completes, but as I said in my post of 17 May 12 14:14, the program starts and is seen in the Task Manager, but is idle. The issue is not whether PHP needs to wait on the program to finish; it's getting the program to actually run after it's invoked.

==> But again, if you are creating a file, then you don't need to return anything to PHP, so simply don't ask for output in the script.
No, I don't need to return anything to the script, but again as I said earlier, the script must wait on the program to complete since the script requires the created file.

Again, from Start->Run, the external program does exactly what it's required to do. It creates the required file and then terminates. However, when the program is invoked from the PHP script, it starts but does nothing. It can been idle in the Task Manager.

--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
O.k.
So the resulting command to be run ends up looking like this:

[green]"[blue]webimage[/blue] [red]'[/red]filename[red]'[/red] [red]'[/red]5[red]'[/red] > c:\progout.txt"[/green]

If you run it as such directly including all quotes does it complete successfully?

I would question the usage of single quotes around the parameters. I'm thinking the app is being invoked but receives no valid parameters.

With that said, if the program is actually running, then PHP is successfully executing the command given. So there's not much else that can be done here other than making sure that the final command that is being created works.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
@CC

i have not used windows for many years. this may be a wild goose chase.
but, is the behavior different if you give the foxpro application and the relevant data directory and output directory wider permissions for the apache user ?

I was wondering, essentially, whether foxpro, when instantiated, was not able to access the relevant dataset or the output directory and so sat there doing nothing. Might it send an error message to its own error log rather than a system error log? i guess it depends on how well behaved the app is.

this would explain why the command line (start->run) was executing correctly, whereas the same string when executed from exec() was not working.

there are also issues with running apps from a 32bit version of php whilst on a 64bit version of windows. if this is the case then there may be workarounds.

try also using an absolute url to the webimage/file name.

last thought is to make express use of the escapeshellarg function.

Code:
$filename = trim(strtolower($fld[1]->value)) . trim(strtolower($fld[2]->value)) . "." . trim(strtolower($fld[3]->value));
//print nl2br ($filename . "\n");
//$filename = addslashes($filename);
$pagenum = 1;
$cvtcmd =  '"webimage ' . 
           escapeshellarg($filename) . 
           ' '. 
           escapeshellarg($pagenum) . 
           ' > c:\\progout.txt "' ;

if none of the above work, could you post the echo of the $cvtcmd string?
 
It is possibly a parameter issue, and as I said in the original post, I've tried various combinations of single and double quotes, but to no avail. Since both of your posts and brought up the parameter issue, I have tried running the program without any parameters (using hard-coded assignments instead), to take the parameter issue out of play for now. From Start->Run, the program performs as expected, but from PHP->exec, the program starts and is seen idle in the Task Manager. Once we get the execution issue resolved, I will keep the escapeshellarg function in mind when bringing the parameters back in play.

It is possibly a permissions problem, but to the best of knowledge, no error logs are being generated by any process. If they are any logs, I don't know where they are. To address the permissions issue, I've configured Apache to log in as Administrator. Same results. PHP is running as CGI script, so is it possible that it's inheriting some other permission set? If so, what might that be?

What are other environmental or configuration issues that would account for the difference between a program executing as expected from Start->Run, but not from PHP->exec?

Again, I appreciate the ideas.

--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
@jpadie ==> i have not used windows for many years.
I envy you.

--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
What are other environmental or configuration issues that would account for the difference between a program executing as expected from Start->Run, but not from PHP->exec?

essentially the differences would be down to the permissions and potentially any base_dir and other security hardening type restrictions in php.ini. but i would expect the latter to throw visible errors.

could you try running the php script from the command line (not through apache) from a user who is also able to execute the command from start->run?

Code:
c:\>php script.php

that might help debug any potential permissions issue.
 
==> could you try running the php script from the command line
Yes I can and it works fine. The script runs, the programs runs, and executes as expected.

Does that suggest that it's something in the Apache setup?


--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
great.
that means that foxpro at least won't care about being run headless from a php script with the right user perms.

Does that suggest that it's something in the Apache setup?

it increases the probability of that or something quirky in the CGI (definitely cgi btw? or perhaps fastcgi?)

can you try running apache not as administrator but as a user that can execute the start->run cmd correctly? if I remember my cgi right, the php executable will run with the same user perms as the apache process so this should be a good test of whether the issue is a permissions issue.
 
The problem is solved, and I thank you both for your help. There were two primary problems that needed to be addressed.

First, there was a corrupt PHP dll (php_checkuid_ex) which required user acknowledgment (i.e., the reason the problem went idle) and a CLI error which prevented the script from properly terminating. Also, there was a Foxpro resource issue which was addressed in the Foxpro config file.

The key tool, as it turned out, was executing the script through the PHP engine from the command line: c:\>php script.php
That allowed for the finding and fixing of the PHP issues. Those errors didn't show when running the program directly from Start-Run. Secondly, when running the script through PHP as a user, and not as administrator, highlighted the Foxpro resource issue. The Foxpro resource issue doesn't come into play when running as an admin.

With that out of the way, now I can turn my attention to the functional requirements, but, since it's Friday ...

Thanks again guys.

--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
Glad you solved it. Have a great weekend.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top