Doubts regarding change of file access mode...

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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Doubts regarding change of file access mode...

Post by raghavan20 »

If I have a file opened in read mode and I have to change the mode to write or append then do I have to fopen again and assign it to the pointer...is there is any function to just specify the mode of access?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Doubts regarding change of file access mode...

Post by Chris Corbyn »

raghavan20 wrote:If I have a file opened in read mode and I have to change the mode to write or append then do I have to fopen again and assign it to the pointer...is there is any function to just specify the mode of access?
If you opened the file in readonly mode then you'd have to close and reopen it... this is beyond the control of PHP and is to do with how the filesystem works ;) The handle created when you open a file in PHP is nothing more than a pointer to an open file.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Re: Doubts regarding change of file access mode...

Post by raghavan20 »

d11wtq wrote:
raghavan20 wrote:If I have a file opened in read mode and I have to change the mode to write or append then do I have to fopen again and assign it to the pointer...is there is any function to just specify the mode of access?
If you opened the file in readonly mode then you'd have to close and reopen it... this is beyond the control of PHP and is to do with how the filesystem works ;) The handle created when you open a file in PHP is nothing more than a pointer to an open file.
say if we change the mode again using fopen again and assign it to the same pointer with had access to the file in different mode...then is the older reference killed or do I have to unset and assign again?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

The old file will remain open your pointer to it will just have been overwritten. Use fclose() whenever you are done with a file (all handles are explicitly closed at the end of the script, but having more files open at one time then you need is a resource hog)
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

I will make the question still more clearer with the code...

Code: Select all

$fp = fopen("trial.txt", "w+");
$fp = fopen("trial.txt", "a+");
Now will the second line kill the reference, $fp in the first line or do I have to use something like this...?

Code: Select all

$fp = fopen("trial.txt", "w+");
unset($fp);
$fp = fopen("trial.txt", "a+");
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

<?php

$fp = fopen('file.txt', 'a+');

fclose($fp);

$fp = fopen('file.txt', 'w');

//etc..

?>
Just as was described by jshpro2.. $fp is simply a callsign for the filepointer generated by fopen()

assigning a new value to $fp, be it another pointer or something completely different will not close the filepointer generated by fopen, fclose does this.

However, your question is different, but moot due to the above example bein the "best practice" of closing a filepointer when you have finished with it..

Code: Select all

<?php

$fp = fopen('file', 'a+');

//do stuff...


$fp = fopen('file', 'w');

$frwite ($fp, 'val'); //will write to the file 'file' .. the original filepointer generated for read-only is now 'lost' and is floating around in memory..
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

That clears off my doubt Jenk and jshpro....so I have to close explicitly or the memory is not deallocated...
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Yes :)
Post Reply