I've done a quick search of this forum, but I haven't found a solution that worked.

I'm running PHP 4.x with an Apache web server on Macintosh OS 10.3.9. It's an internal school web server that sits on my desk, so I have complete control over it.

I've hit a wall with a script I'm working on right now. This is not a web page. It's a php application that reads a directory and uses the data it gathers to write a web page that lets students view thumbnails of and vote on other students' graphic or Flash projects.

I've changed the folder and file permissions to 777, but I'm still not able to fwrite to or fclose the text file that I can successfully fopen.

========== This is my code ($poll is defined above as "f1.php")
133 $filename = $poll;
134 $fh = fopen($filename, "a") || die("Could not open $filename\n");
135 fwrite($fh, $pageBody) || die("Could not write to $filename\n");
136 fclose($fh);

========== This is the error message I'm getting
Warning: fwrite(): supplied argument is not a valid stream resource in /Library/WebServer/Documents/evaluations/evalGen.php on line 135
Could not write to f1.php

Does anyone have a clue what's amiss here?

    I think you might want to replace the "|| die()" expressions with "or die()". This is because the "||" operator has a higher precedence than the "=" operator, so what I think is going on is that you are assigning the result of fopen($filename, "a") || die("....") to the variable $fh, which will then be a boolean TRUE or FALSE instead of a resource ID. Since "or" has a lower precedence, it should then do what you want. (The other option would be to use parentheses around the clauses to be evaluated by "||", but it seems simpler and easier to read to just use "or" instead.)

      Second option is that the filename does not sit in the root of you filesystem, and you therefore need to include the filesystem path to the file

        NoDog,

        Truthfully, I was very skeptical about substituting or for || to solve my problem. It worked. Thank you!

        JK

        Leatherback,

        Your suggestion solved another problem that came up. The php application that wrote the web page could use relative file paths to write the page. However, that web page couldn't write the student votes to the file till I switched to /Library/Webserver/Documents/projects/2007b/F1/scores.txt . Thanks!

        Jk

          NP. Just mark the thread resolved if indeed so. (See thread tools)

            Write a Reply...