这是我的代码:
<?php
$class = array();
/*$class = array_reverse($class_model, false);*/
$cycle = 199 ;
$i= 0;
$contentdirectory = 'DEADWAVE_proj_dir/DEAD-WAVE_content/';
$contentlaunch = scandir($contentdirectory);
natsort($contentlaunch);
$id = count($contentlaunch);
foreach ( $contentlaunch as $value )
{
if ( preg_match("/.png",$value)
|| preg_match("/.jpg",$value)
|| preg_match("/.gif",$value) )
{
echo '<a id="'.--$id.'"target="_blank" href="">
<img class="'.$class[ $i % $cycle ].'"
src="DEADWAVE_proj_dir/DEAD-WAVE_content/'.$value.'"/></a>';
++$i;
}
}
&GT;
这根本不起作用,问题是为什么。
我想象一下,检查文件类型和echo img标签只能检查那些避免目录中非图像文件的图像的数组值。我对所有解决方案持开放态度。
答案 0 :(得分:0)
要查明文件是否为图像,您可以使用:
// $path = 'somepath';
// $filename = 'somefile';
$finfo = new finfo();
$currentFileMimeType = $finfo->file($path . $filename, FILEINFO_MIME_TYPE);
if('image' == array_shift((explode('/', $currentFileMimeType))))
{
// echo images
}
答案 1 :(得分:0)
你的正则表达式模式不起作用。例如,"/.png"
应为/\.png/
不是使用正则表达式,而是使用函数pathinfo,它将为您提供给定路径的文件扩展名。
$info = pathinfo($value);
$extension = $info['extension'];
if (in_array($extension, array('png', 'jpg', 'gif')) {
..
答案 2 :(得分:0)
<?php
$class = array();
$class = array_reverse($class_model, false);
$cycle = 199 ;
$i = 0;
$contentdirectory = 'DEADWAVE_proj_dir/DEAD-WAVE_content/';
$contentlaunch = scandir($contentdirectory);
natsort($contentlaunch);
foreach ( $contentlaunch as $id => $value )
{
if ( preg_match("/(\/.png|\/.jpg|\/.gif)/",$value) )
{
echo '<a id="'.$id.'" target="_blank" href=""><img class="'.$class[ $i++ % $cycle ].'" src="DEADWAVE_proj_dir/DEAD-WAVE_content/'.$value.'"/></a>';
}
}
?>
注意:我不会使用此方法来检查imagess,而是使用brian_d方法:
if ( in_array(pathinfo($value, PATHINFO_EXTENSION), array('jpg', 'gif', 'png')) ) { ...
最后,我不确定所有$ class / $ cycle的东西......