Jump to content


Photo

fwrite()


  • Please log in to reply
5 replies to this topic

#1 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 20 October 2006 - 05:33 PM

I am having issues.

I use the following:
<?php

$filename = "testfile.txt";

if(!$handle = fopen($filename, 'a+')) {
  echo "Cannot open file";
}
?>

Now I get an Permission Denied warning and the Cannot open file error when I try to open/write a file.

It seems (if the file exists) the file that is to be written to needs to be 777 for it to work.  Isn't the a+ argument supposed to create the file and open it if its not there??  The script itself has 777 permissions.

What I want to do is if the file doesn't exists, create it and change the permission so that I can write to it properly.
What am I doing wrong?

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 October 2006 - 05:41 PM

does the folder into which you're trying to write your file have writable permissions? that's key. the file itself needs them, yes, but if the folder in which you are trying to create a file doesn't, you're hosed.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 AlexJ312

AlexJ312
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 20 October 2006 - 06:03 PM

Sounds like a permission issue.

#4 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 20 October 2006 - 07:35 PM

So i would have to turn the entire folder that the file sits in to writable?  geesh that seems a bit of a security issue...

Or is it best to put it into a subdirectory of its own and 777 that subdir?

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 October 2006 - 07:39 PM

Or is it best to put it into a subdirectory of its own and 777 that subdir?


bingo... however, if possible, you should set up your apache user to have group permissions so you can set your folder to 755 or 775 instead of 777.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#6 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 20 October 2006 - 07:54 PM

I'll look into that as well. Thanks Obsidian, thanks others too :)





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users