Page 1 of 1

Can open and read a file but can't chmod or write to it!

Posted: Tue Dec 29, 2009 6:15 am
by Don Dean
Hi everyone.
I'm getting totally frustrated trying to open and write data to a text file.
Here's a section of the code I've been working with. I get different results depending on whether I try to open the file with "r" or "a".
Here's the code with a "r" open:

Code: Select all

<?php
if ($_POST['submitbutton'] == "Submit this Form")
{
$file_name = "http://www.mywebsitename.com/Success/formdata/formdata.txt" or exit(" <br /> No file");
 
$open_file = fopen($file_name, "r") or exit(" <br /> Unable to open file!");
 
$display_contents = fread($open_file,17);
echo " <br /> FILE CONTENTS = " . $display_contents;
 
if (file_exists($open_file)) 
    echo " <br /> The file formdata.txt exists. <br /> ";
else 
    echo " <br /> The file formdata.txt does not exist. <br /> ";
 
chmod($open_file,0777) or exit(" <br /> Unable to change chmod");
fclose($open_file);
}
?>
Using this code, these are the messages I get.
FILE CONTENTS = text inside file
The file formdata.txt does not exist.

Warning: chmod(): No such file or directory in /home/content/d/o/n/dondean/html/Success/form/completion.php on line 214

Unable to change chmod


As you can see, the file read was successful.
The contents of the file were: "text inside file".
Yet chmod tells me the file does not exist.

Now if I change the "r" to "a", I get these messages:

Warning: fopen(http://www.mywebsitename.com/Success/fo ... rmdata.txt): failed to open stream: HTTP wrapper does not support writeable connections. in /home/content/d/o/n/dondean/html/Success/form/completion.php on line 204

Unable to open file!


My host is "Godaddy" on a Linux server. I've changed the permissions on the file and the directory to web user "read,write and execute".
I've also tried running the code without the file existing, to see if it would be created with the fopen statement, but it does not.

Can anyone give me some help at to what the problem is?
Thanks

Re: Can open and read a file but can't chmod or write to it!

Posted: Tue Dec 29, 2009 6:29 am
by requinix

Code: Select all

$file_name = "http://www.mywebsitename.com/Success/formdata/formdata.txt"
You can't write to files over the Internet. That's what you're telling it to do.

Perhaps you want to write to a file on the server?

Re: Can open and read a file but can't chmod or write to it!

Posted: Tue Dec 29, 2009 7:44 am
by Don Dean
tasairis wrote:Warning: I'm known for being blunt.
Blunt? I don't mind if I can get the problem solved.
I'm new to PHP and don't have a lot of knowledge about servers.
Have been strictly an internet HTML and CSS junkie.

How do I get the path name for the server?
Will that be listed somewhere in my hosting information or will I need to contact my Host?

Don

Re: Can open and read a file but can't chmod or write to it!

Posted: Tue Dec 29, 2009 9:41 am
by AbraCadaver
From the path in your errors, I would say:

Code: Select all

$file_name = '/home/content/d/o/n/dondean/html/Success/form/formdata.txt';
Or actually, since it's in the same directory, this should work:

Code: Select all

$file_name = 'formdata.txt';

Re: Can open and read a file but can't chmod or write to it!

Posted: Wed Dec 30, 2009 1:48 am
by Don Dean
Still not working. Here's my code:

Code: Select all

<?php
if ($_POST['submitbutton'] == "Submit this Form")
{
$file_name = "/home/content/d/o/n/dondean/html/Success/formdata/formdata.txt" or exit(" <br /> No file");
$open_file = fopen($file_name, "r") or exit(" <br /> Unable to open formdata.txt");
 
$display_contents = fread($open_file,17);
echo " <br /> FILE CONTENTS = " . $display_contents;
 
file_exists($open_file) or exit(" <br /> Cant find formdata.txt");
 
chmod($open_file,0777) or exit(" <br /> Unable to change formdata.txt chmod");
fclose($open_file);
}
?>
 
Here's my messages:
FILE CONTENTS = text inside file
Cant find formdata.txt

Re: Can open and read a file but can't chmod or write to it!

Posted: Wed Dec 30, 2009 3:21 am
by requinix
$open_file is a file handle. You use it to fread, fwrite, fclose, and so on.

You don't use it as a filename, which is what file_exists, chmod, and every other function wants.

Re: Can open and read a file but can't chmod or write to it!

Posted: Wed Dec 30, 2009 6:00 am
by Don Dean
Thanks a bunch Tasairis!
Everything is working great now.
tasairis wrote:$open_file is a file handle. You use it to fread, fwrite, fclose, and so on.

You don't use it as a filename, which is what file_exists, chmod, and every other function wants.
As a PHP novice, can you help me with a general rule, if there is one?

When do you use a "file handle" and when should you use the file name when coding?

TANKS.....

Re: Can open and read a file but can't chmod or write to it!

Posted: Wed Dec 30, 2009 8:17 am
by AbraCadaver
Its a file system pointer resource. The very first thing at the top of each manual page for a function is a description:

Code: Select all

resource fopen  ( [b]string $filename[/b]  , string $mode  [, bool $use_include_path = false  [, resource $context  ]] )
 
string fread  ( [b]resource $handle[/b]  , int $length  )
 
bool chmod  ( [b]string $filename[/b]  , int $mode  )
See, they tell you what the parameters are and also what the function returns.

If you look below that it will be even more descriptive for the parameters and return value.

Re: Can open and read a file but can't chmod or write to it!

Posted: Thu Dec 31, 2009 2:58 am
by Don Dean
AbraCadaver wrote:Its a file system pointer resource. The very first thing at the top of each manual page for a function is a description:

See, they tell you what the parameters are and also what the function returns.

If you look below that it will be even more descriptive for the parameters and return value.
I see. Now it makes perfectly good sense.
A few words of clairification can go a long way!

Thanks for pointing that out to me AbraCadaver.
Don