note 113149 added to class.splfileobject
| From: | LarsGyrupBrinkNielsen | Date: | Thu, 05 Sep 2013 13:46:47 +0000 |
| Subject: | note 113149 added to class.splfileobject | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196174@lists.php.net to get a copy of this message | ||
Note that this class has a private (and thus, not documented) property that holds the file pointer.
Combine this with the fact that there is no method to close the file handle, and you get into
situations where you are not able to delete the file with unlink(), etc., because an SplFileObject
still has a handle open.
To get around this issue, delete the SplFileObject like this:
---------------------------------------------------------------------
<?php
print "Declaring file object\n";
$file = new SplFileObject('example.txt');
print "Trying to delete file...\n";
unlink('example.txt');
print "Closing file object\n";
$file = null;
print "Deleting file...\n";
unlink('example.txt');
print 'File deleted!';
?>
---------------------------------------------------------------------
which will output:
---------------------------------------------------------------------
Declaring file object
Trying to delete file...
Warning: unlink(example.txt): Permission denied in file.php on line 6
Closing file object
Deleting file...
File deleted!
---------------------------------------------------------------------
----
Server IP: 69.147.83.199
Probable Submitter: 5.56.157.83
----
Manual Page -- http://php.net/manual/en/class.splfileobject.php
Edit -- https://master.php.net/note/edit/113149
Del: integrated -- https://master.php.net/note/delete/113149/integrated
Del: useless -- https://master.php.net/note/delete/113149/useless
Del: bad code -- https://master.php.net/note/delete/113149/bad+code
Del: spam -- https://master.php.net/note/delete/113149/spam
Del: non-english -- https://master.php.net/note/delete/113149/non-english
Del: in docs -- https://master.php.net/note/delete/113149/in+docs
Del: other reasons-- https://master.php.net/note/delete/113149
Reject -- https://master.php.net/note/reject/113149
Search -- https://master.php.net/manage/user-notes.php