0

我有一个程序可以显示我文件夹中的图片。

编码:

$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 文件?

4

4 回答 4

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。

于 2013-09-30T03:58:04.897 回答
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
        }
    }
于 2013-09-30T03:57:36.230 回答
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>";  
}
?>
于 2013-09-30T03:57:46.183 回答
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);
?>
于 2013-09-30T04:02:35.477 回答