Php 对文件夹中的图像进行计数并打印图像名称

Php 对文件夹中的图像进行计数并打印图像名称,php,Php,我做了一个计算文件夹中有多少图像的函数,我还想打印数组中的图像。这是一个用于计数的工作片段 <?php function countNum($dir){ $i = 0; $dir_array = scandir($dir); foreach($dir_array as $key=>$value){ if($value!=".." && $value!="."){ if(is_file($dir."/".$value)){ $i++;

我做了一个计算文件夹中有多少图像的函数,我还想打印数组中的图像。这是一个用于计数的工作片段

<?php
 function countNum($dir){
 $i = 0;
 $dir_array = scandir($dir);
  foreach($dir_array as $key=>$value){
  if($value!=".." && $value!="."){
   if(is_file($dir."/".$value)){

      $i++;
   }
  }
 }
 return array($i);
}

$num = countNum("images");
echo $num[0];
?>

如何将它们作为数组返回?

只需创建一个空数组,并将一个元素放入forach循环中的该数组

附加的代码段:

$array = array();
foreach ($files as $file) {
  $array[] = $file;
}

或者,您可以使用
array\u push()
函数来执行相同的作业。

只需创建一个空数组,并将一个元素放入forach循环中的该数组

附加的代码段:

$array = array();
foreach ($files as $file) {
  $array[] = $file;
}

或者,您可以使用
array\u push()
函数来执行相同的作业。

只需创建一个空数组,并将一个元素放入forach循环中的该数组

附加的代码段:

$array = array();
foreach ($files as $file) {
  $array[] = $file;
}

或者,您可以使用
array\u push()
函数来执行相同的作业。

只需创建一个空数组,并将一个元素放入forach循环中的该数组

附加的代码段:

$array = array();
foreach ($files as $file) {
  $array[] = $file;
}
或者,您可以使用
array\u push()
函数来执行相同的作业。

尝试以下操作:

function getFiles($dir){
     $files = array();
     $dir_array = scandir($dir);
     foreach($dir_array as $key=>$value) {
         if($value!=".." && $value!=".") {
             if(is_file($dir."/".$value)) {
                 $files[]=$value;
             }
          }
     }
     return $files;
}

$all_files = getFiles("images");
$num = count($all_files);
echo "Count: $num\n";
echo $all_files[0];
试试这个:

function getFiles($dir){
     $files = array();
     $dir_array = scandir($dir);
     foreach($dir_array as $key=>$value) {
         if($value!=".." && $value!=".") {
             if(is_file($dir."/".$value)) {
                 $files[]=$value;
             }
          }
     }
     return $files;
}

$all_files = getFiles("images");
$num = count($all_files);
echo "Count: $num\n";
echo $all_files[0];
试试这个:

function getFiles($dir){
     $files = array();
     $dir_array = scandir($dir);
     foreach($dir_array as $key=>$value) {
         if($value!=".." && $value!=".") {
             if(is_file($dir."/".$value)) {
                 $files[]=$value;
             }
          }
     }
     return $files;
}

$all_files = getFiles("images");
$num = count($all_files);
echo "Count: $num\n";
echo $all_files[0];
试试这个:

function getFiles($dir){
     $files = array();
     $dir_array = scandir($dir);
     foreach($dir_array as $key=>$value) {
         if($value!=".." && $value!=".") {
             if(is_file($dir."/".$value)) {
                 $files[]=$value;
             }
          }
     }
     return $files;
}

$all_files = getFiles("images");
$num = count($all_files);
echo "Count: $num\n";
echo $all_files[0];

如果您真的试图只拍摄图像文件,可能还有其他文件:

function getImages($dir) {
  //make a list of extensions
  $extensions = [
    'jpg',
    'png',
    'gif',
    //......other image extensions
  ];
  return array_filter(scandir($dir), function ($val) use ($extensions) {
    //get extension from file
    $ext = pathinfo("$dir/$val")['extension'];
    //only include file if extension matches list
    return in_array($ext, $extensions);
  });
}
$files = getImages("images");
//now show what's in there
print_r($files);
echo count($files);

如果您真的试图只拍摄图像文件,可能还有其他文件:

function getImages($dir) {
  //make a list of extensions
  $extensions = [
    'jpg',
    'png',
    'gif',
    //......other image extensions
  ];
  return array_filter(scandir($dir), function ($val) use ($extensions) {
    //get extension from file
    $ext = pathinfo("$dir/$val")['extension'];
    //only include file if extension matches list
    return in_array($ext, $extensions);
  });
}
$files = getImages("images");
//now show what's in there
print_r($files);
echo count($files);

如果您真的试图只拍摄图像文件,可能还有其他文件:

function getImages($dir) {
  //make a list of extensions
  $extensions = [
    'jpg',
    'png',
    'gif',
    //......other image extensions
  ];
  return array_filter(scandir($dir), function ($val) use ($extensions) {
    //get extension from file
    $ext = pathinfo("$dir/$val")['extension'];
    //only include file if extension matches list
    return in_array($ext, $extensions);
  });
}
$files = getImages("images");
//now show what's in there
print_r($files);
echo count($files);

如果您真的试图只拍摄图像文件,可能还有其他文件:

function getImages($dir) {
  //make a list of extensions
  $extensions = [
    'jpg',
    'png',
    'gif',
    //......other image extensions
  ];
  return array_filter(scandir($dir), function ($val) use ($extensions) {
    //get extension from file
    $ext = pathinfo("$dir/$val")['extension'];
    //only include file if extension matches list
    return in_array($ext, $extensions);
  });
}
$files = getImages("images");
//now show what's in there
print_r($files);
echo count($files);

是否保证文件夹中只包含.jpg文件?或者需要考虑其他文件的可能性吗?@ChicagoRedSox可能包含一些png格式的图像,但只有png和jpgOK,我的意思是你只是直接计算文件。比如说,是否存在需要忽略的文本文件?是否保证文件夹中只包含.jpg文件?或者需要考虑其他文件的可能性吗?@ChicagoRedSox可能包含一些png格式的图像,但只有png和jpgOK,我的意思是你只是直接计算文件。比如说,是否存在需要忽略的文本文件?是否保证文件夹中只包含.jpg文件?或者需要考虑其他文件的可能性吗?@ChicagoRedSox可能包含一些png格式的图像,但只有png和jpgOK,我的意思是你只是直接计算文件。比如说,是否存在需要忽略的文本文件?是否保证文件夹中只包含.jpg文件?或者需要考虑其他文件的可能性吗?@ChicagoRedSox可能包含一些png格式的图像,但只有png和jpgOK,我的意思是你只是直接计算文件。比如说,是否存在需要忽略的文本文件?