Cakephp上传图片 - 无法确定mimetype [英] Cakephp Upload Image - Can not determine the mimetype

查看:192
本文介绍了Cakephp上传图片 - 无法确定mimetype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型中正在进行以下验证

 'image'=> array(
'uploadErrror'=> array(
'rule'=>'uploadError',
'message'=>'图片上传失败。',
'allowEmpty'=> True
),
'mimeType'=> array(
'rule'=> array('mimeType',array('image / gif',' image $ p $','image / jpeg')),
'message'=>'请仅上传图片(gif,png,jpg)',
'allowEmpty'=> true
),
'fileSize'=> array(
'rule'=> array('fileSize','< =','1MB'),
'message '=>'Image必须小于1MB。',
'allowEmpty'=> true
),
'processImageUpload'=& =>'processImageUpload',
'message'=>'无法处理图片上传。',
'allowEmpty'=> true
))
$ b b
public function processImageUpload($ check = array()){
if(!is_uploaded_file($ check ['image'] ['tmp_name']))
{
return假;
}
$ targetdir = WWW_ROOT。 'img'。 DS。 'uploads'。 DS。 $ check ['image'] ['name'];
if(!move_uploaded_file($ check ['image'] ['tmp_name'],$ targetdir))
{
return false;
}
$ this-> data [$ this-> alias] ['image'] ='上传'。 DS。 $ check ['image'] ['name'];
return true;
}

我收到的错误如下,我也启用了debug 2,




$ b b $ b

我已尝试取消注释


extension = php_fileinfo.dll


< blockquote>

并完全重新启动WAMPServer,但仍然无法工作。



在控制器类中,我有以下代码添加

  public function add(){
if($ this-> request-> is ')){
$ this-> Image-> create();
$ data = $ this-> request-> data ['Image'];
if(!$ data ['image'] ['name'])
{
$ this-> Session-> setFlash(__('没有提供图片。 ));
}
if($ this-> Image-> save($ data)){
$ this-> Session-> setFlash(__ 。'));
return $ this-> redirect(array('action'=>'index'));
} else {
$ this-> Session-> setFlash(__('无法保存图片,请重试。
}
}
}

添加视图代码

 <?php echo $ this-> Form-> create('Image',array('type'=&文件')); ?> 
< fieldset>
< legend><?php echo __('Add Image'); ?>< / legend>
<?php
echo $ this-> Form-> input('description');
echo $ this-> Form-> input('image',array('type'=>'file'))
echo $ this-> Form-> input('property_id');
?>
< / fieldset>
<?php echo $ this-> Form-> end(__('Submit')); ?>

任何建议都将非常感激。

解决方案

不是检查mime而只是扩展的关闭解决方案是

  $ ext = substr(strtolower(strrchr($ check ['image'] ['name'],'。')),1); //获取扩展名
$ arr_ext = array('jpg','jpeg','gif'); // set allowed extensions

if(in_array($ ext,$ arr_ext)){
//上传代码
}
else
{
return'请只上传图片(gif,png,jpg)。
}


The following validation is being done in my model

'image' => array(
                'uploadErrror' => array(
               'rule' => 'uploadError',
                'message' => 'The image upload failed.',
                'allowEmpty'=> True
            ),
              'mimeType' => array(
                  'rule' => array('mimeType',array('image/gif','image/png','image/jpeg')),
                  'message' => 'Please only upload images (gif, png, jpg).',
                  'allowEmpty' => true
              ),
            'fileSize'=> array(
                'rule' => array('fileSize', '<=', '1MB'),
                'message' => 'Image must be less than 1MB.',
                'allowEmpty' => true
            ),
              'processImageUpload' => array(
                  'rule' => 'processImageUpload',
                  'message' => 'Unable to process image upload.',
                  'allowEmpty'=> true
              )  )


public function processImageUpload($check = array()){
        if(!is_uploaded_file($check['image']['tmp_name']))
        {
            return FALSE;
        }
        $targetdir = WWW_ROOT . 'img' . DS . 'uploads' . DS . $check['image']['name'];
        if(!move_uploaded_file($check['image']['tmp_name'],$targetdir))
        {
            return false;
        }
        $this->data[$this->alias]['image'] = 'uploads' . DS . $check['image']['name'];
        return true;
    }

The error I am receiving is the below, I also enabled debug 2 but no further clarification was provided.

Can not determine the mimetype.

I have already tried un-commenting

extension=php_fileinfo.dll

and restarted WAMPServer fully but it still did not work.

Also in the controller class I have the following code for add

public function add() {
        if ($this->request->is('post')) {
            $this->Image->create();
                        $data =  $this->request->data['Image'];
                        if (!$data['image']['name'])
                        {
                            $this->Session->setFlash(__('There was no image provided.'));
                        }
            if ($this->Image->save($data)) {
                $this->Session->setFlash(__('The image has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The image could not be saved. Please, try again.'));
            }
        }
    }

Add View Code

<?php echo $this->Form->create('Image',array('type'=>'file')); ?>
    <fieldset>
        <legend><?php echo __('Add Image'); ?></legend>
    <?php
        echo $this->Form->input('description');
        echo $this->Form->input('image',array('type'=>'file'));
        echo $this->Form->input('property_id');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>

Any suggestions would be greatly appreciated.

解决方案

The closes solution which isn't checking mime but just extension is

$ext = substr(strtolower(strrchr($check['image']['name'], '.')), 1); //get the extension
$arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions

if (in_array($ext, $arr_ext)) {
   //upload code
}
 else
{
   return 'Please only upload images (gif, png, jpg).';
}

这篇关于Cakephp上传图片 - 无法确定mimetype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆