Page 1 of 1

calling php scripts

Posted: Sat May 21, 2005 8:55 am
by ehf1969
Hello everyone.

I'm having trouble trying to call a php script. I've tried using exec() and phpopen().

Both of these work when I try to call a simple function such as "ls -l", but when it comes to calling an actual php script, I'm having very little luck. It'll work sporadically, but I can't find a pattern.

Here's a quick example:

php.caller
=======
exec("php_test_script.php");

// I've tried this also:
//$handle = popen("php test_script_make_output.php", "r");
//pclose($handle);


php_test_script.php
==============
$iHandle = fopen("output.txt", "w");
fwrite($iHandle, "testing 1");
fclose($iHandle);

I'm trying to write to a file called output.txt when php.caller is executed.

If I run php_test_script.php by itself, it works fine.

Any suggestions would be GREATLY appreciated!

Thanks!

Posted: Sat May 21, 2005 11:18 am
by timvw
You need to make sure your shell knows how to handle/execute such a file...

Therefor you need to add a "she-bang" line as the first line to your test_script.php

Code: Select all

#!/usr/bin/php
Or you tell explicitely which interpreter that should be used to execute the script in your caller.php:

Code: Select all

exec('/usr/bin/php test_script.php');

php call

Posted: Sat May 21, 2005 1:15 pm
by ehf1969
The strange thing about all this is that if I refresh the page around 5 or 6 times, it'll work. I tried adding the code you suggested and that didn't help, but thanks for the reply!

Posted: Sat May 21, 2005 1:39 pm
by thegreatone2176
why cant you just include() the page?

Posted: Sat May 21, 2005 1:43 pm
by ehf1969
thegreatone2176 wrote:why cant you just include() the page?
I could if this was all I was doing, but I want to be able to run more complicated runs in the background and just have a text file updated so I can view the progress.

Posted: Sat May 21, 2005 5:48 pm
by infolock
if you do include('myphpscript.php'), it will execute the script automatically... there is no reason to do an exec() call at all unless you are simply wanting to go the long way to do this.. :?

Posted: Sat May 21, 2005 7:32 pm
by Ambush Commander
Wait... do you want to do command line scripting with PHP? If so, then you may want to look into threading/forking.