Running a PHP script from PHP script using exec()

-----test123.php-----
[php]<?php echo "hello world"; ?>[/php]

-----test.php-----
[php]<?php $test = exec(“php -q /usr/www/lib/test123.php”);
echo $test[0];
?>[/php]

When I try and run test.php, it just hangs. I have 3 processes started (on linux) and the web page just spins for ever. I have tried fullt qualifying the php path as well as tried using php-cli (which it says I do not have, running verision 5.3…). Does anyone have any ideas?

All help is greatly appreciated!

I think its because it’s expecting a carriage return, I heard of this happening before based on operating system.

Give this a try:

[php]

<?php $test = exec("echo . | php -q /usr/www/lib/test123.php "); echo $test[0]; ?>

[/php]

[php]error_reporting(E_ALL);
ini_set(‘display_errors’, ‘1’);[/php]

exec() returns a string, so most likely it would be ‘h’ which is the 0 offset of the string ‘hello world’ unless there is whitespace echoed before or a newline after, in which case you would get a blank page. Look at either the second parameter or shell_exec() or passthru()

@Topcoder that is incredible that it works! It doesn’t hang and lock up my device! The only thing is, it prints out the first letter of the first word at the end. So echoing “Hello World!\n” gives out “Hello World! H”. Any work around that?

This is excellent that it does not hang, how did you figure this out? And why do you have to do this?

@iliveudie

I searched for your issue and didn’t find anything and then while I was reading more about how the function works on php.net, I saw someone mention that way down in the comments. So basically I got lucky or rather you got lucky :stuck_out_tongue:

You should ask @AbraCadaver about your latest issue I’m sure he can answer that one quickly.

@topcoder, well thanks! That was bugging me for a long time! I appreciate your input and time!!!

@AbraCadaver, any input on the reproduction of the first letter of the first word? haha

I thought I explained it, did you read my post?

Do you want to display all output, the first line of the output, what?

I re-read your post. I just echoed the string itself rather than an offset. It worked great!

Sponsor our Newsletter | Privacy Policy | Terms of Service