天道酬勤,学无止境

splfileobject

取消链接和 SplFileObject(Unlink and SplFileObject)

问题 是否可以从 SplFileObject 取消链接文件? 我没有看到关闭底层资源的方法,并且文件句柄是私有的,因此无法在考虑到该目标的情况下扩展 SplFileObject。 有什么解决方法吗? 回答1 我不建议这样做,因为 PHP 会在幕后为您关闭文件。 如果你看一下 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 : 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

2022-01-16 10:12:32    分类:技术分享    php   file   unlink   splfileobject

Unlink and 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. Are there any workarounds?

2022-01-15 20:43:07    分类:问答    php   file   unlink   splfileobject

使用 PHP 将制表符分隔的字符串转换为 XML(tab-delimited string to XML with PHP)

问题 我正在尝试将变量的制表符分隔内容写入 XML,如下所示: $tsvData = str_getcsv($input, "\t"); foreach($tsvData as $line => $row) { if($line > 0) { $xmlWriter->writeElement('NAME', $row[0]); $xmlWriter->writeElement('CAKE', $row[1]); $xmlWriter->writeElement('BODYPART', $row[2]); } } 但它只为每个 XML 标签写入一个字符,而不是每个选项卡之间的所有内容。 当我使用 SplFileObject 时,从文件中获取相同的 tsv 数据,它可以工作。 我在使用 str_getcsv 函数时做错了什么? 谢谢 回答1 str_getcsv()函数返回一个一维数组,但您将其str_getcsv()返回一个二维数组。 编辑: 澄清str_getcsv() , str_getcsv()没有“行”的概念。 而不是这样做: $tsvData = str_getcsv($input, "\t"); 考虑到你会得到一个行数组,每行包含一个列数组,你必须做这样的事情: $lines = explode("\n", $input); $tsvData = array()

2021-12-07 14:53:11    分类:技术分享    php   xml   csv   splfileobject

tab-delimited string to XML with PHP

I'm trying to write the tab-delimited content of a variable to XML like this: $tsvData = str_getcsv($input, "\t"); foreach($tsvData as $line => $row) { if($line > 0) { $xmlWriter->writeElement('NAME', $row[0]); $xmlWriter->writeElement('CAKE', $row[1]); $xmlWriter->writeElement('BODYPART', $row[2]); } } But it's only writing one character per XML tag instead of everything between each tab. When I use SplFileObject, geting the same tsv data but from a file, it works. What am I doing wrong with the str_getcsv function? Thanks

2021-11-11 15:30:21    分类:问答    php   xml   csv   splfileobject

$file->eof() 在 'r' 模式下使用 PHP 的 SplFileObject 时总是返回 false($file->eof() always returning false when using PHP's SplFileObject in 'r' mode)

问题 为什么我的 PHP 脚本挂了? $path = tempnam(sys_get_temp_dir(), '').'.txt'; $fileInfo = new \SplFileInfo($path); $fileObject = $fileInfo->openFile('a'); $fileObject->fwrite("test line\n"); $fileObject2 = $fileInfo->openFile('r'); var_dump(file_exists($path)); // bool(true) var_dump(file_get_contents($path)); // string(10) "test line // " var_dump(iterator_count($fileObject2)); // Hangs on this 如果我删除最后一行( iterator_count(... )并将其替换为: $i = 0; $fileObject2->rewind(); while (!$fileObject2->eof()) { var_dump($fileObject2->eof()); var_dump($i++); $fileObject2->next(); } // Output: // bool(false) // int(0) //

2021-07-28 16:46:32    分类:技术分享    php   fwrite   eof   splfileobject

PHP LimitIterator 失败(“不支持查找”+“无法倒带文件”)(PHP LimitIterator fails (“Does not support seeking” + “Cannot rewind file”))

问题 我使用SplFileObject和LimitIterator从大文件的位置 x 到 y 读取内容。 这在使用像/home/devel/stuff/myfile.log这样的文件路径时非常/home/devel/stuff/myfile.log 。 当使用像http://mydomain.com:8090/devel/stuff/myfile.log这样的路径时,它不起作用。 然而路径是正确的。 使用绝对路径时会失败吗? 错误消息是: PHP Warning: SplFileObject::rewind() [<a href='splfileobject.rewind'>splfileobject.rewind</a>]: stream does not support seeking in ... PHP Fatal error: Uncaught exception 'RuntimeException' with message 'Cannot rewind file ...' 完整代码: // $pStrFile contains the valid (yes!) path $oFile = new SplFileObject($pStrFile); // $nFrom = 80 and $nLines = 30 $fileIterator = new

2021-07-10 01:17:24    分类:技术分享    php   spl   splfileobject

$file->eof() always returning false when using PHP's SplFileObject in 'r' mode

Why is my PHP script hanging? $path = tempnam(sys_get_temp_dir(), '').'.txt'; $fileInfo = new \SplFileInfo($path); $fileObject = $fileInfo->openFile('a'); $fileObject->fwrite("test line\n"); $fileObject2 = $fileInfo->openFile('r'); var_dump(file_exists($path)); // bool(true) var_dump(file_get_contents($path)); // string(10) "test line // " var_dump(iterator_count($fileObject2)); // Hangs on this If I delete the last line (iterator_count(...) and replace it with this: $i = 0; $fileObject2->rewind(); while (!$fileObject2->eof()) { var_dump($fileObject2->eof()); var_dump($i++); $fileObject2->next

2021-07-02 18:23:35    分类:问答    php   fwrite   eof   splfileobject

PHP LimitIterator fails (“Does not support seeking” + “Cannot rewind file”)

I use SplFileObject and LimitIterator to read content from position x till y of a big file. This works perfectly when using a file path like /home/devel/stuff/myfile.log. When using a path like http://mydomain.com:8090/devel/stuff/myfile.log it does not work. The path is correct however. Does this fail when using absolute paths? The error messages are: PHP Warning: SplFileObject::rewind() [<a href='splfileobject.rewind'>splfileobject.rewind</a>]: stream does not support seeking in ... PHP Fatal error: Uncaught exception 'RuntimeException' with message 'Cannot rewind file ...' Full code: //

2021-06-29 09:39:28    分类:问答    php   spl   splfileobject

RuntimeException SplFileInfo :: getSize():统计信息因……Laravel 4上传图片而失败(RuntimeException SplFileInfo::getSize(): stat failed for… Laravel 4 upload image)

问题 我使用laravel 4(最新更新) ,我创建了一个可以上传图像(徽标/头像)的表单。 我在MAC OS上,我使用sublime Text 3 , laravel和MAMP Applications 。 我所有的配置都正确,没有运行问题。 我的问题是,当我从表单提交到达字段时遇到此错误: RuntimeException SplFileInfo :: getSize():/ Applications / MAMP / tmp / php / phpRUJfMX的统计信息失败 这是我的表单nameOfTheForm.blade.php中的代码: @extends('layout.default') @section('title') Name Of My Project - EditProfile @stop @section('content') {{Form::open(array('url'=>'uploadAvatar','files' => true))}} <p> {{Form::label('pseudo','pseudo (*): ')}} {{Form::text('pseudo',Input::old('nom'))}} </p> @if ($errors->has('pseudo')) <p class='error'> {{ $errors->first

2021-05-26 01:16:41    分类:技术分享    php   file-upload   laravel-4   spl   splfileobject

RuntimeException SplFileInfo::getSize(): stat failed for… Laravel 4 upload image

I work with laravel 4 (last update), I create a form where we could upload an image (logo/avatar). I am on MAC OS, I use sublime Text 3, laravel and MAMP Applications. All my configuration is setting right, no running problems. My probleme is that I have this error when I submit the reaching fields from my form: RuntimeException SplFileInfo::getSize(): stat failed for /Applications/MAMP/tmp/php/phpRUJfMX Here's the code from my form nameOfTheForm.blade.php: @extends('layout.default') @section('title') Name Of My Project - EditProfile @stop @section('content') {{Form::open(array('url'=>

2021-04-20 17:59:22    分类:问答    php   file-upload   laravel-4   spl   splfileobject