Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
FuelPHP - CodeIgniter Upload Class adapted
  • Hey guys! I have met FuelPHP last week, soo I am really new with it. After reading all the documentation and working my first project to try the framework I see a problem (specific to my condition) with the Upload Class, it doesn't work for my needs, it do an all upload files control, its to widespread for me. Soo I get the CI_Upload.php class (from the version 1.7.2) and migrated it to FuelPHP. I know that my solution its temporally, and the FuelPHP Core is working on a lot of new stuff (including Upload and Form classes), soo while it doesn't come, this works for me. (and for anyone it does too). The code is here: https://github.com/dantetekanem/fuel_ci_upload Hope it helps anyone out too :) It works in the exactly same way as code igniter.
  • Can you explain what your issues where? I honestly don't see the difference:
    // CodeIgniter example
     function do_upload()
     {
      $config['upload_path'] = './uploads/';
      $config['allowed_types'] = 'gif|jpg|png';
      $config['max_size'] = '100';
      $config['max_width']  = '1024';
      $config['max_height']  = '768';
    
      $this->load->library('upload', $config);
    
      if ( ! $this->upload->do_upload())
      {
       $error = array('error' => $this->upload->display_errors());
    
       $this->load->view('upload_form', $error);
      }
      else
      {
       $data = array('upload_data' => $this->upload->data());
    
       $this->load->view('upload_success', $data);
      }
     }
    

    vs
    // same in FuelPHP
     function do_upload()
     {
      // Custom configuration for this upload
      $config = array(
          'path' => DOCROOT.DS.'uploads/',
          'ext_whitelist' => array('img', 'jpg', 'jpeg', 'gif', 'png'),
      );
    
      // process the uploaded files in $_FILES
      Upload::process($config);
    
      // if there are any valid files
      if (Upload::is_valid())
      {
       // save them according to the config
       Upload::save();
    
       // call a model method to update the database
       Model_Uploads::add(Upload::get_files());
      }
    
      // and process any errors
      foreach (Upload::get_errors() as $file)
      {
       // $file is an array with all file information,
       // $file['errors'] contains an array of all error occurred
       // each array element is an an array containing 'error' and 'message'
      }
     }
    
  • The difference is with the do_upload() function, which has a param to define what $_FILES field will be procced with the specific configuration (or fields).
    For example, I have two <input type="file" /> submitted for my system, one is AVATAR and the other is SIGNATURE, but I don't them to stay in the same config directory, in reallity, they don't have any config equal at all, file size and etc. But with the FuelPHP everything(All of the $_FILES, which I honestly didn't understand why) is uploaded to the same directory, the same configurations, and I don't have any control in what I put or remove.
    I saw some solutions still using the Upload original class, but unset an variable from $_FILES isn't functional and copy from my destination directory to my correctly directory sounds more like a bad solution than other thing. Soo the CI_Upload class worked as I liked, the code was simple to adapted... and well, there is it. :3
  • In our upload class, per file processing is done via callbacks you can register. Sort of like hooks in the CI world, but then defined dynamically in your controller. We have choosen for this solution because there are a million and one ways you might want to treat the different files that have been uploaded. Hardcoded processing will never be able to handle all those. We also think that most of these processing rules (like for example the way CI uses the sizes) are very application and situation specific. And therefore don't belong in the framework. The framework should support the developer, not dicate how things should be done. The fact that this solution works for you, but not for me, is proof of that.
  • I see. Do you have an example of any callback? I did looked on the original class to work for my solution, but I failed on it. Soo I migrated the old one class.
    But I am really in love with FuelPHP and everything you guys have done, if I can adapt my solution in the right way will be gorgeous! I really want to work with FuelPHP now and create libraries and classes to share with everyone. Sorry if I sounds ignorant in any way, wasn't my intention. thanks!
  • No problem. If someone has a decided not to use something provided by the framework that means we're doing something wrong. Either in code or in documentation. So I tend to ask about it, to understand the reasoning behind it. There a simple example in the docs, see http://docs.fuelphp.com/classes/upload/usage.html#/method_register There are three hooks available:
    - validate, runs in process() after the standard validation is done, called for all uploaded files
    - before, runs in save() before the file is saved to disk, called for validated files only
    - after, runs in save() after the file has been saved to disk, called for validated files only Which one you use depends on what you want to do. For custom filenames and/or paths, you would use "before". The callback method is called for every file, and gets the file array passed (with the fields as documented). It's passed by reference, you can use the contents to determine your actions, and you can alter it's value (for example set a custom save path if it's a zip file larger then 2MB, or run an image resize if it's a jpeg and set a new source filename).
  • Oh. I got it! Thanks Harro. :D It's very cool the way you intervened on my question, for sure any time I have a question or a answer I'll be participating on the forum. I have some other codes to share that I'm otimizating to Fuel (and at the same time, learning more).
    Now I'll try the hooks. Thanks!
  • No problem, it's a way for us to see how we can improve the framework. This discussion has made me start to think about how to change the Upload class to make it easier to deal with multiple uploads which require different treatment. If you got five on your form you'll end up writing a lot of callback code for stuff the class can do itself...
  • I see... an option array() to the process() function to filter that fields should do the trick, at least for my case. Upload::process(); // process $_FILES normal
    Upload::process('avatar'); // process only $_FILES
    Upload::process(array('avatar', 'signature', 'attachment')); // process $_FILES, $_FILES and $_FILES But I don't know if in this way it respects the Fuel philosophy...

Howdy, Stranger!

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

In this Discussion