0

I wrote a php script to get my latest Twitter tweet and save it to a file. Here is the part doing so :

// No errors exist. Write tweets to json/txt file.
$file = $twitteruser."-tweets.txt";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, json_encode($tweets));
fclose($fh);

This works fine when I run my php script directly in the browser, however, when I run the file from a cron job it creates the file in my user root directory (obviously not the correct place).

If I change the above line to :

$file = "public_html/get-tweets/".$twitteruser."-tweets.txt";

the cronjob now works and saves to the correct location, but then manually running the file in my browser gives an fopen error that the file does not exist.

What the heck is the problem? I need this to work both from cronjob and manually.

user756659
  • 3,372
  • 13
  • 55
  • 110

1 Answers1

3

Use a full path from the root of the filesystem, then both should be fine.

Taz
  • 1,235
  • 9
  • 16
  • When I do that as mentioned above the cron works fine, but manually calling the php script in the browser gives an fopen error. – user756659 Oct 22 '13 at 00:40
  • In your example above you've given a path relative to the http/apache root, not the filesystem root (i.e: '/') – Taz Oct 22 '13 at 00:42
  • ahh... yeah I see what you are saying and that fixed the problem. Thanks for the quick help! – user756659 Oct 22 '13 at 00:56