环境描述:
PHP 8.1
CKFinder 4.0
背景:
插件从以前的程序移植的,一直运行良好,唯一差别是换了PHP8.1环境。
问题描述:
第1坑:
CKSource\CKFinder\Filesystem\File\UploadedFile::__construct(): Argument #1 ($uploadedFile) must be of type Symfony\Component\HttpFoundation\File\UploadedFile, array given, called in /data/www/chisu_font/ThinkPHP/Library/Vendor/Ckfinder/cksource/ckfinder/src/CKSource/CKFinder/Command/FileUpload.php on line 68
CKSource\CKFinder\Filesystem\File\UploadedFile::__construct(): Argument #1 ($uploadedFile) must be of type Symfony\Component\HttpFoundation\File\UploadedFile, array given, called in /data/www/chisu_font/ThinkPHP/Library/Vendor/Ckfinder/cksource/ckfinder/src/CKSource/CKFinder/Command/FileUpload.php on line 68
这个问题是由于CKFinder引用了symfony框架,symfony框架版本是2.7的,太老了,不和新的PHP环境兼容。
相关的修复方案及问题讨论:
https://github.com/Largo/symfony-http-foundation/pull/1/commits/45e8d45a170f92e79fde915e8da71b8adc5471ba
https://github.com/symfony/symfony/issues/50624
参照第一个修改方案,在 /Ckfinder/symfony/http-foundation/FileBag.php 文件中的protected function fixPhpFilesArray($data)函数新增一行:
protected function fixPhpFilesArray($data)
{
unset($data['full_path']);
if (!is_array($data)) {
return $data;
}
$keys = array_keys($data);
sort($keys);
if (self::$fileKeys != $keys || !isset($data['name']) || !is_array($data['name'])) {
return $data;
}
$files = $data;
foreach (self::$fileKeys as $k) {
unset($files[$k]);
}
foreach ($data['name'] as $key => $name) {
$files[$key] = $this->fixPhpFilesArray(array(
'error' => $data['error'][$key],
'name' => $name,
'type' => $data['type'][$key],
'tmp_name' => $data['tmp_name'][$key],
'size' => $data['size'][$key],
));
}
return $files;
}
第2坑:
$config_backends = $config->get(“backends”);
$config_root = $config_backends[0][‘root’];
上述代码无法取到配置数据,需要改用:
$config_root = $config_backends[‘default’][‘root’];
这块基本没影响,因为我原来自己写了文件重复的命名规则。
第3坑:
Unsupported image type (not resource): image/png
因为我测试程序是上传的图片,一直出这个错误,很费解。
调了半天,发现是PHP8.1把 imagecreatefromstring 函数返回值改了:https://www.php.net/manual/en/function.imagecreatefromstring.php
8.0.0 On success, this function returns a GDImage instance now; previously, a resource was returned.
7.3.0 WEBP is supported now (if supported by the libgd in use).
修改方案,修改 /Ckfinder/cksource/ckfinder/src/CKSource/CKFinder/Image.php
构造函数:public function __construct($imageData, $bmpSupport = false) 中
if (!is_resource($this->gdImage)) {
throw new CKFinderException('Unsupported image type (not resource): ' . $this->mime);
}
修改成:
if ($this->gdImage === false) {
throw new CKFinderException('Unsupported image type (not resource): ' . $this->mime);
}
总结:
(1)旧系统插件,新环境兼容性差,尽早换,或者自己写;
(2)PHP环境向前兼容做得不好。