Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
File Upload help / New to Fuelphp
  • Ok, I'm a php hacker, but not real deep programmer. (I am going to school for software dev though). Anyhow - I'm having trouble wrapping my head around where things go. My first fuel app is going to be a file manager that simply does the following Allows users to login, and upload contracts - pdf files. Each file will have a Name, and multiple 'revisions or versions'. My first part of this is figuring out how to upload period.. Here's what my Create function looks like in the File controller:
    public function action_create($id = null)
     {
      
      $config = array(
        'path' => DOCROOT.DS.'files',
        'randomize' => true,
        'ext_whitelist' => array('pdf'),
     );
    
     // 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'
     }
      if (Input::method() == 'POST')
      {
       $val = Model_File::validate('create');
       
       if ($val->run())
       {
        $file = Model_File::forge(array(
         'filename' => Input::post('filename'),
        ));
    
        if ($file and $file->save())
        {
         Session::set_flash('success', 'Added file #'.$file->id.'.');
    
         Response::redirect('file');
        }
    
        else
        {
         Session::set_flash('error', 'Could not save file.');
        }
       }
       else
       {
        Session::set_flash('error', $val->show_errors());
       }
      }
    
      $this->template->title = "Files";
      $this->template->content = View::forge('file/create');
    
     }
    

    And this is my view _form
    <?php echo Form::open(array('class' => 'form-stacked')); ?>
    
     <fieldset> <div class="clearfix"> <?php echo Form::label('Filename',
     'filename'); ?>
    
       <div class="input"> 
       <?php 
       echo FORM::file&#40;'filename'&#41;
       
       ?>
       
       <?php /* echo Form::input('filename',
       Input::post('filename', isset($file) ? $file->filename : ''), array('class' =>
       'span6'));  */ ?>
    
       </div> 
       
       </div> <div class="actions"> <?php echo Form::submit('submit',
       'Save', array('class' => 'btn primary')); ?>
    
      </div> </fieldset> <?php echo Form::close(); ?>
    

    Say for example I wanted to get just the filename without the .pdf - is there a way of doing that via the Fuel Class?
    What would the code be?
    If I wanted to get it's file-path, what would that be as well? I will start with this, and post follow ups as needed - till I figure it all out. I'm sure by the time I finish this project I'll be a pro.
    I'm also wondering if there are any tutorials for using phpunit or any other testing frameworks with fuelPHP.
  • Your form doesn't have the correct enctype, and you also need an input field of type 'file' to be able to upload files. As for file information, Upload::get_files() will return an array with all information it has on each file uploaded. The contents of that array is documented here: http://docs.fuelphp.com/classes/upload/usage.html PHPUnit support is build into FuelPHP, and run through our commandline utility "oil". Check the docs for more info. Quite a few of the core classes already have tests written, see fuel/core/tests. v2.0 will be completely unit tested, no code can be committed without proper tests.
  • Hello, I'm trying to do a form in the 'admin' part, I based my code on the "posts" example, I want to upload a pdf file. here is my code
    my controller newsrh
    <?php
    class Controller_Admin_Newsrhs extends Controller_Admin
    { public function action_index()
    {
    $data = Model_Newsrh::find('all');
    $this->template->title = "News RH";
    $this->template->content = View::forge('admin/newsrhs/index', $data); } public function action_view($id = null)
    {
    $data = Model_Newsrh::find($id); $this->template->title = "News RH";
    $this->template->content = View::forge('admin/newsrhs/view', $data); } public function action_create($id = null)
    { /**/
    // Custom configuration for this upload
    $config = array(
    'path' => DOCROOT.DS.'files',
    'randomize' => true,
    'ext_whitelist' => array('pdf'),
    ); // 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 contains an array of all error occurred
    // each array element is an an array containing 'error' and 'message'
    }
    /**/ if (Input::method() == 'POST')
    {
    $val = Model_Newsrh::validate('create'); if ($val->run())
    {
    $newsrh = Model_Newsrh::forge(array(
    'titre' => Input::post('titre'),
    'contenu' => Input::file('contenu'),
    'dateupload' => Input::post('dateupload'), )); if ($newsrh and $newsrh->save())
    {
    Session::set_flash('success', 'Added post #'.$post->id.'.'); Response::redirect('admin/newsrhs');
    } else
    {
    Session::set_flash('error', 'Could not save news rh.');
    }
    }
    else
    {
    Session::set_flash('error', $val->show_errors());
    }
    } $this->template->title = "News RH";
    $this->template->content = View::forge('admin/newsrhs/create'); } public function action_edit($id = null)
    {
    $newsrh = Model_Newsrh::find($id);
    $val = Model_Newsrh::validate('edit'); if ($val->run())
    {
    $newsrh->titre = Input::post('titre');
    $newsrh->contenu = Input::post('contenu');
    $newsrh->dateupload = Input::post('dateupload'); if ($newsrh->save())
    {
    Session::set_flash('success', 'Updated news rh #' . $id); Response::redirect('admin/newsrhs');
    } else
    {
    Session::set_flash('error', 'Could not update news rh #' . $id);
    }
    } else
    {
    if (Input::method() == 'POST')
    {
    $newsrh->titre = $val->validated('titre');
    $newsrh->contenu = $val->validated('contenu');
    $newsrh->dateupload = $val->validated('dateupload');
    Session::set_flash('error', $val->show_errors());
    } $this->template->set_global('newsrh', $newsrh, false);//? $post
    } $this->template->title = "News RH";
    $this->template->content = View::forge('admin/newsrhs/edit'); } public function action_delete($id = null)
    {
    if ($newsrh = Model_Newsrh::find($id))
    {
    $newsrh->delete(); Session::set_flash('success', 'Deleted news rh #'.$id);
    } else
    {
    Session::set_flash('error', 'Could not delete news rh #'.$id);
    } Response::redirect('admin/newsrhs'); }
    } and my _form
    <?php echo Form::open(array('class' => 'form-stacked')); ?> <fieldset>
    <div class="clearfix">
    <?php echo Form::label('titre', 'titre'); ?> <div class="input">
    <?php echo Form::input('titre', Input::post('titre', isset($newsrh) ? $newsrh->titre : ''), array('class' => 'span6')); ?> </div>
    </div>
    <div class="clearfix">
    <?php echo Form::label('contenu', 'contenu'); ?> <div class="input">
    <?php echo Form::file(isset($newsrh) ? $newsrh->contenu : '', array('class' => 'span6')); ?>
    </div>
    </div>
    <div class="clearfix">
    <?php echo Form::label('date', 'date'); ?> <div class="input">
    <?php echo Form::input('dateupload', Input::post('dateupload', isset($newsrh) ? $newsrh->dateupload : ''), array('class' => 'span6')); ?> </div>
    </div>
    <div class="actions">
    <?php echo Form::submit('submit', 'Save', array('class' => 'btn primary')); ?> </div>
    </fieldset>
    <?php echo Form::close(); ?> when I want to create a new newsrh I have the following error "Validation rule required failed for content", can someone help me with this error. thank you.
  • here is my function validate
    public static function validate($factory)
    {
    $val = Validation::forge($factory);
    $val->add_field('titre', 'titre', 'required|max_length[255]');
    $val->add('contenu', 'contenu', 'required|max_length[255]');
    //$val->add('contenu', 'contenu', array(''));
    //val->add('contenu', 'contenu')->add_rule('required'); $val->add_field('dateupload', 'dateupload', 'required|date'); return $val;
    }
  • A form field of type 'file' is not an input field, can not be accessed through the Input class, and can not be validated. This is all the task of the Upload class, so once your form validation passes, you need to check if the upload was valid, and if so save it, if not display the upload errors.
  • thank you, sorry to bother you again, I changed my code so my file doesn't need to be validate. Can you tell me if my upload class is in the right place?
    my controller newsrh method create
    public function action_create($id = null)
    { if (Input::method() == 'POST')
    {
    $val = Model_Newsrh::validate('create'); if ($val->run())
    {
    /**/
    // Custom configuration for this upload
    $config = array(
    //'path' => DOCROOT.DS.'files',
    'path' => DOCROOT.'assets'.'files',
    'randomize' => true,
    'ext_whitelist' => array('pdf'),
    ); // 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 contains an array of all error occurred
    // each array element is an an array containing 'error' and 'message'
    }
    /**/ $newsrh = Model_Newsrh::forge(array(
    'titre' => Input::post('titre'),
    'contenu' => Form::file('contenu'),
    'dateupload' => Input::post('dateupload'), )); if ($newsrh and $newsrh->save())
    {
    Session::set_flash('success', 'Added post #'.$post->id.'.'); Response::redirect('admin/newsrhs');
    } else
    {
    Session::set_flash('error', 'Could not save news rh.');
    }
    }
    else
    {
    Session::set_flash('error', $val->show_errors());
    }
    } $this->template->title = "News RH";
    $this->template->content = View::forge('admin/newsrhs/create'); }
    the trouble is this code insert the html code in the database and doesn't upload anything. Can you help me to put everything in the right order. thanks you again
  • Um, Thanks for hijacking my Thread... but anyhow.. The upload processing code, that goes in the controller or model?
    It doesn't really specify in the docs. also I have these two lines of code in my form:
    <?php echo Form::open(array('class' => 'form-stacked')); 
    echo FORM::file&#40;'filename'&#41;; ?>
    
    A.: how do I append the enctype to the Form::open ?
    B: is there anything else I should have with the FORM:file('filename')
    Anything else that should be in ()? perhaps? I have some experience with Rails, and am really liking Fuel's 'oil' tool - cause it is very Rails-like.
  • Something like this, Not tested. //View
    <?php echo Form::open(array('action' => '/upload', 'method' => 'post','enctype' => 'multipart/form-data')); ?>
    <input type="file" name="userfile" id="file" />
     <button type="submit">Upload</button>
    <?php echo Form::close(); ?>
    

    //Controller
       if (\Upload::is_valid()) &#123;
                        \Upload::save();
                        $file = \Upload::get_files(0);
                    } else &#123;
                       //throw error
                    }
    
    Patrick Curl wrote on Friday 16th of March 2012:
    Um, Thanks for hijacking my Thread... but anyhow.. The upload processing code, that goes in the controller or model?
    It doesn't really specify in the docs. also I have these two lines of code in my form:
    <?php echo Form::open(array('class' => 'form-stacked')); 
    echo FORM::file&#40;'filename'&#41;; ?>
    
    A.: how do I append the enctype to the Form::open ?
    B: is there anything else I should have with the FORM:file('filename')
    Anything else that should be in ()? perhaps? I have some experience with Rails, and am really liking Fuel's 'oil' tool - cause it is very Rails-like.
  • to pixel gremlins, I didn't mean to "hijacking" your thread , just thought it is the same topic so didn't want to do another topic. sorry about that
  • marie vacelet wrote on Friday 16th of March 2012:
    to pixel gremlins, I didn't mean to "hijacking" your thread , just thought it is the same topic so didn't want to do another topic. sorry about that

    It's all good, no rough feelings. Mostly it was just your code /posts blocked kinda drowned out my original post, and I'm still trying to get it all sorted. Rails was a bit easier, I may try codeigniter or yii simply because there's a lot more in way of tutorials and support - but I really like the way Fuel looks and feels.
  • I don't really understand what your issue is. The questions you asked where answered, and aren't any easier to solve in other frameworks. CI for example doesn't have anything in regards to form generation and upload processing, you'll have to code it all by hand. If you still have questions, ask them, and someone will try to answer them. The examples in the docs are pretty clear (use the dev-docs, they are more complete), but having said that, Fuel isn't positioned at the absolute beginner. If you have a problem creating a form with a file upload field, then maybe Fuel isn't for you.
  • Thanks,
    Makes sense. Will try it post back if I have more issues.
    A Huzz wrote on Friday 16th of March 2012:
    Something like this, Not tested. //View
    <?php echo Form::open(array('action' => '/upload', 'method' => 'post','enctype' => 'multipart/form-data')); ?>
    <input type="file" name="userfile" id="file" />
     <button type="submit">Upload</button>
    <?php echo Form::close(); ?>
    

    //Controller
       if (\Upload::is_valid()) &#123;
                        \Upload::save();
                        $file = \Upload::get_files(0);
                    } else &#123;
                       //throw error
                    }
    
    Patrick Curl wrote on Friday 16th of March 2012:
    Um, Thanks for hijacking my Thread... but anyhow.. The upload processing code, that goes in the controller or model?
    It doesn't really specify in the docs. also I have these two lines of code in my form:
    <?php echo Form::open(array('class' => 'form-stacked')); 
    echo FORM::file&#40;'filename'&#41;; ?>
    
    A.: how do I append the enctype to the Form::open ?
    B: is there anything else I should have with the FORM:file('filename')
    Anything else that should be in ()? perhaps? I have some experience with Rails, and am really liking Fuel's 'oil' tool - cause it is very Rails-like.
  • Ok, I'm on ubuntu, and this may be an issue with file permissions, though I've chmodded it 777... but I can't seem to get uploads to upload.. Here's the code I'm using: Views:
    <?php Form::open(array('action' => '/upload', 'method' => 'post','enctype' => 'multipart/form-data')); ?>
     <fieldset> <div class="clearfix"> <?php echo Form::label('Filename',
     'filename'); ?>
    
       <div class="input"> 
       <?php 
       echo FORM::file&#40;'filename'&#41;
       
       ?>
       
       <?php /* echo Form::input('filename',
       Input::post('filename', isset($file) ? $file->filename : ''), array('class' =>
       'span6'));  */ ?>
    
       </div> 
       
       </div> <div class="actions"> <?php echo Form::submit('submit',
       'Save', array('class' => 'btn primary')); ?>
    
      </div> </fieldset> <?php echo Form::close(); ?>
    

    Controller:
    public function action_create($id = null) {
     $config = array(
      'path' => DOCROOT.DS.'files',
      'randomize' => true,
      'ext_whitelist' => array('pdf'),
      );
      
      Upload::process($config);
      if (Upload::is_valid()) {
       $arr = Upload::get_files();
       Upload::save(DOCROOT.'assets/files', array_keys($arr) );   
       Model_Files::add(Upload::get_files());
       }  
       foreach (Upload::get_errors() as $file)
       {
        echo $file;
        echo $file['errors'];
        
        }
      $this->template->title = "Files";
      $this->template->content = View::forge('file/create');
      }
    

    Is there something I'm missing?
  • Your form posts to "/upload", yet your action is called "create". Assuming that this action is in a controller called Controller_Upload, shouldn't you post to "/upload/create"?
  • Harro Verton wrote on Tuesday 20th of March 2012:
    Your form posts to "/upload", yet your action is called "create". Assuming that this action is in a controller called Controller_Upload, shouldn't you post to "/upload/create"?

    My controller is files,
    so I would post to files/create ? is that correct?
  • Sounds better then what it was...

Howdy, Stranger!

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

In this Discussion