issues with command line script and fopen

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
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

issues with command line script and fopen

Post by julian_lp »

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm having problems with a command line script's output. I mean, I can't get -fopen- work as I expect (probably beacause I am not aware of some different beaviour in command line) . I mean, when I use

Code: Select all

fwrite(STDOUT, "whatever");
all works fine, and I can see the output on the screen. But, as soon as I want a file as an output, it doesn't produce any

Code: Select all

$handle = fopen('test.txt', 'a+');
        fwrite($handle, "whatever");  
        fclose($handle);

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Try this one

Code: Select all

<?php
echo 'script file: ', __FILE__, "\n";
echo 'working dir: ', getcwd(), "\n";

$handle = fopen('test.txt', 'a+') or die('cannot open file');
echo 'fwrite: ', fwrite($handle, "whatever");
fclose($handle);
?>
you give fopen a relative path. And it's relative to the current working directory.
So your file test.txt should be in the directory printed after working dir: or fopen has failed.
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

Post by julian_lp »

volka wrote:Try this one

Code: Select all

<?php
echo 'script file: ', __FILE__, "\n";
echo 'working dir: ', getcwd(), "\n";

$handle = fopen('test.txt', 'a+') or die('cannot open file');
echo 'fwrite: ', fwrite($handle, "whatever");
fclose($handle);
?>
you give fopen a relative path. And it's relative to the current working directory.
So your file test.txt should be in the directory printed after working dir: or fopen has failed.

mmm, so, as long as PHP is called from command line, the "actual" path is the root of PHP installation, whereas when srcipt is executed from browser, the actual path is the script's path... I wouldn't have thought mine was a path problem ;)

Thanks volka!
Post Reply