【问题标题】:Can't upload image using Miles Johnson Uploader无法使用 Miles Johnson Uploader 上传图片
【发布时间】:2013-10-28 12:24:33
【问题描述】:

我正在开发一个应用程序,其中我创建了一个具有以下属性的products 表。

{id, image, warranty-image, created, modified}

我正在使用miles johnson 4.3.1 uploader 上传图片。所以我在Product model中写了$actsAs如下。

public $actsAs = array(
    'Uploader.Attachment' => array(
        'image' => array(
            'overwrite' => true,
            'uploadDir' => 'img/products',
            'finalPath' => '',
            'dbColumn' => 'image',
            'transforms' => array(
                'imageLarge' => array(
                    'nameCallback' => 'transformNameCallback',
                    'method' => 'resize',
                    'prepend' => 'large_',
                    'width' => 750,
                    'height' => 100,
                    'aspect' => false
                )
        ),
        'warranty_image' => array(
            'overwrite' => true,
            'uploadDir' => 'img/products',
            'finalPath' => '',
            'dbColumn' => 'warranty_image',
            'transforms' => array(
                'warranty_imageSmall' => array(
                    'nameCallback' => 'transformNameCallback',
                    'method' => 'resize',
                    'prepend' => 'small_',
                    'width' => 150,
                    'height' => 96,
                    'aspect' => false
                )
            )
        )
    ),
    'Uploader.FileValidation' => array(
        'image' => array(
            'required' => true,
            'extension' => array('gif', 'jpg', 'png', 'jpeg'),
            'type' => array('image'),
            'minWidth' => 100,
            'minHeight' => 100,
            'filesize' => 5242880
        ),
        'warranty_image' => array(
            'required' => true,
            'extension' => array('gif', 'jpg', 'png', 'jpeg'),
            'type' => 'image',
            'minWidth' => 100,
            'minHeight' => 96,
            'filesize' => 5242880
        )
    )
);

public function transformNameCallback($name, $file) {
    return $this->getUploadedFile()->name();
}

add view我写file inputs如下。

echo $this->Form->create('Product', array('enctype' => 'multipart/form-data')); 
echo $this->Form->file('image');
echo $this->Form->file('warranty_image');
echo $this->Form->end();

这个上传者只上传了image 的图片,而不是warranty_image 的图片。请帮助我获得解决方案。这项工作将受到更多赞赏。

【问题讨论】:

  • 那么真的是上传只为image选择的文件,还是你的意思是它只是存储只为image选择的文件image,即实际上上传两个文件(浏览器发送两个文件的数据),但为warrant_image 选择的文件没有存储在数据库和/或文件系统中?
  • @ndm 它只是上传'image'文件并复制到'table',但它没有发生在'warranty_image'中。
  • 如果真的是这样,那么生成的 HTML 表单代码和发送的请求(检查控制器中的 debug($this->request->data))究竟是什么样的?

标签: cakephp plugins cakephp-2.3 uploader


【解决方案1】:

我会推荐https://github.com/josegonzalez/cakephp-upload

这与您的外观非常相似,并且确实可以正常工作。

【讨论】:

    【解决方案2】:

    在视图中使用 HTML input 标签或 CakePHP Form Helper Input 标签。

    echo $this->Form->input('image', array('type' => 'file', 'required' => false, 'label' => 'Select the File to Upload'));
    echo $this->Form->input('warranty_image', array('type' => 'file', 'required' => false, 'label' => 'Select the File to Upload'));
    

    【讨论】:

      【解决方案3】:

      您的数据库表使用“warranty-image”作为字段。

      但是您的 PHP 代码正在使用“warranty_image”。

      注意“-”而不是“_”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-15
        • 1970-01-01
        • 1970-01-01
        • 2012-10-30
        • 2018-12-14
        • 2013-12-27
        • 2012-12-31
        • 2018-10-23
        相关资源
        最近更新 更多