【问题标题】:CakePhp, How do i add to check if the uploaded file is image only?CakePhp,如何添加以检查上传的文件是否仅为图像?
【发布时间】:2026-02-08 16:10:01
【问题描述】:

一直在尝试将一些代码添加到旧编写的程序中,我没有编写这些代码,因为我不知道这段代码可能工作得有多好。 以下代码以不同的形式在两个不同的页面中起作用。

$type = $this->data['Gallery']['type'];

if (!empty($this->data)) {
    if (!isset($this->data['Gallery']['gallery_category_id'])) {
        if ($this->data['Gallery']['type'] == 1) {
            echo "<script>alert('" . INFOGALLERYSECTION . "')</script>";
        } elseif ($this->data['Gallery']['type'] == 2) {
            echo "<script>alert('" . INFOSHROTSECTION . "')</script>";
        } else {
        }
    } else {
        // set the upload destination folder
        //$destination = realpath('../../app/webroot/img/gallery') . '/';

        $bigimg = WWW_ROOT . 'img/gallery/big/';
        $smallimg = WWW_ROOT . 'img/gallery/small/';

        // grab the file
        $file = $this->data['Gallery']['photofile'];
        $imageTypes = array("image/gif", "image/jpeg", "image/png"); //List of accepted file extensions. 
        foreach ($iamgeTypes as $type) {                 //check if image type fits one of allowed types
            if ($type == $this->data['type']) {
                // upload the image using the upload component
                $result = $this->Upload->upload($file, $bigimg, null, array('type' => 'resize', 'size' => '965', 'output' => 'jpg'));
                $result = $this->Upload->upload($file, $smallimg, null, array('type' => 'resize', 'size' => '146', 'output' => 'jpg'));
            }
        }
    }
}

【问题讨论】:

  • 使用 cakephp 验证
  • public $validate = array( 'image' => array( 'rule' => array( 'extension', array('gif', 'jpeg', 'png', 'jpg') ), 'message' => '请提供有效图片。' ) );

标签: php validation cakephp mime-types fileinfo


【解决方案1】:

您需要获取上传文件的 mime 类型。根据您的 PHP 版本,有两种方法可以做到这一点。

PHP 5.3 >=, PECL 文件信息 >= 0.1.0

$file = $this->data['Gallery']['photofile'];
$imageTypes = array("image/gif", "image/jpeg", "image/png"); //List of accepted file extensions.
// get file mime type
$fileInfo = finfo_open(FILEINFO_MIME_TYPE);
$fileType = finfo_file($fileInfo, $file);
finfo_close($fileInfo);

if (in_array($fileType, $imageTypes)) {
    $result = $this->Upload->upload($file, $bigimg, null, array('type' => 'resize', 'size' => '965', 'output' => 'jpg'));
    $result = $this->Upload->upload($file, $smallimg, null, array('type' => 'resize', 'size' => '146', 'output' => 'jpg'));
}

您可以在documentation 中找到有关fileinfo 函数的更多信息。

PHP 5.3

// get file mime type
$fileType = mime_content_type($file);

Documentation 用于mime_content_type() 函数。

我不知道您的模型是什么样子,但这是在验证器中移动检查 mime 类型的好方法。

【讨论】:

  • 非常感谢您浏览它,所以使用mimi我不必使用foreach来不断检查上传文件的扩展名?
  • 我用ifin_array() function更改了foreach循环和if语句。
  • 它不工作。也许代码本身一定有问题。
  • 如果图像文件到达if语句尝试调试。
  • 我会期待的,非常感谢你的帮助:)
【解决方案2】:
$file = $this->data["Gallery"]['photofile'];
if(!empty($file['tmp_name']))
{
    $type = explode('/', $file['type']);
    if($type[0] == 'image')
    {
     // Your Code
    }
}

【讨论】:

  • 你好。虽然这可能是问题的答案,但最好也解释一下它为什么会有所帮助。尝试向 OP 解释错误是什么,以及您的解决方案如何解决它。