1、文件地址:\crmeb\crmeb\services\upload\BaseUpload.php
把 checkFileContent 方法改成下面内容:
public function checkFileContent($fileHandle)
{
// 获取文件扩展名
$extension = strtolower(pathinfo($fileHandle->getOriginalName(), PATHINFO_EXTENSION));
$safeBinaryExt = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'zip', 'rar', '7z', 'mp4', 'avi', 'mov', 'mp3', 'wav', 'xlsx', 'xls', 'docx', 'doc', 'pdf'];
if (in_array($extension, $safeBinaryExt)) {
return true;
}
// 只对文本/代码类文件做内容检测
$stream = fopen($fileHandle->getPathname(), 'r');
$content = (fread($stream, filesize($fileHandle->getPathname())));
if (is_resource($stream)) {
fclose($stream);
}
// 检测 PHP 代码标签
if (preg_match('/<\?php|<\?=|<\?[\s]/i', $content)) {
return $this->setError('文件内容包含非法代码');
}
// 检测危险函数调用
if (preg_match('/\beval\s*\(|\bsystem\s*\(|\bexec\s*\(|\bshell_exec\s*\(|\bpassthru\s*\(|\bassert\s*\(|\bcreate_function\s*\(|\bpopen\s*\(|\bproc_open\s*\(|\bpcntl_exec\s*\(/i', $content)) {
return $this->setError('文件内容包含非法代码');
}
// 检测框架敏感关键词
if (preg_match('/\bthink\b|\bphar\b|\bSocket\b|\bChannel\b|\bFlysystem\b|\bPsr6Cache\b|\bCached\b|\bRequest\b|\bdebug\b|\bPsr6Cachepool\b/i', $content)) {
return $this->setError('文件内容不合法');
}
return true;
}
2、文件地址:\crmeb\crmeb\services\upload\storage\Local.php
在 Local.php 的 move 方法中,图片上传后加上:

// 如果是图片,验证是否为真实图片
$extension = strtolower(pathinfo($fileHandle->getOriginalName(), PATHINFO_EXTENSION));
if (in_array($extension, ['jpg', 'jpeg', 'png', 'gif', 'webp'])) {
$imageInfo = @getimagesize($fileHandle->getPathname());
if (!$imageInfo) {
return $this->setError('不是合法的图片文件');
}
}
附件下载:

