Findfiles Without Path

Hi to everyone

I want a list name file of specific folder but without the path

I found the

CFileHelper::findFiles('the server path/myfolder');

It works but I want only the name of files.

The reason for that is to concatenating the name with specific url to publish in web like , etc rather than

var/public_html/images/img1.jpg , var/public_html/images/img2.jpg

Obviusly I could use php function to extract the name of file of arraylist items like that

    foreach ($listfile as $key=>$val) {

         $listfile[$key] = basename($val);


but I want to reduce the consuming cpu and memory resources and using Yii Api function rather than php native function.

Is there way to do that directly like

CFileHelper::findFiles('the server path/myfolder',array('...onlyfilename...'=>true));

So, which is the best way ?

Thanks :)

I would imagine that the processing time involved in using basename() on each entry is negligible compared to the initial file system access to retrieve the names. Certainly, using a PHP inbuilt function is going to be faster than a Yii implemented OO approach, as the PHP libraries are built in C rather than interpreted at run time.

I’d just iterate through the files in the manner you suggested.

Keith, I agree with you

I was imagine the CFileHelper::findFiles had a parameter to fetch directly the name of files by using php function (so faster as you say) rather than to

fetching all the file names with absolute path and then take the name by basename (2 commands for 2 iterators)

In details:

-(the way to do that now)-

  1. get all files with absolute path

  2. get the received list and foreach item remove the absolute path

  3. get the modified list and concatenate with base url

-the way that I want

  1. get ditectly the name of files

  2. get the list and concatenate with base url

-the best way

  1. get the url of file (, foreach file

For few files there is no problem cpu-ram consuming but for many files and many http requests the differents may could be significant

Maybe the solution is the cache-system

You might want to make your own helper function that uses glob().

Yii’s function is designed to allow recursive scanning of directories, so returning just the file names wouldn’t be sensible in that context. This would need to be a separate function.

I still think that there’s no need to optimise this function though, you could just create a helper function to process the files using the method discussed previously.

Ok keith,

May be it is not necessary as I thought.

I prefer to use Yii Api as possible as but in a few cases I have to use native php function.

Thanks keith :)