Page 1 of 1

issues with command line script and fopen

Posted: Tue Aug 01, 2006 1:53 am
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]

Posted: Tue Aug 01, 2006 5:40 am
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.

Posted: Tue Aug 01, 2006 2:14 pm
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!