1

我第一次在 cakephp 工作。我能够开始使用具有图像变量的产品模型。我可以上传图片,但之后我无法保存到产品中。这是我的 add.ctp

<div class="products form">
<?php echo $this->Form->create('Product', array('enctype' => 'multipart/form-data')); ?>
    <fieldset>
        <legend><?php echo __('Add Product'); ?></legend>
    <?php
        echo $this->Form->input('name');
        echo $this->Form->input('slug');
        echo $this->Form->input('description');
        echo $this->Form->input('price');
        echo $this->Form->input('weight');

            //upload image    
            echo $this->Form->input('Product.image', array('type'=>'file', 'tmp_name'=>'temp'));



    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
    <h3><?php echo __('Actions'); ?></h3>
    <ul>

        <li><?php echo $this->Html->link(__('List Products'), array('action' => 'index')); ?></li>
    </ul>
</div>

这是我的 ProductsController add() 功能

 public function add($seller_id = null) {
        if ($this->request->is('post')) {
            $this->Product->create();
            debug($this->request->data);
                            if(isset($this->request->data["Product"]["image"]["name"]))
                            {
                                $file = new File($this->request->data["Product"]["image"]["name"]);
                                debug($this->request->data);
                                $ext = pathinfo(($this->request->data["Product"]["image"]["name"]), PATHINFO_EXTENSION);

                                if ($ext != 'jpg' && $ext != 'jpeg' && $ext != 'gif' && $ext != 'png') 
                                {
                                    $this->Session->setFlash('You may only upload image files.');
                                }else
                                {
                                    if(move_uploaded_file($this->request->data["Product"]["image"]["tmp_name"],WWW_ROOT."/img/uploads/" . $this->data["Product"]["image"]["name"]) == true)
                                    {
                                        $this->data["Product"]["image"] =  $this->data["Image"]["image"]["name"];
                                    }
                                }
                                if ($this->Product->save($this->request->data)) //error here
                                {
                                    $this->Session->setFlash(__('The product has been saved'));
                                    $this->redirect(array('action' => 'index'));
                                    $this->request->data['Product']['seller_id'] = $seller_id;
                                }
                                else {
                                    $this->Session->setFlash(__('The product could not be saved. Please, try again.'));
            }

                        }$this->Session->setFlash(__('The image could not be saved. Please, try again.'));
        }

    }

谁能看到为什么它会上传和移动图像但不保存产品?此外,这里是 debug($this->request->data);

array(
    'Product' => array(
        'name' => '8',
        'slug' => '8',
        'description' => '8',
        'price' => '8',
        'weight' => '8',
        'image' => array(
            'name' => 'celeb16.com-purple-strapless-short-bridesmaid-dress-g129-33(1).jpg',
            'type' => 'image/jpeg',
            'tmp_name' => 'C:\xampp\tmp\phpDD08.tmp',
            'error' => (int) 0,
            'size' => (int) 1404
        )
    )
)

另一条可能有用的信息是,当我使用此代码时:

<?php echo $this->Form->create('Product', array('enctype' => 'multipart/form-data')); ?>

它将允许图像正确上传,但不会保存产品,当我使用此代码时:

 <?php echo $this->Form->create('Product'); ?>

它将保存产品但不上传图片

4

2 回答 2

1

您需要在移动图像之前保存数据,试试这个..

 if ($this->Product->save($this->request->data)) //error here
                            {
                                $this->Session->setFlash(__('The product has been saved'));
                                $this->redirect(array('action' => 'index'));
                                $this->request->data['Product']['seller_id'] = $seller_id;
                            }
  if(move_uploaded_file($this->request->data["Product"]["image"]["tmp_name"],WWW_ROOT."/img/uploads/" . $this->data["Product"]["image"]["name"]) == true)
                                {
                                    $this->data["Product"]["image"] =  $this->data["Image"]["image"]["name"];
                                }
                            }

希望这对你有用

这是我之前如何实现文件上传的示例,请尝试将其用作指南,我建议保持简单,如果您想限制文件类型,我建议在 MODEL 中执行此操作。

public function add() {

 if($this->request->is('post')){

 $file = $this->request->data['Upload']['file'];
 if($this->Upload->save($this->data) && move_uploaded_file($file['tmp_name'],APP.'webroot/files/uploads'.DS.$this->Upload->id.'.mp4')) 
 {
 $this->Session->setFlash('<p class="uploadflash">The upload has been saved</p>', true);
 $this->redirect(array('controller'=>'Uploads', 'action' => 'watch', $this->Upload->id));
 }  else {
 $this->Session->setFlash('<p class="loginerror">The upload could not be saved, mp4 files can be saved only.</p>', true);   
 }
 }
 }
于 2012-08-08T20:28:26.593 回答
0

尝试更改此行:

$this->data["Product"]["image"] =  $this->data["Image"]["image"]["name"];

对此:

$this->request->data["Product"]["image"] =  $this->request->data["Image"]["image"]["name"];

在您尝试操作的原始代码中$this->data,在您的保存调用中,您正在引用$this->request->data

$this->Product->save($this->request->data)

希望这应该可以解决问题,如果没有错误本身就很难说:)

于 2012-08-08T21:57:25.263 回答