{"id":1290,"date":"2025-10-23T15:32:44","date_gmt":"2025-10-23T07:32:44","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/yycxsc\/1290.html"},"modified":"2025-10-23T15:33:13","modified_gmt":"2025-10-23T07:33:13","slug":"php%e5%88%a4%e6%96%ad%e6%96%87%e4%bb%b6%e7%b1%bb%e5%9e%8b%e6%96%b9%e6%b3%95%ef%bc%8c%e6%a3%80%e7%b4%a2%e6%96%87%e4%bb%b6%e7%b1%bb%e5%9e%8b%e6%98%af-html%e8%bf%98%e6%98%af%e5%9b%be%e7%89%87","status":"publish","type":"yycxsc","link":"https:\/\/www.zhaozhao123.cn\/php\/yycxsc\/1290.html","title":{"rendered":"PHP\u5224\u65ad\u6587\u4ef6\u7c7b\u578b\u65b9\u6cd5\uff0c\u68c0\u7d22\u6587\u4ef6\u7c7b\u578b\u662f.html\u8fd8\u662f\u56fe\u7247"},"content":{"rendered":"<p>\u5728PHP\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5224\u65ad\u6587\u4ef6\u8def\u5f84\u662fHTML\u6587\u4ef6\u8fd8\u662f\u56fe\u7247\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p><h2 class=\"wp-block-heading\">1. \u4f7f\u7528 pathinfo() \u51fd\u6570\u83b7\u53d6\u6269\u5c55\u540d<\/h2><pre class=\"wp-block-code\"><code>&lt;?php\n\/\/$filePath\uff1a\u6587\u4ef6\u8def\u5f84\nfunction checkFileType($filePath) {\n    $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));\n\n    \/\/ \u56fe\u7247\u6269\u5c55\u540d\u5217\u8868\n    $imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg'];\n\n    if ($extension === 'html' || $extension === 'htm') {\n        return 'HTML\u6587\u4ef6';\n    } elseif (in_array($extension, $imageExtensions)) {\n        return '\u56fe\u7247\u6587\u4ef6';\n    } else {\n        return '\u5176\u4ed6\u6587\u4ef6\u7c7b\u578b';\n    }\n}\n\n\/\/ \u6d4b\u8bd5\u793a\u4f8b\n$files = [\n    'index.html',\n    'photo.jpg',\n    'image.png',\n    'document.htm',\n    'picture.gif',\n    'style.css'\n];\n\nforeach ($files as $file) {\n    echo $file . ' - ' . checkFileType($file) . \"n\";\n}\n?&gt;<\/code><\/pre><h2 class=\"wp-block-heading\">2. \u4f7f\u7528 finfo \u51fd\u6570\u8fdb\u884c\u66f4\u7cbe\u786e\u7684\u6587\u4ef6\u7c7b\u578b\u68c0\u6d4b<\/h2><pre class=\"wp-block-code\"><code>&lt;?php\nfunction getFileMimeType($filePath) {\n    if (!file_exists($filePath)) {\n        return '\u6587\u4ef6\u4e0d\u5b58\u5728';\n    }\n\n    $finfo = finfo_open(FILEINFO_MIME_TYPE);\n    $mimeType = finfo_file($finfo, $filePath);\n    finfo_close($finfo);\n\n    return $mimeType;\n}\n\nfunction checkFileTypeAdvanced($filePath) {\n    $mimeType = getFileMimeType($filePath);\n\n    if (strpos($mimeType, 'text\/html') !== false) {\n        return 'HTML\u6587\u4ef6';\n    } elseif (strpos($mimeType, 'image\/') !== false) {\n        return '\u56fe\u7247\u6587\u4ef6 (' . $mimeType . ')';\n    } else {\n        return '\u5176\u4ed6\u6587\u4ef6\u7c7b\u578b (' . $mimeType . ')';\n    }\n}\n\n\/\/ \u6d4b\u8bd5\u793a\u4f8b\n$file = 'example.jpg';\necho checkFileTypeAdvanced($file);\n?&gt;<\/code><\/pre><h2 class=\"wp-block-heading\">3. \u7ed3\u5408\u6269\u5c55\u540d\u548cMIME\u7c7b\u578b\u7684\u5b8c\u6574\u89e3\u51b3\u65b9\u6848<\/h2><pre class=\"wp-block-code\"><code>&lt;?php\nclass FileTypeChecker {\n    private static $imageExtensions = [\n        'jpg', 'jpeg', 'png', 'gif', 'bmp', \n        'webp', 'svg', 'ico', 'tiff', 'tif'\n    ];\n\n    private static $htmlExtensions = ['html', 'htm'];\n\n    public static function isImage($filePath) {\n        $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));\n        return in_array($extension, self::$imageExtensions);\n    }\n\n    public static function isHTML($filePath) {\n        $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));\n        return in_array($extension, self::$htmlExtensions);\n    }\n\n    public static function getFileType($filePath) {\n        if (self::isHTML($filePath)) {\n            return 'HTML';\n        } elseif (self::isImage($filePath)) {\n            return 'IMAGE';\n        } else {\n            return 'OTHER';\n        }\n    }\n\n    \/\/ \u9a8c\u8bc1\u6587\u4ef6\u5b9e\u9645\u5185\u5bb9\uff08\u66f4\u5b89\u5168\u7684\u65b9\u6cd5\uff09\n    public static function validateImageFile($filePath) {\n        if (!file_exists($filePath)) {\n            return false;\n        }\n\n        $imageInfo = @getimagesize($filePath);\n        return $imageInfo !== false;\n    }\n}\n\n\/\/ \u4f7f\u7528\u793a\u4f8b\n$testFiles = [\n    'index.html',\n    'photo.jpg',\n    'style.css',\n    'logo.png',\n    'document.htm'\n];\n\nforeach ($testFiles as $file) {\n    $type = FileTypeChecker::getFileType($file);\n    echo \"$file - \u7c7b\u578b: $typen\";\n\n    if (FileTypeChecker::isImage($file)) {\n        echo \"  \u2713 \u8fd9\u662f\u4e00\u4e2a\u56fe\u7247\u6587\u4ef6n\";\n    }\n\n    if (FileTypeChecker::isHTML($file)) {\n        echo \"  \u2713 \u8fd9\u662f\u4e00\u4e2aHTML\u6587\u4ef6n\";\n    }\n}\n?&gt;<\/code><\/pre><h2 class=\"wp-block-heading\">4. \u5904\u7406URL\u8def\u5f84\u7684\u60c5\u51b5<\/h2><pre class=\"wp-block-code\"><code>&lt;?php\nfunction checkUrlFileType($url) {\n    \/\/ \u4eceURL\u4e2d\u63d0\u53d6\u6587\u4ef6\u540d\n    $path = parse_url($url, PHP_URL_PATH);\n    $filename = basename($path);\n\n    $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));\n\n    $imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'];\n    $htmlExtensions = ['html', 'htm'];\n\n    if (in_array($extension, $htmlExtensions)) {\n        return 'HTML';\n    } elseif (in_array($extension, $imageExtensions)) {\n        return 'IMAGE';\n    } else {\n        return 'UNKNOWN';\n    }\n}\n\n\/\/ \u6d4b\u8bd5URL\n$urls = [\n    'https:\/\/example.com\/index.html',\n    'https:\/\/example.com\/images\/photo.jpg',\n    'https:\/\/example.com\/page.htm',\n    'https:\/\/example.com\/logo.png'\n];\n\nforeach ($urls as $url) {\n    echo \"$url - \" . checkUrlFileType($url) . \"n\";\n}\n?&gt;<\/code><\/pre><h2 class=\"wp-block-heading\">\u4e3b\u8981\u65b9\u6cd5\u603b\u7ed3\uff1a<\/h2><ol class=\"wp-block-list\">\n<li><strong>pathinfo()<\/strong> &#8211; \u6700\u5e38\u7528\uff0c\u57fa\u4e8e\u6587\u4ef6\u6269\u5c55\u540d<\/li>\n\n\n\n<li><strong>finfo<\/strong> &#8211; \u66f4\u51c6\u786e\uff0c\u57fa\u4e8e\u6587\u4ef6\u5185\u5bb9\u68c0\u6d4b<\/li>\n\n\n\n<li><strong>getimagesize()<\/strong> &#8211; \u4e13\u95e8\u9a8c\u8bc1\u56fe\u7247\u6587\u4ef6\u7684\u6709\u6548\u6027<\/li>\n\n\n\n<li><strong>parse_url()<\/strong> &#8211; \u5904\u7406URL\u8def\u5f84\u7684\u60c5\u51b5<\/li>\n<\/ol><p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\uff1a<\/p><ul class=\"wp-block-list\">\n<li>\u5982\u679c\u53ea\u662f\u7b80\u5355\u5224\u65ad\uff0c\u4f7f\u7528 <code>pathinfo()<\/code> \u6700\u5feb<\/li>\n\n\n\n<li>\u5982\u679c\u9700\u8981\u5b89\u5168\u9a8c\u8bc1\uff0c\u5efa\u8bae\u7ed3\u5408\u6269\u5c55\u540d\u548c\u5185\u5bb9\u68c0\u6d4b<\/li>\n\n\n\n<li>\u5bf9\u4e8e\u7528\u6237\u4e0a\u4f20\u7684\u6587\u4ef6\uff0c\u5f3a\u70c8\u5efa\u8bae\u4f7f\u7528 MIME \u7c7b\u578b\u9a8c\u8bc1<\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>\u5728PHP\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5224\u65ad\u6587\u4ef6\u8def\u5f84\u662fHTML\u6587\u4ef6\u8fd8\u662f\u56fe\u7247\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 pathinfo() \u51fd\u6570\u83b7\u53d6\u6269\u5c55\u540d 2. \u4f7f\u7528 finfo \u51fd\u6570\u8fdb\u884c\u66f4\u7cbe\u786e\u7684\u6587\u4ef6\u7c7b\u578b\u68c0\u6d4b 3. \u7ed3\u5408\u6269\u5c55\u540d\u548cMIME\u7c7b\u578b\u7684\u5b8c\u6574\u89e3\u51b3\u65b9\u6848 4. \u5904\u7406URL\u8def\u5f84\u7684\u60c5\u51b5 \u4e3b\u8981\u65b9\u6cd5\u603b\u7ed3\uff1a \u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":true},"tags":[],"yycxsc2nav":[2057],"tuisongtax":[],"class_list":["post-1290","yycxsc","type-yycxsc","status-publish","hentry","yycxsc2nav-wjjs"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"PHP\u5224\u65ad\u6587\u4ef6\u7c7b\u578b\uff08.html\u4e0e\u56fe\u7247\uff09","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":"PHP\u5224\u65ad\u6587\u4ef6\u7c7b\u578b\uff08.html\u4e0e\u56fe\u7247\uff09"}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/yycxsc\/1290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/yycxsc"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/yycxsc"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=1290"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=1290"},{"taxonomy":"yycxsc2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/yycxsc2nav?post=1290"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=1290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}