在cakephp上传图像

时间:2015-12-30 09:38:57

标签: cakephp

嗨我试图在cakephp中上传一个图片,但是它只是在数据库表中插入jpg而得到错误

警告(2):move_uploaded_file(C:\ xampp \ htdocs \ practice \ app \ webroot \ img \ image \ 3.jpg):无法打开流:没有这样的文件或目录[APP \ Controller \ UsersController.php ,第98行]

警告(2):move_uploaded_file()[function.move-uploaded-file]:无法移动' C:\ xampp \ tmp \ php24B4.tmp'到' C:\ xampp \ htdocs \ practice \ app \ webroot \ img \ image \ 3.jpg' [APP \ Controller \ UsersController.php,第98行]

这是我的代码

控制器:

$extension =strtolower(pathinfo($this->request->data['User']['image_file']['name'],PATHINFO_EXTENSION));
            if( 
                !empty ($this->request->data['User']['image_file']['tmp_name'])&&
                in_array($extension,array('png','jpeg','jpg'))
              )
              {
                move_uploaded_file
                (
                    $this->request->data['User']['image_file']['tmp_name'],
                    IMAGES.'image'.DS.$this->User->id.'.'. $extension
                );
                $this->User->saveField('image',$extension);
              }
              elseif(!empty ($this->request->data['User']['image_file']['tmp_name']))

观看:

<?php echo $this->Form->create('User',array('type' =>'file'));?>
<?php echo $this->Form- >input('image_file',array('label'=>'Image(jpeg,jpg,png)','type' =>'file'));?>
<?php echo $this->Form->end();?>

1 个答案:

答案 0 :(得分:1)

确保您的图片文件夹路径正确无误?

C:\ XAMPP \ htdocs中\实践\应用\根目录\ IMG \图像\ 3.JPG

img 文件夹中是否存在

图片文件夹?

请替换此

move_uploaded_file($this->request->data['User']['image_file']['tmp_name'],IMAGES.'image'.DS.$this->User->id.'.'. $extension);

$this->User->saveField('image',$extension);

move_uploaded_file($this->request->data['User']['image_file']['tmp_name'],IMAGES.'image'.DS.$this->request->data['User']['image_file']['name']);

$this->User->saveField('image',$this->request->data['User']['image_file']['name']);