gpt4 book ai didi

php - Laravel 检查图像路径字符串是否为图像

转载 作者:行者123 更新时间:2023-12-02 06:55:05 25 4
gpt4 key购买 nike

单击链接时,我将图像路径作为 GET 参数传递,但出于安全原因,我需要检查这是否是图像。

当我尝试此代码时,其中 $fileName是“15612.jpg”:

$fileName = $_GET['fileName'];

$image = array('file' => File::get('unverified-images/'.$fileName));
$rules = array('file' => 'image');
$validator = Validator::make($image, $rules);

if ($validator->fails()) {
Session::flash('error', 'Not an image');
return Redirect::to('controlpanel');
}

我测试过的所有 .jpg 文件都给出“不是图像”,但是当我尝试使用 .txt 文件时,它没有给出错误,这是为什么?我猜我做错了什么,因为验证器应该在它不是图像时失败,对吗?

我知道验证器需要 Input::file()而不是 File::get() ,但是如果我不使用表单,我该如何使用它?

最佳答案

这可能是避免验证器并自己进行检查的情况,因此您可以执行以下操作:

$allowedMimeTypes = ['image/jpeg','image/gif','image/png','image/bmp','image/svg+xml'];
$contentType = mime_content_type('path/to/image');

if(! in_array($contentType, $allowedMimeTypes) ){
Session::flash('error', 'Not an image');
return Redirect::to('controlpanel');
}

关于php - Laravel 检查图像路径字符串是否为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33283993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com