【问题标题】:CakePHP image upload by josegonzalez pulgin由 jose gonzalez 插件上传 CakePHP 图片
【发布时间】:2014-05-06 11:35:49
【问题描述】:

我正在尝试使用 https://github.com/josegonzalez/cakephp-upload 这个插件在 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')); ?>

这里是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?请任何人帮助我?

【问题讨论】:

  • 你如何使用行为?你放了'dir' =>'photo_dir'吗?阅读github.com/josegonzalez/cakephp-upload#usage
  • 'photo' => array( 'fields' => array( 'dir' => 'photo_dir' ) ) 保存目录但不完整目录+图片名,只保存id文件夹。
  • 我也有这个问题。啧

标签: php oop cakephp


【解决方案1】:

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

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 的约定。

这是不使用插件的正确上传 (snip-it)。注意:这是来自跟踪团队的网站的实际代码。

控制器

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'));?>

【讨论】:

    猜你喜欢
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多