使用PHP显示文件夹中的文件数
我有一个简单的脚本来显示文件夹中的所有图像使用PHP显示文件夹中的文件数,php,html,Php,Html,我有一个简单的脚本来显示文件夹中的所有图像 <?php foreach(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE) as $images) { $filecount = count(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE)); if ($filecount >1) { e
<?php
foreach(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE) as $images)
{
$filecount = count(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE));
if ($filecount >1)
{
echo "<img width='75' height='auto' style='margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$images."\">";
}
else
{
echo "<img width='200' height='auto' style='margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$images."\">";
}
}
?>
我想显示文件夹中的文件数。下面给出我尝试的内容。我的问题是,这会显示每个图像之前的文件数
if ($filecount >1)
{
echo '' . $user . ' ' .'added ' . ' ' . $filecount . ' ' . 'new photos';
echo "<img width='75' height='auto' style='margin-right: 3px; alt='".$row["caption"] ."' src=\"".$images."\">";
}
if($filecount>1)
{
回显'.$user'.'已添加'.$filecount'.'新照片';
回声“;
}
如何在显示图像组之前显示文件数?只有在图像组收集完文件后,才需要显示文件数。
您可以通过在循环之前声明filecount变量来完成此操作,然后每次循环转到下一个结果时都需要对值进行计数和求和,并最终显示文件总数。
下面是编写代码的方法:
<?php
$filecount = 0;
foreach(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE) as $images)
{
$tempFileCount = count(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE))
$filecount += $tempFileCount;
if ($tempFileCount > 1)
{
echo "<img width='75' height='auto' style='margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$images."\">";
}
else
{
echo "<img width='200' height='auto' style='margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$images."\">";
}
}
if($filecount > 1)
{
echo '' . $user . ' ' .'added ' . ' ' . $filecount . ' ' . 'new photos';
}
?>
首先创建一个函数,将所有图像作为数组返回。如果php函数出现错误,这将引发异常(通常不会发生,但值得在发生时知道)。这个函数会给人一种感觉,代码更干净一点
<?php
function getAllImagesOnDirectory($directory) {
$imagesArray = glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE);
if(!is_array($imagesArray)) {
throw new RuntimeException("There is a problem getting images on directory " . $directory);
}
return $imagesArray;
}
//Then we call this function to get all the images.
$allImagesOnFilePath = getAllImagesOnDirectory($filePath);
$numberOfImages = count($allImagesOnFilePath);
if($numberOfImages > 0) {
echo '<p>' . $user . ' added ' . $numberOfImages . ' new photos</p>';
//If the number of images is more than 0 then traverse all the images and echo them
foreach($allImagesOnFilePath as $oneImage) {
echo "<img style='max-width:100%;height:auto;margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$oneImage."\">";
}
}
else {
//The user has no uploaded images
echo '<p>' . $user . ' has not added new photos yet :(</p>';
}
这是什么语言,你应该添加一个标记抱歉。这是php。我已经添加了标记。也许做一个foreach循环来获取文件的数量,然后在主foreach循环之前打印出来谢谢你的回答。我试试看……)如果这对你有用,请让我知道,我会将其作为答复提交。我在这行中发现了一个错误并更正了它$filecount+=$tempFileCount;我将此行更改为$filecount=$tempFileCount;谢谢你的反馈,很抱歉,我没有机会测试这段代码。