Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

PHP fopen() problems

fopen

  • Please log in to reply
11 replies to this topic

#1 FlyByWire128

FlyByWire128

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 23 July 2007 - 07:53 AM

So, I'm trying to write a PHP script to open a text file and edit it. I've made all the permissions 777, and I still get:

Warning: fopen() [function.fopen]: failed to open stream: Permission denied in [directory] on line 3

I can't seem to figure out why this isn't working. Any suggestions would be appreciated.
  • 0

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 23 July 2007 - 09:48 AM

Could you post your fopen() code and the file path/variable?
  • 0

#3 Lop

Lop

    Speaks fluent binary

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1112 posts

Posted 23 July 2007 - 05:41 PM

Sounds like you have the wrong path name. Could you show us some code and maybe a "pwd" from the server in the directory the file is at?
  • 0

#4 FlyByWire128

FlyByWire128

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 23 July 2007 - 07:59 PM

The code is very simple:

<?php 
echo getcwd();
$fp = fopen("tuesdayR.txt", "w+");
fclose($fp);

 ?>

No matter what I do, as long as I'm not just trying to read the file (fopen(file, r)) I get the same error:

E:\users\orchestra\Rehearsals
Warning: fopen(tuesdayR.txt) [function.fopen]: failed to open stream: Permission denied in E:\users\orchestra\Rehearsals\testPHP.php on line 3

Warning: fclose(): supplied argument is not a valid stream resource in E:\users\orchestra\Rehearsals\testPHP.php on line 4
PHP Warning: fopen(tuesdayR.txt) [function.fopen]: failed to open stream: Permission denied in E:\users\orchestra\Rehearsals\testPHP.php on line 3 PHP Warning: fclose(): supplied argument is not a valid stream resource in E:\users\orchestra\Rehearsals\testPHP.php on line 4
  • 0

#5 Lop

Lop

    Speaks fluent binary

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1112 posts

Posted 24 July 2007 - 07:29 AM

Try adding the path:


$fp = fopen("E:\\users\\orchestra\\file\\path\\here\tuesdayR.txt", "w+");

  • 0

#6 FlyByWire128

FlyByWire128

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 24 July 2007 - 07:48 AM

I tried adding the whole path as suggested, but no luck. It's still telling me permission denied, and I can't figure out why... Every suggestion I've ever heard when this happens is to change the permissions, but they're all set, so that shouldn't be a problem. Any other suggestions?
  • 0

#7 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 24 July 2007 - 08:18 AM

Have you tried a different file? Maybe it isn't the file permissions but the directory permissions. I've ran into this on Windows Machines before.
  • 0

#8 Crane

Crane

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 388 posts

Posted 24 July 2007 - 11:30 AM

How did you change the permission to 777 in Windows? You mean you just made it world?
  • 0

#9 FlyByWire128

FlyByWire128

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 24 July 2007 - 12:02 PM

I've tried different files, I've tried different directories, I've tried files that already exist, I've tried creating a new file using fopen(). I'm using SmartFTP, and according to it, the directory, and the files are all 777 permissions... I don't know what else to do.
  • 0

#10 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 24 July 2007 - 01:16 PM

        $filename = "tuesdayR.txt";
echo realpath($filename);
$filename = realpath($filename);

if (is_readable($filename)) {
echo "<br />The file is readable...<br />";
} else {
echo "<br />The file is not readable...<br />";
}

if (is_writable($filename)) {
echo "The file is writable...<br />";
} else {
echo "The file is not writable...<br />"
}

if (file_exists($filename)) {
echo "File exists...<br />";
} else {
echo "File cannot be found...<br />";
}

$handle = fopen($filename, "w");
fwrite($handle, 'Yo, sup?');
fclose($handle);


Post the results of this...
  • 0

#11 FlyByWire128

FlyByWire128

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 24 July 2007 - 01:29 PM

The output to that was:

Fatal error: Can't use function return value in write context in E:\users\orchestra\Rehearsals\testPHP.php on line 27
PHP Fatal error: Can't use function return value in write context in E:\users\orchestra\Rehearsals\testPHP.php on line 27

If I take out the last 3 lines of code, I get:

E:\users\orchestra\Rehearsals\tuesdayR.txt
The file is readable...
The file is writable...
File exists...
  • 0

#12 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 24 July 2007 - 03:14 PM

How about this?

$filename = "tuesdayR.txt";
$filename = realpath($filename);
$content = "Yo sup!\n";

if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}

if (fwrite($handle, $content) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote ($content) to file ($filename)";

fclose($handle);


One of my favorite things to do when I have a problem I cant solve is to use "debug statements" to find precisely where the the problem is.

Edit:
Also please post these:

PHP Version
Web server software [apache or IIS] and version
Operating System
Safemode enabled/disabled
  • 0





Also tagged with one or more of these keywords: fopen

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download