PHP/Apache errors writing to file

Soldato
Joined
1 Feb 2006
Posts
8,188
Hi,

I have an RSS generation script which I use to open a file using fopen function in PHP. The problem is that I have to change the file permissions to do so.

I have tried using the chmod function in PHP but it doesn't work.

Code is something like this
Code:
chmod("/somedir/somefile", 0755);

This returns an error saying...Warning: chmod() [function.chmod]: Operation not permitted.....

Would this mean that chmod isn't enabled on my hosting? I know the PHP script works fine on a local testing server but when it went live it didn't play ball. Eventually I want it to run at set intervals itself so I need the permissions sorted first.

I am guessing that I want to firstly set the permissions to 0777, write to file and then reset permissions to 0644. Is this correct?

Thanks
 
Last edited:
This returns an error saying...Warning: chmod() [function.chmod]: Operation not permitted.....

Would this mean that chmod isn't enabled on my hosting?

Yes thats normally what it means. Contact your host to see if they will allow it or start looking for a new host :)
 
yeah I can execute my script when I manually set the permissions of the file but I want this to run on my server like a scheduled task or cron job or something similar. Ideally the script will execute to allow the write, database contents exported to rss, file permissions reset to no-write and then the process completes. I cannot keep the file permissions set at 777 all the time. You see where I am coming from?
 
Don't want to start a new thread on my RSS generation but I managed to get the above cron task sorted and now it is pumping database content every 12 hours into my feed.

There is one problem though. My feed doesn't validate and for the life of me I cannot work it out. When I view source in a browser it looks like perfectly formed xml but the validator on w3c says otherwise. The error is where the <atom> tag is generated.

I am thinking that it must be to do with writing the content to the file using php. What way should I be outputting my content?

I have something similar to this... do I need to strip tags or whitespace or anything?

Code:
	$xml = "<?xml version=\"1.0\"?>
<rss version=\"2.0\">
	<channel>
		<title>News</title>
		<link>http://www.site.co.uk/feed.xml</link>
		<description>Latest news from site.co.uk</description>";

//database query above to look for content
...
$xml .= "
		<item>
			<guid>http://www.site.co.uk/articles/".$article_id."</guid>
			<title>$title</title>
			<link>http://www.site.co.uk/articles/".$article_id."</link>
			<description>$description</description>
			<pubDate>$time</pubDate>	
		</item>";
		
	}
	
	$xml .= "\r\t<atom:link href=\"http://www.site.co.uk/feed.xml\" rel=\"self\" type=\"application/rss+xml\" />\r\t</channel>\r</rss>";

//written to file
	$xml = str_replace("\t", "   ", $xml);
				
	fwrite($handle, $xml);
	fclose($handle);

Anyone able to spot any problems? As far as I am concerned the feed is valid based on content as it is identical to the manually created version of the file, and also it displays perfect is RSS readers but there must be some whitespace or something that is breaking it.
 
Last edited:
If you can get cron to change the permission its prob that the file your trying to modify is owned by a different group to that which the script is owned by. Cron is run as root usually so can modify pretty much anything.
 
Cheers for that - got the cron all sorted now so its just getting my rss file to validate that is the problem. Is is very strange and it looks well formed to me and displays correctly in RSS reader.
 
Well, you should give specific group ownership to single files if they need to be modified. Its dangerous to let anything like cron modify files - also cron is meant for maintenance tasks. Id look into setting the correct permissions on only files that require modification to keep the system secure and permissions in tact.
 
Yeah well there is one file in question which sits on my root directory. The cron just runs a php script to output from database and generate the rss feed file. The script is located outside of my public html folder so cannot be executed by anyone else. The permissions of the file are still 644 so I don't believe there is a risk here. Is there anything else I should be checking? My cron just executes the php script but it doesn't contain anything about permissions.
 
In your case theres prob no harm at all in doing it that way. I just personally dont like to run anything as root if I dont have to.

Edit- What does the validator output read? And what does the output text look like from the php?
 
Last edited:
Back
Top Bottom