Page 1 of 1

shell_exec("php file.php"); not working

Posted: Mon Jul 11, 2005 12:49 pm
by mltsy
I'm trying to do the following to get the output of a php file into a variable to send via e-mail, and getting nothing returned:

Code: Select all

$description = shell_exec("php propertytemplate.php id=$id");
echo $description;
Anyone know why nothing is being returned? I know it's starting in the right directory, and the file exists. And If I do shell_exec("ls"); it works fine, or even shell_exec("cat propertytemplate.php"); What's wrong?

-Joe

Posted: Mon Jul 11, 2005 2:20 pm
by pickle
Absolutely nothing is being returned? I would think it would echo the error you're probably getting.

There are a couple things wrong/I'd change with your approach. First is how you call the 'php' command. Unless the 'php' command is in your path, then just calling 'php' will likely get you an error saying that it doesn't know where 'php' is. If you're on linux, try changing that command to:

Code: Select all

/usr/local/bin/php -f propertytemplate.php
I put in the -f flag just to be neater, although it should work without the flag.

Also, sending get variables like that doesn't work in the command line - there is no file called "propertytemplate.php?id=7"(for example). The filename is, of course, just "propertytemplate.php".


What does this file do - are you just trying to run it so you can get output from it? If so, why not either
  • Have the file store it's output in a variable, include that file, then just read that variable, or
  • Turn on buffering before including that file, then after the file is run, take the contents of the output buffer and store that in a variable.
Both approaches are simpler and, in my opinion, cleaner than calling the file from the command line.

Buffering maybe...

Posted: Mon Jul 11, 2005 4:27 pm
by mltsy
Well, the command "php propertytemplate.php id=1022" works fine from a command line, passing id as a variable into it somehow, so I just went with that. But even if it didn't pass that variable, I should be getting some text back from it just as if there were no variable passed.

So, I tried adding the path to php and that didn't change anything. I'm pretty sure it's finding php alright. Also, I've tried with and without the -f flag.

It's not returning an error or anything, I even tried adding 2>&1 to the end, and still nothing gets returned.

Oddly if I just say:

Code: Select all

$description = shell_exec("php");
//or
$description = shell_exec("php propertytemplate.php");
It returns some text as if it's passing itself to the php program (the file "action.php" which this is all being written in). I don't understand this at all.

What I'm trying to do is get the result of parsing propertytemplate.php and send it in an e-mail. I could just put the whole page in a string, but I'd rather keep the file editable as HTML if it's possible. I'd be willing to try using the output buffer, but I don't know how to do that :) Any hints on that?

Posted: Mon Jul 11, 2005 5:38 pm
by pickle
It's actually quite a novel and simple concept once you see it.

Code: Select all

output_end_flush();//dump whatever's already in the output buffer
ob_start();//start output buffering
include('property_template.php');
$output_contents = ob_get_contents()//get the contents of the output buffer;
ob_end_clean();//silently discard what's in the buffer, and stop output buffering
I've never used this myself, but I've seen it working. This particular snippet is untested though - might need some tweaking.

That works great :)

Posted: Tue Jul 12, 2005 10:39 am
by mltsy
That works very well, except output_end_flush() had to be changed to ob_end_flush().

That is pretty slick! Thanks :)