为文件上载添加安全对象,如下所示:
class FileSecure
{
public resource $Allowed;
private object $Info;
public function __construct($allow)
{
$this->Allowed = $allow;
$this->Info = new finfo();
}
public function Check($file) : bool
{
if(in_array($fileType = $this->Info->file($file, FILEINFO_MIME_TYPE, $this->Allowed))) { return true; } else { return false; }
}
}
$fileCheck = array(
\'Image\' => new FileSecure([\'image/bmp\', \'image/gif\', \'image/jpeg\', \'image/png\']),
\'Text\' => new FileSecure([\'text/plain\']),
\'Compressed\' => new FileSecure([\'application/zip\', \'application/x-rar-compressed\'])
);
Click here to view all the MIME name\'s to extensions
然后添加一个允许文件上传的前端表单,并在使用上传的文件之前通过该对象。
// note it must exist on the server before checking
if(!$fileCheck[\'Image\']->Check($filename)):
// delete the file
endif;
您可以使用数据库保存文件位置,或者在服务器上有一个默认的上载目录,并遍历该目录中保存的文件。