Doubts regarding change of file access mode...
Moderator: General Moderators
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
Doubts regarding change of file access mode...
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?
- 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...
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 worksraghavan20 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?
- 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...
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?d11wtq wrote: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 worksraghavan20 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?The handle created when you open a file in PHP is nothing more than a pointer to an open file.
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
I will make the question still more clearer with the code...
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+");
$fp = fopen("trial.txt", "a+");Code: Select all
$fp = fopen("trial.txt", "w+");
unset($fp);
$fp = fopen("trial.txt", "a+");Code: Select all
<?php
$fp = fopen('file.txt', 'a+');
fclose($fp);
$fp = fopen('file.txt', 'w');
//etc..
?>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..- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact: