Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Image Uploading with a Form
  • When I try to make a simple form to upload images, I get this error when I try to access the form: error.png Here is my controller code:
    <?php
    
    class Controller_Image extends Controller {
      
      public function action_index() {
        $config = array(
        'path' => DOCROOT.DS.'files',
        'randomize' => true,
        'ext_whitelist' => array('img', 'jpg', 'jpeg', 'gif', 'png'),
        );
    
        Upload::process($config);
        if(Upload::is_valid()) {
          Upload::save();
        }
        
        return Response::forge(View::forge('image/index'));
      
      }
    
      public function action_404() {
        return Response::forge(ViewModel::forge('welcome/404'), 404);
      }
    
    }
    

    Here is my view code:
    <?php
    
      echo Form::open(array('enctype' => 'multipart/form-data'));
      echo Form::file&#40;'filename'&#41;;
      echo Form::submit('submit');
      echo Form::close(); 
    
    ?>
    

    Help? Thanks!
  • You get that error if you don't upload anything and using 1.2/master or 1.2RC1. It has already been fixed in 1.2/develop, so either wait for the 1.2 release (next couple of days), switch to a develop branch (1.3/develop would then be the option) or get the source of the upload class from the 1.2/develop repository on github to patch your current installation.
  • Perfect thank you! Only now I'm getting this error when I try to upload something. error2.png What would cause that?
  • Didn't realize the image links were broken. Anyway, I'm getting the following error: http://postimage.org/image/ldjvf1i67/
  • Then the fileinfo PHP extension isn't loaded.
  • Okay awesome! So now it gets through the uploading process but the files don't actually upload. What could be causing this? Thank you so much guys Fuel is a great framework just way different than anything else I've ever used! Stoked!
  • Aren't they uploaded, does the validation fail, or does the save fail? The correct way to work is to process, to check for errors, to save, to check for errors again. So your first port of call would be to dump the errors.
  • Awesome. I will start working on that and post back with updates.
  • Okay awesome I got it working. The problem was that my path wasn't writable so the files couldn't be stored there. Here is the controller and view for anyone that runs into this problem. View:
    <html>
    <head>
    <title>Upload test</title>
    <body>
    <?php
      foreach($errors as $error) {
        foreach($error as $message) {
          echo $message['message'];
        }
      }
      echo Form::open(array('enctype' => 'multipart/form-data'));
      echo Form::file&#40;'filename'&#41;;
      echo Form::submit('submit');
      echo Form::close();
    
    ?>
    </body>
    </html>
    

    Controller:
    <?php
    
    class Controller_Image extends Controller {
    
      public function action_index() {
        $config = array(
        'path' => '/var/www/pixelforger.com/web/thinkmeme/public/uploads',
        'randomize' => true,
        'ext_whitelist' => array('img', 'jpg', 'jpeg', 'gif', 'png'),
        );
    
        Upload::process($config);
        if(Upload::is_valid()) {
          Upload::save();
        }
        $data = array();
        $data['errors'] = '';
        $ii = 0;
        foreach(Upload::get_errors() as $file) {
          $data['errors'][$ii] = $file['errors'];
          ++$ii;
        }
    
        return Response::forge(View::forge('image/index', $data));
    
      }
    
      public function action_404() {
        return Response::forge(ViewModel::forge('welcome/404'), 404);
      }
    
    }
    
    ?>
    
  • Cool. Good to hear you've got it sorted!
  • I get this error please: ErrorException [ Error ]: Call to undefined function Fuel\Core\finfo_open()
  • According to the PHP documentation, the finfo extension should be present by default in every PHP 5.3 installation. On Windows, you have to install an extra DLL, which is documented (so see the docs). Unfortunately, some hosters compile their own PHP binary, and instead of reviewing this process when switching to 5.3, they just used the old 5.2 scripts, leading to a crippled PHP binary. If you're with a hoster, complain to them. If that doesn't work, switch hosters.

Howdy, Stranger!

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

In this Discussion