Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Issue with upload class
  • I have the following code:
        Upload::register('before', function (&$file) use ($article) {
         var_dump($file);
         $file['saved_to'] .= $article->thumbnail;
         $file['saved_as'] = $article->id;
         $file['name'] = $article->id;
         var_dump($file);
         var_dump($article->thumbnail);
        });
        
        Upload::process();
        
        Upload::save();
    

    However, when I upload files, they are simply saved to the default path specified in my config/file.php file. Here's the output of the var_dumps() above:
    var_dump($file); (before closure is finished)
    array
      'name' => string 'signature.jpg' (length=13)
      'type' => string 'image/jpeg' (length=10)
      'error' => int 0
      'size' => int 12631
      'field' => string 'thumbnail' (length=9)
      'key' => boolean false
      'file' => string '/private/var/tmp/phpTr2Lri' (length=26)
      'extension' => string 'jpg' (length=3)
      'filename' => string 'signature' (length=9)
      'mimetype' => string 'image/jpeg' (length=10)
      'message' => string 'The file uploaded with success' (length=30)
      'saved_to' => string '/Volumes/Storage/Projects/soanime/public/assets/img/articles/' (length=61)
      'saved_as' => string 'signature.jpg' (length=13)
    
    var_dump($file); (after closure is finished)
    array
      'name' => string '1' (length=1)
      'type' => string 'image/jpeg' (length=10)
      'error' => int 0
      'size' => int 12631
      'field' => string 'thumbnail' (length=9)
      'key' => boolean false
      'file' => string '/private/var/tmp/phpTr2Lri' (length=26)
      'extension' => string 'jpg' (length=3)
      'filename' => string 'signature' (length=9)
      'mimetype' => string 'image/jpeg' (length=10)
      'message' => string 'The file uploaded with success' (length=30)
      'saved_to' => string '/Volumes/Storage/Projects/soanime/public/assets/img/articles/c/8/' (length=65)
      'saved_as' => string '1' (length=1)
    
    var_dump($article->thumbnail);
    string 'c/8/' (length=4)
    

    The file ends up saved to articles/signature.jpg (or signature_1.jpg, etc). Any ideas? Is this a bug?
  • I'm not in a position to test at the moment. I see in the code (line 556) that the array is passed by reference, that doesn't seem right. Could you change that, and see if that is the culprit?
  • Harro Verton wrote on Thursday 28th of July 2011:
    I'm not in a position to test at the moment. I see in the code (line 556) that the array is passed by reference, that doesn't seem right. Could you change that, and see if that is the culprit?

    I just tried without passing it by reference, and that didn't work either. The fuel documentation shows it should be passed by reference, and from everything I know about PHP it makes sense that it would be passed by reference as I'm not returning $file at the end of the closure, and the changes need to make it out of the closure's scope. I'm starting to wonder if this is somehow a bug in the Upload class...
  • The issue was the folder didn't exist, and apparently the Upload class only tries to create the 'path' set in config, not the full path set for each file. Not sure if this is a bug or the intended behavior, but the documentation seems ambiguous here.
  • Pushed an update that add's a re-check of the saved_to path after the callback. It was intended behaviour. i.e. if you alter something in the callback, you are supposed to know what your doing, and return a $file array that is still valid. I'll update the docs to make this more clear.

Howdy, Stranger!

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

In this Discussion