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

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
Don Dean
Forum Newbie
Posts: 5
Joined: Tue Dec 29, 2009 5:42 am

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

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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?
Don Dean
Forum Newbie
Posts: 5
Joined: Tue Dec 29, 2009 5:42 am

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

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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';
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Don Dean
Forum Newbie
Posts: 5
Joined: Tue Dec 29, 2009 5:42 am

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

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
Don Dean
Forum Newbie
Posts: 5
Joined: Tue Dec 29, 2009 5:42 am

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

Post 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.....
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Don Dean
Forum Newbie
Posts: 5
Joined: Tue Dec 29, 2009 5:42 am

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

Post 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
Post Reply