Cakephp使用Miles J Uploader插件时出错 [英] Cakephp Error using Miles J Uploader plugin

查看:91
本文介绍了Cakephp使用Miles J Uploader插件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正尝试使用位于此处的Miles J插件 http://milesj.me/code/cakephp / uploader 虽然我在学习CakePhp方面取得了很大进展,但我目前使用插件时遇到了问题,我非常感谢您提供帮助。



使用插件的所有必要步骤。它已经下载放在Plugin文件夹,我用CakePlugin :: loadAll()启动。



到目前为止这么好。



接下来,我将继续按照插件开发人员的指示设置表格。



到我自己的代码。我有以下设置:



images_controller.php,image.php及其视图。 $ b

我的目标是在这些文件中使用插件:

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

Class ImagesController扩展AppController {

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

function manage(){
//这里显示一个简单的上传表单,使用动作saveimage
}

function saveimage(){

$ this->上传者=新上传者();
if(!empty($ this-> data)){
$ this-> Upload-> save($ this-> data);
}


}



}

现在,我的模型设置如下

 类Image扩展AppModel {

public $ useTable ='uploads';
public $ actsAs = array(
'Uploader.FileValidation'=> array(
'file'=> array(
'extension'=> array $ b'value'=> array('gif','jpg','jpeg'),
'error'=>'只允许gif,jpg和jpeg图片!'
) ,
'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(
//在转换
数组之后,在数据库中保存其他图像(
'method'=>'resize',
'width'=> 100,
'height'=> 100,
'dbColumn'=>'path_alt'

),
'metaColumns'=>数组(
'size'=>'filesize',//大小值将被保存到文件大小列
'type'=>'type' b $ b)
),
'import'=> array(
'uploadDir'=>'/ files / uploads /',
'name'=>'uploaderFilename',
'dbColumn'=>'path',
'overwrite'=> true,
'stopSave'=> false,
'transforms'=> array(
array(
'method'=> 'scale',
'percent'=> .5,
'dbColumn'=>'path'//覆盖原始图像







}

}

在我的模型测试的目的,我没有改变任何东西,只是复制和粘贴相同的数组,如插件中的Test / Model文件夹所示,这是为了显示插件的功能。



发生以下混乱,错误或缺乏理解:



我的文件没有上传到webroot / files / uploads文件夹
我的数据正在插入数据库,但不是以完整的方式留下空如下所示:

  id |字幕|路径| path_alt |创建| 
4 | | | | 2012:02:.. |

以上,我希望路径被保存,但不是。



我不得不承认我的困惑主要来自于我使用插件的经验,所以我知道我可能对我的模型或我的配置做错了。



任何提示帮助将非常感谢,因为我已经尝试自己工作,没有任何成功。

解决方案

有几件事:



1 - 在您的控制器中,上传者类。您也不需要使用Uploader.Upload模型(它只是一个示例测试用例)。所有你需要做的是在控制器中调用$ this-> Image-> save(),它将上传文件并将路径作为一行保存到数据库(如果你在图像中定义了附件)。



2 - 在您的视图中,创建文件输入。注意输入名称。

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

3 - 在Image模型中,为该特定输入字段设置AttachmentBehavior及其选项: p>

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

确保图片表格中存在path就是这样。



有关每个选项的详细信息,请查看以下内容: http://milesj.me/code/cakephp/uploader#uploading-files-through-the-model


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.

解决方案

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

这篇关于Cakephp使用Miles J Uploader插件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆