我试图在不使用内存或临时文件的情况下创建一个“虚拟”文件。 “虚拟”文件需要通过使用 file_exists() 进行的检查,同时在使用 require 或 include 时不会抛出任何错误或警告。
Allows you to implement your own protocol handlers and streams for use with all the other filesystem functions (such as
fopen(),fread()etc.).
...其中 file_exists() 是其中之一。 The docs page状态:
As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support
stat()family of functionality.
我的尝试是构建一个自定义的虚拟文件包装器
class VirtualFileWrapper
{
public $context;
public function stream_open( $path, $mode, $options, &$opened_path )
{
return TRUE;
}
public function stream_stat()
{
var_dump( __METHOD__ );
$data = [
'dev' => 0,
'ino' => getmyinode(),
'mode' => 'r',
'nlink' => 0,
'uid' => getmyuid(),
'gid' => getmygid(),
'rdev' => 0,
'size' => 0,
'atime' => time(),
'mtime' => getlastmod(),
'ctime' => FALSE,
'blksize' => 0,
'blocks' => 0,
];
return array_merge( array_values( $data ), $data );
}
}
stream_wrapper_register( 'virtual', 'VirtualFileWrapper' );
$file = fopen( "virtual://foo", 'r+' );
// Executes VirtualFileWrapper::stream_stat()
fstat( $file );
// Executes no VirtualFileWrapper method
file_exists( $file );
fstat() 函数执行该方法时,file_exists() 不执行任何流类方法。
如何让虚拟流包装器工作(使用 file_exists())?
我完全知道 tempnam( __DIR__, '' ) 将同时通过:
var_dump( tempnam( __DIR__, '' ) ); 返回 truerequire tempnam( __DIR__, '' ); 没有错误但我不想使用临时文件,因为可能有更好的方法(性能方面)。
最佳答案
看起来你只需要在 VirtualFileWrapper 上实现一个公共(public)的 url_stat() 方法就可以通过 file_exists() 检查。
要消除来自include 和require 的警告和错误,您必须实现stream_read() 和stream_eof() 方法:
class VirtualFileWrapper
{
public $context;
public function stream_open( $path, $mode, $options, &$opened_path )
{
return TRUE;
}
public function stream_stat()
{
var_dump( __METHOD__ );
return [];
}
public function url_stat()
{
return array (
0 => 0,
1 => 0,
2 => 0,
3 => 0,
4 => 0,
5 => 0,
6 => 0,
7 => 0,
8 => 0,
9 => 0,
10 => 0,
11 => 0,
12 => 0,
'dev' => 0,
'ino' => 0,
'mode' => 0,
'nlink' => 0,
'uid' => 0,
'gid' => 0,
'rdev' => 0,
'size' => 0,
'atime' => 0,
'mtime' => 0,
'ctime' => 0,
'blksize' => 0,
'blocks' => 0
);
}
public function stream_read(){
return '';
}
public function stream_eof(){
return '';
}
}
stream_wrapper_register( 'virtual', 'VirtualFileWrapper' );
$file = fopen( "virtual://foo", 'r+' );
// Executes VirtualFileWrapper::stream_stat()
fstat( $file );
// Executes no VirtualFileWrapper method
file_exists("virtual://foo");
//Still no errors :-)!
require "virtual://foo";
include "virtual://foo";
注意向 file_exists() 传递一个字符串,而不是您使用 fopen() 创建的资源。
关于php - 如何创建一个假的/"virtual"文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37553914/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题