I’m using this PHP code below to create dynamic xml files.
...
$file_name = "";
$rss_feed_dir = $_SERVER['DOCUMENT_ROOT'] . '/xml/';
chmod($rss_feed_dir, 0777);
$file = $rss_feed_dir . $file_name . '.xml';
$file_handle = fopen($file, "w");
fwrite($file_handle, $xml);
fclose($file_handle);
The file will be created if the directory permissions are set 0777, and seems to fail at 0755. I’ve read on many sites that world execute permissions can pose a security risk.
Should I chmod back to 0755 at the end of this script?
Is there a better way to set the directory permissions?
I suspect it is because whichever directory your rss_feed_dir points to, is a directory owned by another user – other than the user that is being used to run your script.
So 755 permissions would exclude you from write access to the directory if your script is not running with the specified group or user. Which explains why you cannot do a write file command successfully when permissions were 755, but successful when 777.
If this php file is on a server and being called from the web, try modifying your directory ownership itself:
And then chmod to 775
Hope that helps!