By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,953 Members | 1,524 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,953 IT Pros & Developers. It's quick & easy.

How to write PHP standard output on a text file?

P: n/a
Hi.

Using PHP 4.3.11, I want my script not to write standard output on the
webpage, but
on a textfile.

I tried with:

Expand|Select|Wrap|Line Numbers
  1. $variableName = 44;
  2. system("/usr/local/php/bin/php /home/foo/script.php >
  3. /home/foo/file.txt", $retval);
  4.  
But '/home/foo/script.php' doesn't consider $variableName.

Is there any way to call '/home/foo/script.php' and make it to write
standard output
on a textfile?

Thank you very much.

Feb 10 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
d
<fr**********@usa.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
Hi.

Using PHP 4.3.11, I want my script not to write standard output on the
webpage, but
on a textfile.

I tried with:

Expand|Select|Wrap|Line Numbers
  1.  $variableName = 44;
  2.  system("/usr/local/php/bin/php /home/foo/script.php >
  3.  /home/foo/file.txt", $retval);
  4.  

But '/home/foo/script.php' doesn't consider $variableName.

Is there any way to call '/home/foo/script.php' and make it to write
standard output
on a textfile?

Thank you very much.


Use output buffering:

ob_start();
system("/bin/date");
$c=ob_get_contents();
ob_end_clean();
$fp=fopen("/home/foo/file.txt", "wb");
fwrite($fp, $c);
fclose($fp);

The ob_start() causes all the output of the script to be stored in a buffer,
which you have access to.

http://www.php.net/outcontrol

hope that helps!

dave
Feb 10 '06 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

fr**********@usa.com wrote:
Is there any way to call '/home/foo/script.php' and make it to write
standard output
on a textfile?


You can always capture the stdout output with the output buffering
functions, then write everthing into a file.
- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

http://acm.asoc.fi.upm.es/~mr/
Proudly running Debian Linux with 2.6.12-1-686 kernel, KDE3.5.0, and PHP
5.1.2-1 generating this signature.
Uptime: 19:17:22 up 36 min, 1 user, load average: 1.51, 1.03, 0.89

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFD7NjZ3jcQ2mg3Pc8RAkQSAJkBbKliuAT64G1CPIdeQs gX9b/82wCdHYd1
LcAZW0B8n2aCdKtbeK7pAAQ=
=WKOM
-----END PGP SIGNATURE-----
Feb 10 '06 #3

P: n/a
d wrote:
<fr**********@usa.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...

Using PHP 4.3.11, I want my script not to write standard output on the
webpage, but on a textfile.

I tried with:

Expand|Select|Wrap|Line Numbers
  1.  $variableName = 44;
  2.  system("/usr/local/php/bin/php /home/foo/script.php >
  3.  /home/foo/file.txt", $retval);
  4.  

But '/home/foo/script.php' doesn't consider $variableName.

Is there any way to call '/home/foo/script.php' and make it to write
standard output on a textfile?
Use output buffering:


I love the ob_* functions myself as well...
ob_start();
system("/bin/date");
$c=ob_get_contents();
ob_end_clean();


Since your php version is >=4.3.0, "$c=ob_get_clean();" will do the same
as the last two lines, and IIRC, it's a bit more efficient for cpu cycles.

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Feb 10 '06 #4

P: n/a
d
"Justin Koivisto" <ju****@koivi.com> wrote in message
news:Hp********************@onvoy.com...
d wrote:
<fr**********@usa.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...

Using PHP 4.3.11, I want my script not to write standard output on the
webpage, but on a textfile.

I tried with:

Expand|Select|Wrap|Line Numbers
  1.  $variableName = 44;
  2.  system("/usr/local/php/bin/php /home/foo/script.php >
  3.  /home/foo/file.txt", $retval);
  4.  

But '/home/foo/script.php' doesn't consider $variableName.

Is there any way to call '/home/foo/script.php' and make it to write
standard output on a textfile?
Use output buffering:


I love the ob_* functions myself as well...
ob_start();
system("/bin/date");
$c=ob_get_contents();
ob_end_clean();


Since your php version is >=4.3.0, "$c=ob_get_clean();" will do the same
as the last two lines, and IIRC, it's a bit more efficient for cpu cycles.


Indeed :)
--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com

Feb 10 '06 #5

P: n/a
NC
fr**********@usa.com wrote:

Using PHP 4.3.11, I want my script not to write standard output
on the webpage, but on a textfile.

I tried with:

Expand|Select|Wrap|Line Numbers
  1.  $variableName = 44;
  2.  system("/usr/local/php/bin/php /home/foo/script.php >
  3.  /home/foo/file.txt", $retval);
  4.  

But '/home/foo/script.php' doesn't consider $variableName.


Of course; you are not passing it to the script... Try this:

$variableName = 44;
system("/usr/local/php/bin/php /home/foo/script.php $variableName " .
" > /home/foo/file.txt", $retval);

In the script, you'll have to refer to the variable being passed as
$argv[1].

Cheers,
NC

Feb 10 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.