errors from fopen, fputs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rfigley
Forum Commoner
Posts: 70
Joined: Sun Apr 21, 2002 7:10 pm

errors from fopen, fputs

Post by rfigley »

Been using this code:

їphp]

$fp = fopen ("file.php", "w");

$Line1 = "this is a testn";
$Line2 = "test for line 2n";

echo $Line1;
echo $Line2 ;
fputs($fp, "this is a testn");
fputs($fp, "test for line 2n");

ї/php]


And I keep getting these errors: (regarding the permission denied, I"ve changed teh permission to 777. But still gives me that.

Warning: fopen("file.php", "w") - Permission denied in /home/account/public_html/template_test3.php on line 13
this is a testntest for line 2n
Warning: fputs(): supplied argument is not a valid File-Handle resource in /home/account/public_html/template_test3.php on line 20

Warning: fputs(): supplied argument is not a valid File-Handle resource in /home/account/public_html/template_test3.php on line 21



?>
jv
Forum Newbie
Posts: 3
Joined: Sat Sep 07, 2002 6:50 am

Post by jv »

are you sure you chmod'd the right "file.php"?
are you sure the path is correct?
what happens if you do...

include "file.php";

(type some text in it first)

does it find the file?

when it says "supplied argument is not a valid File-Handle resource " it means that no value has been assigned to $fp... i.e. couldn't open the file for writing.

also the "dir." that "file.php" is in might also have to be chmod 777
rfigley
Forum Commoner
Posts: 70
Joined: Sun Apr 21, 2002 7:10 pm

Post by rfigley »

hmm...some good questions here. Actaully the current script is template_test2.php as you can see form teh error. Tha is actaully th file I am giving the permissions. Put I guess I'd need an actual "file.php" tht I chmod or as you say chmod the directory.

Regarding the "supplied argument is not a valid File-Handle resource "It would seem that I've created a bit of a catch22 for myself. Need the file.php to be there and it can't create it.

OK, from a previous example I was reading from someone else teh example was given to use fopen as:

$fp = fopen ("http://www.domain.com/file.php", "w");

Which gave me:

Warning: fopen("http://www.npiflorida.com/file.php", "w") - Success in /home/account/public_html/template_test3.php on line 13

Why would I get a success waring?
rfigley
Forum Commoner
Posts: 70
Joined: Sun Apr 21, 2002 7:10 pm

Post by rfigley »

OK it seems to be working by creating the file.php then writing to it. However in the statement:

$fp = fopen ("file.php", "w");

Doesn't the "w" make it write the "file.php" if it doesn't exist? I checked the directory and chmod 777 was in place.

The ability to write the file if it doesn't exist is pretty important because this script will be used to write a file that doesn't exist and the name of the file wil be entered in the form that will create it when submitted.
rfigley
Forum Commoner
Posts: 70
Joined: Sun Apr 21, 2002 7:10 pm

Post by rfigley »

OK upon further attempts, it is now creating a file that doesn't exist. Just as I need it to. It wasn't working before. Any idea why?
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post by gite_ashish »

hi,

from php man:
'w' - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
i think u r script is trying to create the file, because the script is giving the error:
Warning: fopen("file.php", "w") - Permission denied in /home/account/public_html/template_test3.php on line 13

this can happen if the directory in which this file is getting created is not having proper rights for php to do so. ie if one want to create file "user1.txt" in /home/www/site.com/userdata/user1.txt, the "userdata" directory should have rights for php.
drwxr-xr-x 2 nobody nobody 4096 Sep 7 20:09 userdata
my web server is running as nobody:nobody for user:group.
either set like this of "chmod 777 userdata" will always work !
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

CHMOD file.php to 777.
Use this

Code: Select all

<?php
$fp = fopen("file.php","a");
?>

Code: Select all

'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
Post Reply