使用 cakephp2.0 文件上传插件保存标题/描述时出现问题

发布于 2025-01-08 15:54:00 字数 2086 浏览 5 评论 0原文

我正在使用 http://milesj.me/code/cakephp/uploader#configuration 来上传图片。我让它工作正常(在上传图像方面),但我无法让它在我的数据库中保存标题/描述。

所以我有 Image.php 模型,它具有以下代码

<?php
class Image extends AppModel {

    var $name = 'Image';

    public $actsAs = array('Uploader.Attachment', 'Uploader.FileValidation');
    public $validate = array(
                            'title' => array( 'rule' => 'notEmpty')
                            );  

}

在我看来,我有一堆字段,例如

echo $this->Form->input('title');

My ImagesController.php 添加函数,如下所示

 function add($number_of_images = 1){ 

        if (!empty($this->data)) {
            var_export($this->data);
            exit();
            $count = 1;
            foreach($this->data['Images'] as $entry){
                $file_name = "file" . $count;
                if ($data_s = $this->Uploader->upload($file_name)) {
                    $this->Image->saveAll($data_s);
                }
                $count++;
            }

            $this->Session->setFlash("Your image(s) has been saved");
            $this->redirect(array('action'=>'index'));

        }else{   
            // make sure 10 is max amount of images a user can upload
            if($number_of_images <= 10 ){
                $this->set('number_of_images', $number_of_images);
            }else{
                // set to default 1
                $this->set('number_of_images', '1');
            }
        }
    }

当我单击“保存”时,图像将被上传,但标题/描述不会被上传或验证。这就是我的 var_export($this->data) 的样子

array ( 'selectImages' => '1', 'Images' => array ( 'title' => 'adsafdas', 'description' => 'asdfasd', 'tags' => '', 'file1' => array ( 'name' => '308462_926071922398_11704522_41424436_637322498_n.jpg', 'type' => 'image/jpeg', 'tmp_name' => '/tmp/php7tycbu', 'error' => 0, 'size' => 81638, ), ), )

我该如何解决这个问题?

I am using http://milesj.me/code/cakephp/uploader#configuration to upload images. I got it to work fine (in terms of uploading images) but I can't get it to save title/description in my db.

so I have Image.php model that has the following code

<?php
class Image extends AppModel {

    var $name = 'Image';

    public $actsAs = array('Uploader.Attachment', 'Uploader.FileValidation');
    public $validate = array(
                            'title' => array( 'rule' => 'notEmpty')
                            );  

}

In my view I have bunch of fields such as

echo $this->Form->input('title');

My ImagesController.php add function looks like this

 function add($number_of_images = 1){ 

        if (!empty($this->data)) {
            var_export($this->data);
            exit();
            $count = 1;
            foreach($this->data['Images'] as $entry){
                $file_name = "file" . $count;
                if ($data_s = $this->Uploader->upload($file_name)) {
                    $this->Image->saveAll($data_s);
                }
                $count++;
            }

            $this->Session->setFlash("Your image(s) has been saved");
            $this->redirect(array('action'=>'index'));

        }else{   
            // make sure 10 is max amount of images a user can upload
            if($number_of_images <= 10 ){
                $this->set('number_of_images', $number_of_images);
            }else{
                // set to default 1
                $this->set('number_of_images', '1');
            }
        }
    }

When I click save, the image gets uploaded but title/description doesnt get uploaded or validated. This is how my var_export($this->data) looks like

array ( 'selectImages' => '1', 'Images' => array ( 'title' => 'adsafdas', 'description' => 'asdfasd', 'tags' => '', 'file1' => array ( 'name' => '308462_926071922398_11704522_41424436_637322498_n.jpg', 'type' => 'image/jpeg', 'tmp_name' => '/tmp/php7tycbu', 'error' => 0, 'size' => 81638, ), ), )

How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

五里雾 2025-01-15 15:54:00

根据链接,$this->Uploader->upload()仅返回上传文件的数据。因此,您需要在 saveAll 之前将此数组与表单 $this->data 的其他字段合并。

但是,如果您需要在上传文件之前验证表单数据,请使用 $this->Image->validates($this->request->data)

According with the link, $this->Uploader->upload() returns only data of file uploaded. So, you need merge this array with the other fields of your form $this->data before saveAll.

However, if you need validate form data before upload the file, use $this->Image->validates($this->request->data).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文