在cakephp中上传的图片并没有上传到文件夹中

时间:2014-02-11 10:44:44

标签: php image cakephp cakephp-2.3 image-upload

我正在尝试上传图片,但它正在保存在数据库中,但在将此文件保存在webroot文件夹中时出现问题。有任何可能的错误,请提供我的建议 我的控制器是这样的:

if ($this->request->is('post')) {
            //echo '<pre>';print_r($this->request->data);exit;
            $this->request->data['MediaManagement']['created_date']= date('Y-m-d H:i:s');
            $this->request->data['MediaManagement']['status'] = 'unpublish';//default
            $this->request->data['MediaManagement']['user_id'] = $this->Auth->user('id');
            $random_number = $this->Smart->random_code();

            //echo $random_number;exit;
            $tmp_name = $this->request->data['MediaManagement']['file_name']['tmp_name'];
            $name = $this->request->data['MediaManagement']['file_name']['name'];
            if(empty($this->request->data['MediaManagement']['media_category_id'])){
                $this->request->data['MediaManagement']['media_category_id'] = 0;
            }
            $this->request->data['MediaManagement']['file_type'] = $this->Smart->get_file_extention($this->request->data['MediaManagement']['file_name']['name']);

            if(file_exists(WWW_ROOT.'mediafiles/'.$name))
            {
                $name= $random_number.$this->request->data['MediaManagement']['file_name']['name']; 
            }




            $this->request->data['MediaManagement']['file_name'] = $name;
            //debug($this->request->data);exit;
            $this->MediaManagement->create();
            if ($this->MediaManagement->save($this->request->data)) {
                move_uploaded_file($tmp_name, WWW_ROOT.'mediafiles/'.$name);

                $last_mediamanagement_id = $this->MediaManagement->getLastInsertID();
                $this->Session->setFlash(__('The media  has been saved'));
                $this->redirect(array('action' => 'redirectview/'.$last_mediamanagement_id));
            } else {
                $this->Session->setFlash(__('The media management could not be saved. Please, try again.'));
            }
        }
        $mediaCategories = $this->MediaManagement->MediaCategory->find('list');
        $users = $this->MediaManagement->User->find('list');
        $this->set('currentUserId',$this->Auth->user('id'));
        $this->set(compact('mediaCategories', 'users'));

    }

1 个答案:

答案 0 :(得分:0)

您正在使用$this->request->data['MediaManagement']['file_name'] = $name;

清除文件信息数组

这应该是:$this->request->data['MediaManagement']['file_name']['name'] = $name;

更新(稍微更改了代码):

//in view:
echo $this->Form->input('MediaManagement.file', array(/*your options*/));//instead of MediaManagement.file_name

//in controller:
if($this->request->is('post')) {
    //echo '<pre>';print_r($this->request->data);exit;
    $this->request->data['MediaManagement']['created_date']= date('Y-m-d H:i:s');
    $this->request->data['MediaManagement']['status'] = 'unpublish';//default
    $this->request->data['MediaManagement']['user_id'] = $this->Auth->user('id');

    $path = WWW_ROOT.'mediafiles'.DS;
    if(!is_dir($path)) {
        mkdir($path, 0777);
    }

    $tmp_name = $this->request->data['MediaManagement']['file']['tmp_name'];
    $name = $this->request->data['MediaManagement']['file']['name'];
    if(empty($this->request->data['MediaManagement']['media_category_id'])){
        $this->request->data['MediaManagement']['media_category_id'] = 0;
    }

    //you should get the file_type based on the mime type and not based on the extention, because the extension can be manipulated by the user
    $this->request->data['MediaManagement']['file_type'] = $this->Smart->get_file_extention($this->request->data['MediaManagement']['file']['name']);

    while(file_exists($path.$name)) {
        $name = $this->Smart->random_code().$this->request->data['MediaManagement']['file']['name']; 
    }
    $this->request->data['MediaManagement']['file_name'] = $name;

    $this->MediaManagement->create();
    if ($this->MediaManagement->save($this->request->data)) {
        $last_mediamanagement_id = $this->MediaManagement->getLastInsertID();

        if(move_uploaded_file($tmp_name, $path.$name)) {
            $this->Session->setFlash(__('The media has been saved'));
            $this->redirect(array('action' => 'redirectview/'.$last_mediamanagement_id));
        } else {
            $this->Session->setFlash(__('Could not upload file. Please, try again.'));
            $this->MediaManagement->delete($last_mediamanagement_id);//delete from database
        }
    } else {
        $this->Session->setFlash(__('The media management could not be saved. Please, try again.'));
    }
}
$mediaCategories = $this->MediaManagement->MediaCategory->find('list');
$users = $this->MediaManagement->User->find('list');
$this->set('currentUserId',$this->Auth->user('id'));
$this->set(compact('mediaCategories', 'users'));