dongpo7467
2015-05-21 19:25
浏览 98
已采纳

获取symfony中任何文件的绝对路径

I'm trying to follow the small tutorial here http://symfony.com/doc/current/components/finder.html but I'm just absolutely lost on how to actually find a file. I guess I have two problems to solve.

1) What's a good method in Symfony to determine the /path/to/src from say the controller?

2) How do I use Finder to get the absolute path to my desired file?

In a partial answer to 1 I'm aware of $this->get('kernel')->getRootDir() but that points to /path/to/app and I guess I could just add ../src to the end but I would like something a little cleaner.

I would prefer to crawl through subdirectories rather than specify just a single folder to look in.

Here's what I've tried

/* In this case the actual file exists in src/MyBundle/config/myFile.xml */
$finder = new Finder();

$finder->files()->name('myFile.xml')->in('/path/to/src');

Or I've also done.

$finder->files()->name('myFile.xml')->in('/path/to/src/MyBundle/config');

Finder throws an error if the directory doesn't exist. So I know there's not an error with my path string. But when I try to foreach($finder as $file) through the results or $finder->getIterator() I just end up with NULL every time.

Thanks.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试按照这里的小教程 http://symfony.com/doc/current/components/finder.html 但我完全迷失了如何实际查找文件 。 我想我有两个问题需要解决。 </ p>

1)Symfony从控制器中确定 / path / to / src </ code>的好方法是什么?</ p>

2)如何使用Finder获取所需文件的绝对路径?</ p>

在1的部分答案中我知道 $ this-&gt; get( 'kernel') - &gt; getRootDir()</ code>但指向 / path / to / app </ code>,我想我可以添加 ../ src </ code>到 结束,但我希望有点清洁。</ p>

我更喜欢爬行子目录,而不是只指定一个文件夹来查看。</ p>

这是我尝试过的</ p>

  / *在这种情况下,实际文件存在于src / MyBundle / config / myFile.xml * / 
 $ finder = new Finder(  ); 
 
 $的finder-&GT;文件() - &GT;名称( 'myfile.xml中') - &gt;在( '/路径/到/ SRC'); 
 </代码> </ PRE>  
 
 

或者我也完成了。</ p>

  $ finder-&gt; files() - &gt; name('myFile.xml') - &gt;  in('/ path / to / src / MyBundle / config'); 
 </ code> </ pre> 
 
 

如果目录不存在,Finder会抛出错误。 所以我知道我的路径字符串没有错误。 但是当我尝试通过结果 foreach($ finder as $ file)</ code>或 $ finder-&gt; getIterator()</ code>时,我最终得到 NULL </ 代码>每次。</ p>

谢谢。</ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题