Php 如何在文件夹中只选择jpg文件并在html中显示
如何仅选择文件名/扩展名中包含*.jpg的文件,而忽略其他任何内容。我意识到我的代码试图获取它能在文件夹中获取的任何东西,我如何过滤它,使它只获取jpg文件Php 如何在文件夹中只选择jpg文件并在html中显示,php,html,Php,Html,如何仅选择文件名/扩展名中包含*.jpg的文件,而忽略其他任何内容。我意识到我的代码试图获取它能在文件夹中获取的任何东西,我如何过滤它,使它只获取jpg文件 <?php $folder = "photobooth/photobooth/Michelle_Illona_Alexander/animated/"; //folder tempat gambar disimpan $handle = opendir($folder); $i = 1; whil
<?php
$folder = "photobooth/photobooth/Michelle_Illona_Alexander/animated/"; //folder tempat gambar disimpan
$handle = opendir($folder);
$i = 1;
while(false !== ($file = readdir($handle) )){
if($file != '.' && $file != '..'){
$file2=str_replace("_mp4.jpg","",$file);
$file3=substr($file,0);
//$filenames= directory(".","jpg");
//foreach ($filenames as $value)
//{
echo '<li>'.
'<a href="photobooth/photobooth/Michelle_Illona_Alexander/animated/'.$file2.'.mp4">
<img src="photobooth/photobooth/Michelle_Illona_Alexander/animated/'.$file.'" width="300" title="" type="jpg"></a>'.
'<br/></li>';
if(($i % 4) == 0){
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
}
$i++;
if($i==0)
break;
//}
}
}
?>
您可以添加如下检查:
if( strpos($filename, '.jpg') >= 0) {
echo "this is a jpg image";
}
如果您只想要带有jpg的扩展名,那么可以将pregmatch与这个regex/^.*\(jpg)$/i
可以这样做:
<?php
$folder = "photobooth/photobooth/Michelle_Illona_Alexander/animated/"; //folder tempat gambar disimpan
$handle = opendir($folder);
$i = 1;
while(false !== ($file = readdir($handle) )){
if($file != '.' && $file != '..' && pathinfo("$folder/$file", PATHINFO_EXTENSION) == "jpg"){
$file2 = str_replace("_mp4.jpg","",$file);
$file3 = substr($file,0);
//$filenames = directory(".","jpg");
//foreach ($filenames as $value)
//{
echo '<li><a href="photobooth/photobooth/Michelle_Illona_Alexander/animated/'.$file2.'.mp4"><img src="photobooth/photobooth/Michelle_Illona_Alexander/animated/'.$file.'" width="300" title="" type="jpg"></a><br/></li>';
if(($i % 4) == 0){
echo '<br/><br/><br/><br/><br/><br/><br/><br/><br/>';
}
$i++;
if($i == 0){
break;
}
//}
}
}
?>
我看到的可能的重复,我认为它有点不同,因为我希望它只查看文件夹中的图像文件,而忽略任何不同类型的文件,例如*.mp4。老实说,我不知道如何把它写在我的代码里,我是新来的谢谢你的回答。。。但我认为它不起作用