如何将图像保存到数据库cakephp

时间:2014-04-22 18:10:10

标签: cakephp

我在使用cakephp上传图片并将图片的路径保存到数据库时遇到了一些麻烦。虽然我上传图像的代码很好,但它没有将url添加到数据库中...我不完全确定如何做到这一点。

这是我的控制器代码:

<?php

 class PhotosController extends AppController {

   public function photo() {
        $filename = '';
        if ($this->request->is('post')) { // checks for the post values

         $uploadData = $this->data['uploadFile']['pdf_path'];

            if ( $uploadData['size'] == 0 || $uploadData['error'] !== 0) { //             checks for the errors and size of the uploaded file
                return false;
            }
            $filename = basename($uploadData['name']); // gets the base name of the uploaded file
            $uploadFolder = WWW_ROOT. 'files';  // path where the uploaded file has to be saved
            $filename = time() .'_'. $filename; // adding time stamp for the uploaded image for uniqueness
            $uploadPath =  $uploadFolder . DS . $filename;
            if( !file_exists($uploadFolder) ){
                mkdir($uploadFolder); // creates folder if  not found
            }
            if (!move_uploaded_file($uploadData['tmp_name'], $uploadPath)) {
                return false;
            }

                 if (!empty($this->request->data)) {

             $this->Photo->create();
             $this->Photo->save($this->request->data['Photo']['url']);
             $this->Session->setFlash(__('Product Has Been Added'));

}


        }
       $this->set('image',$filename);

}

}

这允许我保存到文件服务器但是我不太确定如何将url保存到数据库的文件路径。

当我添加

  $this->Photo->create();
  $this->Photo->save($this->request->data);
  $this->Session->setFlash(__('Photo Has Been Added'));

保存了数据库条目,但这不包含“照片”表格“url”列中的文件路径。

提前致谢,

0 个答案:

没有答案