【问题标题】:Uploading an image in cakephp在 cakephp 中上传图片
【发布时间】:2015-12-30 09:38:56
【问题描述】:

您好,我正在尝试在 cakephp 中上传图像,但它仅在数据库表中插入 jpg 时出现错误

警告 (2): move_uploaded_file(C:\xampp\htdocs\practise\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\praactise\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();?>

【问题讨论】:

    标签: cakephp


    【解决方案1】:

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

    C:\xampp\htdocs\practise\app\webroot\img\image\3.jpg

    image 文件夹存在于您的 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']);
    

    【讨论】:

    • move_uploaded_file ( $this-&gt;request-&gt;data['User']['image_file']['tmp_name'], IMAGES.'image'.DS.$this-&gt;User-&gt;id.'.'. $extension );你创建了image文件夹吗?
    • 我在 webroot/img/uploads 中创建了文件夹
    • 希望你得到解决方案:)
    • 但在数据库中它只保存为 jpg 它不保存图像名称
    • $this-&gt;User-&gt;saveField('image',$extension); 您单独保存扩展程序?请将其更改为名称
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多