Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8143577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:09:19+00:00 2026-06-06T13:09:19+00:00

I’m trying to working with the Uploader Plugin to create a structure where a

  • 0

I’m trying to working with the Uploader Plugin to create a structure where a User Model can upload it’s avatar with the Avatar Model, I’ve read the instructions several times but when I try to $this->Uploader->upload('Avatar.filename') I get no validation errors but the upload method fails.

Here is how I’ve written the User Model

<?php
class User extends AppModel {
    public $name = 'User';
    public $hasOne = array(
        'Profile' => array(
            'className' => 'Profile',
            'conditions' => '',
            'dependent' => true,
            'foreignKey' => 'user_id',
            'associatedKey' => 'user_id'
        ),
        'Avatar' => array (
            'className' => 'Avatar',
            'foreignKey' => 'user_id',
            'dependent' => true
        )
    );
    public $validate = array(...);
    // other stuff not relevant here...
?>

Here is the Avatar Model

<?php
class Avatar extends AppModel {
public $name = 'Avatar';
public $actsAs = array (
    'Uploader.Attachment' => array (
        'Avatar.filename' => array(
            'name'      => 'setNameAsImgId',    // Name of the function to use to format filenames
            'baseDir'   => '',                  // See UploaderComponent::$baseDir
            'uploadDir' => 'files/avatars/',    // See UploaderComponent::$uploadDir
            'dbColumn'  => 'filename',          // The database column name to save the path to
            'importFrom'    => '',              // Path or URL to import file
            'defaultPath'   => '',              // Default file path if no upload present
            'maxNameLength' => 500,             // Max file name length
            'overwrite' => true,                // Overwrite file with same name if it exists
            'stopSave'  => true,                // Stop the model save() if upload fails
            'allowEmpty'    => true,            // Allow an empty file upload to continue
            'transforms'    => array (
                array('method' => 'resize', 'width' => 128, 'height' => 128, 'dbColumn' => 'name')
            )       // What transformations to do on images: scale, resize, ete
        )
    ),
    'Uploader.FileValidation' => array (
        'Avatar.filename' => array (
            'maxWidth'  => array (
                'value' => 512,
                'error' => 'maxWidth error'
            ),
            'maxHeight' => array (
                'value' => 512,
                'error' => 'maxWidth error'
            ),
            'extension' => array (
                'value' =>  array('gif', 'jpg', 'png', 'jpeg'),
                'error' => 'extension error'
            ),
            'filesize'  => array (
                'value' => 5242880,
                'error' => 'filesize error'
            )
        )
    )
);

public $belongsTo = array(
    'User' => array(
        'className'     => 'User',
        'foreignKey'    => 'user_id',
        'conditions'    => '',
        'order'         => ''
    )
);

public function setNameAsImgId ($name, $field, $file) {
    /**
    * Format the filename a specific way before uploading and attaching.
    * 
    * @access public
    * @param string $name   - The current filename without extension
    * @param string $field  - The form field name
    * @param array $file    - The $_FILES data
    * @return string
    */
    // devo ricavare l'id dell'immagine appena creata per rinominare il file
    return $name;
}
}
?>

This is the UsersController for edit method

<?php
App::uses('CakeEmail','Network/Email');
CakePlugin::load('Uploader');
App::import('Vendor', 'Uploader.Uploader');

class UsersController extends AppController {
public $name = 'Users';

public function edit ($id) {
    $this->User->id = $id;
    if (!$this->User->exists()) {
        throw new NotFoundException ('Nessuna corrispondenza trovata per questo utente');
    }
    if (!$id) {
        $this->set('flash_element','error');
        $this->Session->setFlash ('Utente non valido');
    }
    $this->User->recursive = 1;
    $this->set('user', $this->User->read());

    if ($this->request->is('post')) {
        $this->User->id = $this->request->data['User']['id'];
        if (!$this->User->exists()) {
            $this->set('flash_element','warning');
            $this->Session->setFlash('Nessun utente trovato con questa corrispondenza');
    }
    if ($this->User->save($this->request->data)) {
        $this->request->data['Profile']['user_id'] = $this->User->id;

        $conditions = array(
            'conditions' => array(
                'Profile.id' => $this->request->data['Profile']['id']
            )
        );

        if ($this->User->Profile->save($this->request->data, $conditions)) {

            if (!empty($this->request->data['Avatar']['filename'])) {

                $this->request->data['Avatar']['user_id'] = $this->User->id;
                if ($this->User->Avatar->save($this->request->data)) {

                    $avatar = $this->User->Avatar->find('first', array(
                        'conditions' => array('Avatar.user_id' => $this->User->id)
                    ));
                    $ext = Uploader::ext($this->request->data['Avatar']['filename']);
                    $filename = $avatar['Avatar']['id'].'.'.$ext;


                if ($this->User->Avatar->save('Avatar.filename')) {
                    $this->set('flash_element','done');
                    $this->Session->setFlash('Avatar changed successfully');
                    debug('saved successfully');
                } else {
                    debug('not saved');
                    $this->set('flash_element','warning');
                    $this->Session->setFlash('Avatar not saved on the server');
                }

                } else {
                        $this->Session->write('flash_element','error');
                        $this->Session->setFlash('Avatar data not saved on the server');
                        $this->redirect(array('action'=>'index'));
                    }
                } else {
                    $this->Session->write('flash_element','done');
                    $this->Session->setFlash('Data successfully saved, avatar not changed');
                    $this->redirect(array('action'=>'index'));
                }
        } else {
                $this->set('flash_element','error');
                $this->Session->setFlash('Error on saving Profile data to the server');
            }
        } else {
            $this->Session->write('flash_element','error');
            $this->Session->setFlash('Error on saving User data to the server');
            $this->redirect(array('action'=>'index'));
        }
    }
}
}
?>

And in the view file I have this

<?php
echo $this->Form->create('User', array ('class' => 'form'));
echo $this->Form->input('User.id', array ('type'=>'hidden', 'value'=> $user['User']['id'],'label'=> false, 'id' => 'id'));
echo $this->Form->input('User.username', array ('label'=> false, 'value' => $user['User']['username'], 'id' => 'username', 'after' => '<div class="message">Message for username field'));
echo $this->Form->input('User.email', array ('label'=> false, 'value' => $user['User']['email'], 'id' => 'email', 'after' => '<div class="message">Message for email field</div>'));
echo $this->Form->input('UserOptions.id', array ('type'=>'hidden', 'value'=> $user['UserOptions']['id'],'label'=> false, 'id' => 'UserOptions.id'));
$attributes = array ('value' => $user['UserOptions']['avatar_type'], 'empty' => false);
$options = array('0' => 'This site', '1' => 'Gravatar');
echo $this->Form->select('UserOptions.avatar_type', $options, $attributes);
/* avatar code */
echo $this->Form->input('Avatar.id', array ('type'=>'hidden', 'value'=> $user['Avatar']['id'],'label'=> false, 'id' => 'Avatar.id'));
echo $this->Form->input('Avatar.filename', array('type' => 'file'));
/* end avatar code */
echo $this->Form->input('Profile.city', array ('label'=> false, 'value' => defaultValue ('City', $user['Profile']['city']), 'id' => 'city', 'after' => '<div class="message">Message for city field</div>'));
echo $this->Form->input('Profile.country', array ('label'=> false, 'value' => defaultValue('',$user['Profile']['country']), 'id' => 'country', 'after' => '<div class="message">Message for country field</div>'));
echo $this->Form->input('Profile.url', array ('label'=> false, 'value' => defaultValue('http://', $user['Profile']['url']), 'id' => 'url', 'after' => '<div class="message">Message for url field</div>'));
echo $this->Form->input('Profile.description', array ('label'=> false, 'value' => defaultValue('Description',$user['Profile']['description']), 'id' => 'description', 'after' => '<div class="message">Message for description field</div>'));
echo $this->Form->submit('Modifica', array('id'=>'edit'));
echo $this->Form->end();
?>

In the Controller, every part of the data is saved until I reach $this->Uploader->upload('Avatar.filename', array('overwrite' => true, 'name' => $filename)) where I get a generic error.

This Plugin seems to be the best way to do it without write tons of code, but I’m not sure how to use it.
I’m not sure what’s wrong with the code, can You help me to solve the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:09:21+00:00Added an answer on June 6, 2026 at 1:09 pm

    I’ve found the problems, I forgot two things:

    In the view page I’ve forgot 'type' => 'file'

    <?php
    echo $this->Form->create('User', array ('class' => 'form', 'type' => 'file'));
    ?>
    

    I was used to work with 1.3 version so the app/Config/bootstrap.php configuration was missing:

    <?php
    CakePlugin::load('Uploader');
    ?>
    

    Now it works!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.