PHP识别链接后缀:获取文件类型和验证扩展名
PHP可以使用pathinfo函数来获取文件路径的信息,包括文件名、扩展名等。以下是获取链接后缀的代码示例:
$url = 'https://www.example.com/path/to/file.html';
$ext = pathinfo($url, PATHINFO_EXTENSION);
echo $ext; // 输出:html
此外,如果要判断链接是否为某种格式的文件,可以使用in_array函数来判断扩展名是否在指定的数组中,例如:
$url = 'https://www.example.com/path/to/file.html';
$allowed_exts = array('html', 'php', 'css');
$ext = pathinfo($url, PATHINFO_EXTENSION);
if (in_array($ext, $allowed_exts)) {
echo 'This is a valid file format.';
} else {
echo 'This is not a valid file format.';
}
原文地址: https://www.cveoy.top/t/topic/mjz9 著作权归作者所有。请勿转载和采集!