fwrite not working using CLI

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
venomizer
Forum Newbie
Posts: 2
Joined: Wed Aug 11, 2010 4:34 am

fwrite not working using CLI

Post by venomizer »

Hi,

I'm trying to carry out a simple fwrite function which will write the contents of a particular array value to a txt file.

I know the code works fine because if I run the script via a web browser, my values are written as I want them to be.

However, as soon as I run the script via the command line (by running php.exe and passing it the location of the script), the files aren't written. I know the script is still running okay though as I get the output of the my array printed, but the files just don't get written to. I really can't understand this, it works fine when running through a browser.

I've come to the conclusion that it must be permissions related, but I've ensured that every user on the box (it's Windows) has full control over all files being used. I've tried running cmd.exe as administrator and then running the command, but that doesn't work either. Is it something to do php.exe not having the correct permissions? How can I ensure that it does?

Here is the code if you're interested, but like I said I know it works fine in the browser...

Code: Select all

$title2_txt = "2/title.txt";
$title2_handler = fopen($title2_txt, 'w');
fwrite($title2_handler, $arrFeeds['1']['title']);
fclose($title2_handler);
Please help!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: fwrite not working using CLI

Post by requinix »

That isn't "the code". That's a "small part of the code". Could be a permissions issue - test the return value from fopen - or it could be that $arrFeeds doesn't have the right values, or it could be something else entirely that I wouldn't know of because you only posted a small part of the code.
venomizer
Forum Newbie
Posts: 2
Joined: Wed Aug 11, 2010 4:34 am

Re: fwrite not working using CLI

Post by venomizer »

Well here's the entire script then:

Code: Select all

<?php
	$doc = new DOMDocument();
	$doc->load('************************************');
	$arrFeeds = array();
	foreach ($doc->getElementsByTagName('item') as $node) {
		$itemRSS = array ( 
			'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
			'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
			'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
			'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
			);
		array_push($arrFeeds, $itemRSS);
	}

$max_length = 200;


/////////// UPDATE TITLES ///////////

$title1_txt = "title.txt";

$title1_handler = fopen($title1_txt, 'w');

fwrite($title1_handler, $arrFeeds['0']['title']);

fclose($title1_handler);




print $arrFeeds['0']['title'];




?>
The *'s represent the location of the feed. The point is I know the script works as everything is perfect when it is run in a browser. I need to run it through the command line however, and it just doesn't write the file. I've enabled error reporting in php.ini and I get no errors whatsoever.
Post Reply