josegonzalez pulgin上传CakePHP图像

josegonzalez pulgin上传CakePHP图像,php,oop,cakephp,Php,Oop,Cakephp,我正在尝试使用这个插件在cakephp中上传图像。我几乎完成了上传,现在的问题是图像目录发送不正确。这是图片 在控制器中,我添加了此代码 public function add() { if ($this->request->is('post')) { $this->User->create(); $data = $this->request->data['User'];

我正在尝试使用这个插件在cakephp中上传图像。我几乎完成了上传,现在的问题是图像目录发送不正确。这是图片

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

 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')); ?>
photo_dir字段类型为varchar(255)

照片正在上载webroot\img\upload文件夹
如何在数据库表中发送完整的url?任何人都可以帮助我吗?

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

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."));
        }
    }
}
然而,这实际上并没有进行任何上传,也没有严格遵循cake的惯例

这是一个正确的上传(剪断它),没有使用插件。注意:这是来自跟踪团队的站点的实际代码

控制器

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'));
}
看法



您如何使用行为?你把'dir'=>'photo_dir'放进去了吗?读取'photo'=>array('fields'=>array('dir'=>'photo_dir'))目录保存,但不保存完整目录+图像名称,仅保存id文件夹。我对此也有问题。尖沙咀
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'));?>