Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Problem with File::read_dir when using area
  • I'm having trouble getting File::read_dir to recursively read a directory when limited to an area.
    My directory structure is as follows:
    DOCROOT
    -uploads
    --level1
    ---leve2a
    ---leve2b
    
    For example, the following code will throw an InvalidPathException.
    $depth = 0;   // a value of 2 will also throw the exception
    $config = array(
     'basedir'  => DOCROOT,
     'use_locks'  => true,
    );
    $area = \File::factory($config);
    
    try
    {
     $dir = $area->read_dir('uploads', $depth);
    }
    catch(\InvalidPathException $e)
    {
     $dir = 'operation failed';
    }
    
    Debug::dump($dir);
    
    No exception is thrown when var $depth is set to 1. Is it not intended for read_dir to be used recursively with areas?
  • @WanWizard You have been most helpful.
    The changes you commited to the develop branch solved the issue stated at the beginning of this thread.
    If I could trouble you once more...
    I am now trying to move the areas config to app/config/file.php as shown in the docs.
    Here is my config file:
    <?php
    
    return array(
     'areas' => array(
      'my_area' => array(
       'basedir'  => DOCROOT,
       'use_locks'  => true,
      ),
     ),
    );
    

    This is my call:
    $area = \File::instance('my_area');
    
    At this point I get the following error twice:
    Notice!
    
    ErrorException [ Notice ]: Object of class Fuel\Core\File_Area could not be converted to int
    
    COREPATH/classes/file.php @ line 49:
    
    48: {
    49: static::$areas[$name] = \File_Area::factory($config) + static::$base_area;
    50: }
    
    Could this be related? Or, is it me again.
    :)
  • Not you. I've found another bug, which is now fixed...
  • Thanks again.
  • I am now having trouble with File::create_dir().
    I have created directory DOCROOT/uploads with 0777 permissions.
    Consider the following:
    $basepath = DOCROOT.'uploads';
    
    if (file_exists($basepath) and is_dir($basepath))
    {
     \File::create_dir($basepath, 'level1');
    }
    else
    {
     echo 'File not found or is not a directory';
    }
    
    Fuel gives the following warning:
    Warning!
    
    ErrorException [ Warning ]: mkdir(): No such file or directory
    
    COREPATH/classes/file.php @ line 155:
    
    154: 
    155: return mkdir($new_dir, $chmod, $recursive);
    156: }
    
    Sorry to be so much trouble.
  • No trouble at all, I'm very happy that you've found these issues, it's difficult for us to test it all. This bug should now fixed...
  • Thanks, WanWizard. That did indeed fix the problem with creating directories.
    I've run into problems with File::create() and File::delete_dir().
    Rather than a wordy explanation, I believe the comments in the following code can say it better than I.
    $basepath = DOCROOT.'uploads';
    
    // clean up for testing
    rmdir($basepath.DS.'level1'.DS.'level2');
    rmdir($basepath.DS.'level1');
    
    // create a dir and subdir
    $rval1 = \File::create_dir($basepath, 'level1');
    $rval2 = \File::create_dir($basepath, 'level1'.DS.'level2');
    Debug::dump($rval1, $rval2);
    // Results: Directories are successfully created and
    //          both return values are true. Thanks, WanWizard.
    
    // create one file in each newly created directory
    $rval1 = \File::create($basepath.'level1',             'foo1.txt', 'contents of file foo1.txt');
    $rval2 = \File::create($basepath.'level1'.DS.'level2', 'foo2.txt', 'contents of file foo2.txt');
    Debug::dump($rval1, $rval2);
    // Results: Fuel throws an InvalidPathException.
    //          [ Error ]: Invalid basepath, cannot create file at this location.
    
    // delete newly created directories and their contents recursively
    $rval1 = \File::delete_dir($basepath, true, false);
    Debug::dump($rval1);
    // Results: Returns true but directories still exist.
    
    // delete subdirectory 'level2' only
    $rval1 = \File::delete_dir($basepath.DS.'level1'.DS.'level2', true, true);
    Debug::dump($rval1);
    // Results: Returns true and subdirectory is successfully deleted.
    
    Again, thanks so much.
  • This time not all Fuel bugs:
    // create one file in each newly created directory
    $rval1 = \File::create($basepath.'level1', 'foo1.txt', 'contents of file foo1.txt');
    $rval2 = \File::create($basepath.'level1'.DS.'level2', 'foo2.txt', 'contents of file foo2.txt');
    
    you're missing a directory separator here, as basepath doesn't end with one: $basepath.DS.'level1'. The recursive deletion problem was a bug, which is now fixed.
  • Thanks again, WanWizard.
  • You've bumped into a bug. The first time read_dir() is called, you pass it a path, relative to the area's basedir. When you use depth, it calls itself recursively, but with an absolute path, which fails, because the basedir always gets prepended. I've created issue #210 in the issue tracker on github, I'll try to fix this as soon as possible.
  • Issue has been fixed in the develop branch of Fuel/core.
  • Thanks, WanWizard.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion