由jose gonzalez插件上传的CakePHP图片

时间:2014-05-06 11:35:49

标签: php oop cakephp

我正在尝试使用https://github.com/josegonzalez/cakephp-upload这个插件在cakephp上映像上传。我几乎完成了上传,现在问题是图像目录没有正确发送。这是图像 enter image description here

在控制器中我添加了此代码

 public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            $data = $this->request->data['User'];
                    if(!$data['photo']['name'])
                            unset($data['photo']);  
                if ($this->User->save($data))  {
                $this->Session->setFlash(__('The img has been saved.'));

            } else {

                $this->Session->setFlash(__("The img hasn't been saved."));
            }
    }
    }

在add.ctp代码中是

<?php echo $this->Form->create('User', array('type'=>'file')); ?>
    <fieldset>
        <legend><?php echo __('Add Img'); ?></legend>
    <?php
        echo $this->Form->input('User.username'); 
        echo $this->Form->input('photo',array('type'=>'file'));
        echo $this->Form->input('User.photo_dir', array('type' => 'hidden')); 
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>

这是user.php模型代码

class User extends AppModel {
     public $actsAs = array(
        'Upload.Upload' => array(
            'photo' => array(
                'fields' => array(
                    'dir' => 'photo_dir'
                )
            )
        )
    );
}

photo_dir字段类型为varchar(255)

照片正在上传webroot \ img \ upload文件夹 如何在数据库表中发送完整的URL?请问有谁可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您的控制器代码可能如下所示:

public function add() {
    if ($this->request->is('post')) {
        $this->User->create();
        $data = $this->request->data['User'];
                if(!$data['photo']['name'])
                        unset($data['photo']);
            $data['photo']['name']='/img/upload/'.$data['photo']['name']  
            if ($this->User->save($data))  {
            $this->Session->setFlash(__('The img has been saved.'));
        } else {
            $this->Session->setFlash(__("The img hasn't been saved."));
        }
    }
}

然而,这实际上并没有进行任何上传,也没有严格遵守蛋糕的惯例。

这是一个不使用插件的正确上传(剪辑)。注意:这是跟踪团队的网站的实际代码。

控制器

public function add() {
    if ($this->request->is('post')) {
        $this->Player->create();
        if(isset($this->request->data['Player']['upload'])) {
            foreach($this->request->data['Player']['upload'] as $key => $upload) {
                $file=$upload;
                $path=APP.'webroot/img/Players/'.$file['name'];

                while(file_exists($path)) {
                    $r=rand(1,10000);
                    $file['name']=$r.$file['name'];
                    $path=APP.'webroot/img/Players/'.$file['name'];
                }
                if ($file['error'] == 0) {
                    if (move_uploaded_file($file['tmp_name'], $path)) {
                        $this->request->data['Player'][$key]='Players/'.$file['name'];;
                    } else {
                        $this->Session->setFlash(__('Something went wrong. Please try again.'));
                        $this->redirect(array('action' => 'index'));
                    }
                }
            }
        }
        if ($this->Player->save($this->request->data)) {
            $this->Session->setFlash(__('The player has been saved.'), 'default', array('class' => 'alert alert-success'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The player could not be saved. Please, try again.'), 'default', array('class' => 'alert alert-danger'));
        }
    }
    $teams = $this->Player->Team->find('list');
    $ranks = $this->Player->Rank->find('list');
    $this->set(compact('teams', 'ranks'));
}

视图

<?php echo $this->Form->create('Player', array('role' => 'form', 'type' => 'file')); ?>
    <?php echo $this->Form->input('team_id', array('class' => 'form-control', 'placeholder' => 'Team Id'));?>               
    <?php echo $this->Form->input('rank_id', array('class' => 'form-control', 'placeholder' => 'Rank Id'));?>
    <?php echo $this->Form->input('name', array('class' => 'form-control', 'placeholder' => 'Name'));?>
    <?php echo $this->Form->input('upload', array('class' => 'form-control', 'placeholder' => 'Name'));?>