Jump to content

PHP fopen() problems

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
FlyByWire128

FlyByWire128

    Newbie

  • Members
  • PipPip
  • 16 posts
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.

#2
Guest_Jordan_*

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

#3
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
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?

#4
FlyByWire128

FlyByWire128

    Newbie

  • Members
  • PipPip
  • 16 posts
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

#5
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
Try adding the path:

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


#6
FlyByWire128

FlyByWire128

    Newbie

  • Members
  • PipPip
  • 16 posts
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?

#7
Guest_Jordan_*

Guest_Jordan_*
  • Guests
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.

#8
Crane

Crane

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 398 posts
How did you change the permission to 777 in Windows? You mean you just made it world?

#9
FlyByWire128

FlyByWire128

    Newbie

  • Members
  • PipPip
  • 16 posts
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.

#10
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
        $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...

#11
FlyByWire128

FlyByWire128

    Newbie

  • Members
  • PipPip
  • 16 posts
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...

#12
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
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