取消链接和SplFileObject [英] Unlink and SplFileObject

查看:209
本文介绍了取消链接和SplFileObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从SplFileObject中取消链接文件?

Is it possible to unlink a file from an SplFileObject?

我没有看到关闭底层资源的方法,而且文件句柄是私人的,

I don't see a method to close the underlying resource, and the file handle is private so one can't extend SplFileObject with that goal in mind.

是否有任何解决方法?

推荐答案

我不会推荐这个,因为PHP关闭了你背后的文件。如果你看一下php src, ext / spl / spl_directory.c

I would not recommend this, because PHP closes the file behind scenes for you. If you take a look at the php src, ext/spl/spl_directory.c:

retval.handle = zend_objects_store_put(intern, 
  (zend_objects_store_dtor_t) zend_objects_destroy_object, 
  (zend_objects_free_object_storage_t) spl_filesystem_object_free_storage, 
   NULL TSRMLS_CC);

处理程序的设置是为了在所有引用已经用尽时处理对象的清理。现在,我们检查清理处理程序: spl_filesystem_object_free_storage
$ b

A handler is setup in order to deal with the cleanup of the object when all references have been exhausted. Now, we check the cleanup handler: spl_filesystem_object_free_storage:

    case SPL_FS_FILE:
        if (intern->u.file.stream) {
            if (intern->u.file.zcontext) {
/*              zend_list_delref(Z_RESVAL_P(intern->zcontext));*/
            }
            if (!intern->u.file.stream->is_persistent) {
                php_stream_free(intern->u.file.stream, PHP_STREAM_FREE_CLOSE);
            } else {
                php_stream_free(intern->u.file.stream, PHP_STREAM_FREE_CLOSE_PERSISTENT);
            }
            if (intern->u.file.open_mode) {
                efree(intern->u.file.open_mode);
            }
            if (intern->orig_path) {
                efree(intern->orig_path);
            }
        }
        spl_filesystem_file_free_line(intern TSRMLS_CC);
        break;

php_stream_free 调用将关闭文件流为你。如果你不链接文件,我不能保证PHP将如何处理你刚才链接的文件句柄。

The php_stream_free call will close the file stream for you. If you unlink the file, I can't guarantee how PHP will handle trying to close the file handle you just linked.

你必须记住SplFileObject提供了什么你:

You have to keep in mind what the SplFileObject provides you:

SplFileObject extends SplFileInfo implements RecursiveIterator , Traversable , Iterator , SeekableIterator {

它为文件提供了许多基于迭代器的接口。如果你 unlink 这个文件,它应该迭代什么?您会注意到 close()在可用方法中不存在。如果你想做你所说的话,那么你最好把文件作为资源来处理,你可以在 close()这个句柄中使用它, unlink(),节省不愉快的副作用。

It's provides many iterator based interfaces for a file. If you unlink the file, what is it supposed to iterate over? You'll notice that close() is not present in the available methods either. If you want to do what you're saying, then you're better off handling the file as a resource, where you can close() the handle and make it usable with unlink(), saving from nasty side effects.

这篇关于取消链接和SplFileObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆