Cakephp Error using Miles J Uploader plugin

2019-07-28 21:56发布

问题:

I am currently trying to use Miles J plugin located here http://milesj.me/code/cakephp/uploader Although I have made great progress learning CakePhp, I am currently having a problem using the plugin and I would appreciate any help provided.

I have followed all the necessary steps to use the plugin. It has been downloaded put on Plugin folder, I bootstrapped with CakePlugin::loadAll().

So far so good.

Next I have proceed to set up the table as indicated by the plugin developer.

Ok, now back to my own code. I have the following set up:

images_controller.php , image.php and their views.

My goal now is to use the plugin inside those files as such:

App::import('Vendor', 'Uploader.Uploader');

Class ImagesController extends AppController {

     var $components = array('Auth');
     var $helpers = array('Design');
     var $uses = array('Image', 'Uploader.Upload');

     function manage(){
         //here I show a simple upload form that uses the action saveimage 
     }

     function saveimage(){

          $this->Uploader = new Uploader();
          if(!empty($this->data)){
               $this->Upload->save($this->data);
          }


     }



}

Now, my model is set as follows

Class Image extends AppModel {

       public $useTable = 'uploads';
       public $actsAs = array(
        'Uploader.FileValidation' => array(
            'file' => array(
                'extension' => array(
                    'value' => array('gif', 'jpg', 'jpeg'),
                    'error' => 'Only gif, jpg and jpeg images are allowed!'
                ),
                'minWidth' => 500,
                'minHeight' => 500,
                'required' => true
            ),
            'import' => array(
                'required' => false
            )
        ),
        'Uploader.Attachment' => array(
            'file' => array(
                'name' => 'uploaderFilename',
                'uploadDir' => '/files/uploads/',
                'dbColumn' => 'path',
                'maxNameLength' => 30,
                'overwrite' => true,
                'stopSave' => false,
                'transforms' => array(
                    // Save additional images in the databases after transforming
                    array(
                        'method' => 'resize',
                        'width' => 100,
                        'height' => 100,
                        'dbColumn' => 'path_alt'
                    )
                ),
                'metaColumns' => array(
                    'size' => 'filesize',   // The size value will be saved to the filesize column
                    'type' => 'type'        // And the same for the mimetype
                )
            ),
            'import' => array(
                'uploadDir' => '/files/uploads/',
                'name' => 'uploaderFilename',
                'dbColumn' => 'path',
                'overwrite' => true,
                'stopSave' => false,
                'transforms' => array(
                    array(
                        'method' => 'scale',
                        'percent' => .5,
                        'dbColumn' => 'path' // Overwrite the original image
                    )
                )
            )
        )
    );


}

}

On my model for testing purposes I have not changed anything but just copied and pasted the very same array as shown in the the Test/Model folder inside the plugin, which is meant to show the functionality of the plugin.

The following confusion, errors or lack of understanding is taking place:

My file is not being uploaded to the webroot/files/uploads folder My data is being inserted in the database, but not in a complete manner by leaving empty as shown:

id | caption | path | path_alt | created  |
4  |         |      |          |2012:02:..|

Above, I expect the path to be saved, but it doesn't.

I have to admit my confusion comes mainly from my inexperience using plugins, so I am aware I might be doing something wrong regarding my models or my configuration.

Any prompt help would be appreciated greatly as I have tried to work this out on my own without any success.

回答1:

A few things:

1 - In your controller you do not need to import the Uploader class. You also don't need to use the Uploader.Upload model (it's merely an example test case). All you need to do in the controller is call $this->Image->save() which will upload the file and save the path as a row into the database (if you defined the Attachment in Image).

2 - In your view, create the file input. Pay attention to the input name.

echo $this->Form->input('FILE_INPUT_NAME', array('type' => 'file'));

3 - In your Image model, setup the AttachmentBehavior and its options for that specific input field:

'Uploader.Attachment' => array(
    'FILE_INPUT_NAME' => array(
        'uploadDir' => '/files/uploads/',
        'dbColumn' => 'path'
    ),
    'ANOTHER_INPUT' => array()
);

Be sure that the column "path" exists in your images table. And thats it.

For more information on what each option does, check out the following: http://milesj.me/code/cakephp/uploader#uploading-files-through-the-model