calling php scripts

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ehf1969
Forum Newbie
Posts: 3
Joined: Sat May 21, 2005 8:53 am

calling php scripts

Post 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!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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');
ehf1969
Forum Newbie
Posts: 3
Joined: Sat May 21, 2005 8:53 am

php call

Post 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!
thegreatone2176
Forum Contributor
Posts: 102
Joined: Sun Jul 11, 2004 1:27 pm

Post by thegreatone2176 »

why cant you just include() the page?
ehf1969
Forum Newbie
Posts: 3
Joined: Sat May 21, 2005 8:53 am

Post 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.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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.. :?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
Post Reply