Settings | Sign in | Sign up

There are currently 2 posts.

PHP判断一个文件是否为图像文件的方法

Floor 1 巨大八爪鱼 2/23/16 18:31
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Use finfo</title>
<style>
thead tr {
    background-color: #AAAAAA;
}
tbody tr:nth-child(even) {
    background-color: #EEEEEE;
}
</style>
</head>

<body>
<?php
$dirname = '../super2/imgs/';
$dir = dir($dirname);
echo '<table border="1" cellpadding="3" style="border-collapse:collapse">';
echo '<caption style="margin-bottom:4px">Image List</caption>';
echo '<thead><tr><th>Name</th><th>MIME Type</th></tr></thead><tbody>';
$finfo = new finfo(FILEINFO_MIME_TYPE);
while ($filename = $dir->read()) {
    if ($filename{0} == '.') {
        continue;
    }
    echo '<tr>';
    echo "<td><input type=\"text\" value=\"$filename\" size=\"5\"></td>";
    printf('<td><input type="text" value="%s" size="10"></td>', $finfo->file($dirname . $filename));
    echo '</tr>';
}
echo '</tbody></table>';
$dir->close();
?>
</body>
</html>
Floor 2 巨大八爪鱼 2/23/16 18:32
判断获得的MIME值是否以image/开头即可。

Content converter:

Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.
©2010-2025 Purasbar Ver3.0 [Mobile] [Desktop]
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported license.