我有一个程序来显示我的文件夹中的图片。
代码:
$dir = dir("tags/carrot_cake");
while($filename=$dir->read()) {
if($filename == "." || $filename == ".." || $filename == $first_image) continue;
echo "<div class='cp-thumb'>";
/**********************************sql query to fetch date time and caption************************/
$timeqry = mysql_query("SELECT created_date FROM foodsites.images WHERE img_name='$filename'") or die(mysql_error());
$row1 = mysql_fetch_row($timeqry);
$datetime = $row1[0];
$newDatetime = date('d/m/Y h:i A', strtotime($datetime));
$capqry = mysql_query("SELECT caption FROM foodsites.images WHERE img_name='$filename'") or die(mysql_error());
$row2 = mysql_fetch_row($capqry);
$caption = $row2[0];
echo "<div class='cp-hover' style='display: none;' ><div class='cpHover-bg'></div>
<div class='cpHover-info'><p class='text11'>".$newDatetime."</p><p class='text10'>".$caption."</p></div></div>";
echo "<img src='tags/carrot_cake/".$filename."'class='img_235x235' />
</div>";
}
在我的代码中,我发现我获取目录中的所有文件包括那些不是jpg文件的文件。
如何将其更改为仅抓取jpg文件?
答案 0 :(得分:2)
您应该更改此行:
if($filename == "." || $filename == ".." || $filename == $first_image) continue;
到:
if ($filename != "." && $filename != ".." && strtolower(substr($filename , strrpos($filename , '.') + 1)) == 'jpg') continue;
另一种方法是使用glob()
:
$filename = glob('/tags/carrot_cake/*.jpg');
在glob()
情况下,这将返回一个包含匹配文件/目录的数组,如果没有文件匹配则返回空数组,如果错误则返回FALSE。
答案 1 :(得分:0)
试试这个
while($filename=$dir->read()) {
$allowed='jpg'; //which file types are allowed seperated by comma
$extension_allowed= explode(',', $allowed);
$file_extension= pathinfo($filename, PATHINFO_EXTENSION);
if(array_search($file_extension, $extension_allowed))
{
//allowed
}
else
{
//not allowed
}
}
答案 2 :(得分:0)
<?php
$dir = dir("tags/carrot_cake");
while($filename=$dir->read()) {
$filename_mime = image_type_to_mime_type(exif_imagetype($filename));
if($filename == "." || $filename == ".." || $filename == $first_image || ($filename_mime != "image/pjpeg" || $filename_mime != "image/jpeg")) continue;
echo "<div class='cp-thumb'>";
/**********************************sql query to fetch date time and caption************************/
$timeqry = mysql_query("SELECT created_date FROM foodsites.images WHERE img_name='$filename'") or die(mysql_error());
$row1 = mysql_fetch_row($timeqry);
$datetime = $row1[0];
$newDatetime = date('d/m/Y h:i A', strtotime($datetime));
$capqry = mysql_query("SELECT caption FROM foodsites.images WHERE img_name='$filename'") or die(mysql_error());
$row2 = mysql_fetch_row($capqry);
$caption = $row2[0];
echo "<div class='cp-hover' style='display: none;' ><div class='cpHover-bg'></div>
<div class='cpHover-info'><p class='text11'>".$newDatetime."</p><p class='text10'>".$caption."</p></div></div>";
echo "<img src='tags/carrot_cake/".$filename."'class='img_235x235' />
</div>";
}
?>
答案 3 :(得分:0)
试试此代码
<?php
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
// Output the image
imagejpeg($im);
// Free up memory
imagedestroy($im);
?>