Php 只显示图片
我有这个剧本:Php 只显示图片,php,Php,我有这个剧本: $uploadsDirectory = dirname($_SERVER['SCRIPT_FILENAME']) .'/slides/head/'; if ($handle = opendir($uploadsDirectory)) { $uplo = array(); while (false !== ($file = readdir($handle))) { array_push($uplo, $file);} sort($uplo,SOR
$uploadsDirectory = dirname($_SERVER['SCRIPT_FILENAME']) .'/slides/head/';
if ($handle = opendir($uploadsDirectory)) {
$uplo = array();
while (false !== ($file = readdir($handle))) {
array_push($uplo, $file);}
sort($uplo,SORT_NATURAL | SORT_FLAG_CASE);
$user = array();
foreach($uplo as $fname) {
if($fname != ".." && $fname != "."){
if(substr($fname,0,1) != "_")
echo "<div class='bgitem' id='head'>$fname</div>";
else
array_push($user, "$fname");}}
closedir($handle);}
它工作得很好,但是我怎样才能使它只显示图片呢?我有其他不是照片的文件,因此它会显示一张损坏的图片。您的解决方案:
$extension = explode(".", $fname);
$extension = (isset($extension) && count($extension) > 0)?strtolower($extension[count($extension) -1]):null;
if(in_array($extension, ['jpg', 'jpeg', 'png', 'gif'])){
//Show the image
}else{
//dont show image
}
一种简单的方法是让它测试该文件是否是与您测试该文件是父目录还是当前目录(如果$fname!=…&&&$fname!=。{ 可以使用getimagesize确定文件是否为任何类型的图像。如果不是图像,则返回零
$uploadsDirectory = dirname($_SERVER['SCRIPT_FILENAME']) .'/slides/head/';
if ($handle = opendir($uploadsDirectory)) {
$uplo = array();
while (false !== ($file = readdir($handle))) {
array_push($uplo, $file);}
sort($uplo,SORT_NATURAL | SORT_FLAG_CASE);
$user = array();
foreach($uplo as $fname) {
if($fname != ".." && $fname != "." && getimagesize($fname) != 0){ //Tests if file is an iamge
if(substr($fname,0,1) != "_")
echo "<div class='bgitem' id='head'>$fname</div>";
else
array_push($user, "$fname");}}
closedir($handle);}
为什么不制作一个具有可接受扩展名的筛选器数组呢?如果$fname是一个图片{显示它}