Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Upload an image
  • Hi everybody!

    I've a little problem to upload an image with FuelPHP.

    So there is a kind of blog. We cant add posts and I want an image (featured) for every post.

    I follow the DOC, i have NO error, but the upload take always the default image defined in the model and not the image of the upload. And if I don't put a default value, I have a database error!


    Here my controller:


    And here my Model:


    Thanks for your help!


    PS: The field name in my database is "image" to store the name and extension of the image!
  • In your controller, you use $post on line 29, but that is defined nowhere? And on line 41, you create a new $post, overwriting whatever it was before, and losing your image information.

    The image name should be part of the forge too.
  • So if I do like above, it should be work? I don't overwrite the $post and I pass the image name in the forge.

    $post = Model_Post::forge(array(
    'title' => Input::post('title'),
    'slug' => Input::post('slug'),
    					'image' => $value[0]['saved_as'],
    					'summary' => Input::post('summary'),
    'body' => Input::post('body'),
    'user_id' => Input::post('user_id'),
    ));
  • That should work fine.
  • Unfortunately not... And I test to put a mp3 file in my upload and it pass the validation!!! O_o

    So I think that the upload doesn't work and it puts always the default image!

    But I have put the extensions accepted in config: 'img', 'jpg', 'jpeg', 'gif', 'png'

    $config = array(
            'path' => DOCROOT.DS.'assets/images',
            'randomize' => true,
            'ext_whitelist' => array('img', 'jpg', 'jpeg', 'gif', 'png'),
        );

        Upload::process($config);

        // if a valid file is passed than the fucntion will save, or if its not empty
        if (Upload::is_valid())
        {
           
            // save them according to the config
            Upload::save();

            //if you want to save to tha database lets grab the file name
            $value = Upload::get_files();
           

            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'
        echo $file['errors'];
    }


    $post = Model_Post::forge(array(
    'title' => Input::post('title'),
    'slug' => Input::post('slug'),
    'image' => $value[0]['saved_as'],
    'summary' => Input::post('summary'),
    'body' => Input::post('body'),
    'user_id' => Input::post('user_id'),
    ));

    I don't understand... There is nothing in log... How can I debug to see what it doesn't work?

    Thanks a lot
  • dump Upload::get_files(); and Upload::get_errors();, after save().

    The first one will contain all validated and saved uploaded files, the second one the files that errorred.

    Since you call process() with a custom config, did you disable auto processing in your app/config/upload.php? If not, the files will be processed by the default config and not yours.
  • It works. Apologies, I'm really tired and I put hours to see that I changed the upload admin interface rather than the user one!

    I really appreciated the speed of your responses!


  • :-)

    Glad to hear you've got it sorted.

Howdy, Stranger!

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

In this Discussion