44

我正在使用 WordPress。我有一个像mytheme/images/myimages.

我想从文件夹中检索所有图像名称myimages

请告诉我,我怎样才能获得图像名称。

4

11 回答 11

105

尝试这个

$directory = "mytheme/images/myimages";
$images = glob($directory . "/*.jpg");

foreach($images as $image)
{
  echo $image;
}
于 2013-06-15T09:47:06.443 回答
21

你可以简单地使用 PHPopendir函数来做到这一点。

例子:

$handle = opendir(dirname(realpath(__FILE__)).'/pictures/');
while($file = readdir($handle)){
  if($file !== '.' && $file !== '..'){
    echo '<img src="pictures/'.$file.'" border="0" />';
  }
}
于 2013-06-15T09:47:13.087 回答
12

当您想从文件夹中获取所有图像时,请使用glob()有助于获取所有图像的内置功能。但是,当您得到所有​​内容时,有时需要检查所有内容是否有效,因此在这种情况下,此代码可以帮助您。此代码还将检查它是否是图像

  $all_files = glob("mytheme/images/myimages/*.*");
  for ($i=0; $i<count($all_files); $i++)
    {
      $image_name = $all_files[$i];
      $supported_format = array('gif','jpg','jpeg','png');
      $ext = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
      if (in_array($ext, $supported_format))
          {
            echo '<img src="'.$image_name .'" alt="'.$image_name.'" />'."<br /><br />";
          } else {
              continue;
          }
    }

了解更多信息

PHP 手册

于 2017-04-06T10:06:39.667 回答
4
$dir = "mytheme/images/myimages";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;
}
$images=preg_grep ('/\.jpg$/i', $files);

非常快,因为您只扫描所需的目录。

于 2013-06-15T09:50:27.997 回答
4

这个答案是针对 WordPress 的

$base_dir = trailingslashit( get_stylesheet_directory() );
$base_url = trailingslashit( get_stylesheet_directory_uri() );

$media_dir = $base_dir . 'yourfolder/images/';
$media_url = $hase_url . 'yourfolder/images/';

$image_paths = glob( $media_dir . '*.jpg' );
$image_names = array();
$image_urls = array();

foreach ( $image_paths as $image ) {
    $image_names[] = str_replace( $media_dir, '', $image );
    $image_urls[] = str_replace( $media_dir, $media_url, $image );
}

// --- You now have:

// $image_paths ... list of absolute file paths 
// e.g. /path/to/wordpress/wp-content/uploads/yourfolder/images/sample.jpg

// $image_urls ... list of absolute file URLs 
// e.g. http://example.com/wp-content/uploads/yourfolder/images/sample.jpg

// $image_names ... list of filenames only
// e.g. sample.jpg

以下是一些其他设置,它们将为您提供来自子主题以外的其他地方的图像。只需将上面代码中的前 2 行替换为您需要的版本即可:

从上传目录:

// e.g. /path/to/wordpress/wp-content/uploads/yourfolder/images/sample.jpg
$upload_path = wp_upload_dir();
$base_dir = trailingslashit( $upload_path['basedir'] );
$base_url = trailingslashit( $upload_path['baseurl'] );

从父主题

// e.g. /path/to/wordpress/wp-content/themes/parent-theme/yourfolder/images/sample.jpg
$base_dir = trailingslashit( get_template_directory() );
$base_url = trailingslashit( get_template_directory_uri() );

从儿童主题

// e.g. /path/to/wordpress/wp-content/themes/child-theme/yourfolder/images/sample.jpg
$base_dir = trailingslashit( get_stylesheet_directory() );
$base_url = trailingslashit( get_stylesheet_directory_uri() );
于 2016-08-18T19:44:44.660 回答
3
//path to the directory to search/scan
        $directory = "";
         //echo "$directory"
        //get all files in a directory. If any specific extension needed just have to put the .extension
        //$local = glob($directory . "*"); 
        $local = glob("" . $directory . "{*.jpg,*.gif,*.png}", GLOB_BRACE);
        //print each file name
        echo "<ul>";

        foreach($local as $item)
        {
        echo '<li><a href="'.$item.'">'.$item.'</a></li>';
        }

        echo "</ul>";
于 2018-03-08T16:36:01.443 回答
3

这是我的一些代码

$dir          = '/Images';
$ImagesA = Get_ImagesToFolder($dir);
print_r($ImagesA);

function Get_ImagesToFolder($dir){
    $ImagesArray = [];
    $file_display = [ 'jpg', 'jpeg', 'png', 'gif' ];

    if (file_exists($dir) == false) {
        return ["Directory \'', $dir, '\' not found!"];
    } 
    else {
        $dir_contents = scandir($dir);
        foreach ($dir_contents as $file) {
            $file_type = pathinfo($file, PATHINFO_EXTENSION);
            if (in_array($file_type, $file_display) == true) {
                $ImagesArray[] = $file;
            }
        }
        return $ImagesArray;
    }
}
于 2016-08-03T12:22:33.663 回答
2

检查是否存在,将所有文件放入数组中,preg grep 所有 JPG 文件,回显新数组对于所有图像可以试试这个:

$images=preg_grep('/\.(jpg|jpeg|png|gif)(?:[\?\#].*)?$/i', $files);


if ($handle = opendir('/path/to/folder')) {

    while (false !== ($entry = readdir($handle))) {
        $files[] = $entry;
    }
    $images=preg_grep('/\.jpg$/i', $files);

    foreach($images as $image)
    {
    echo $image;
    }
    closedir($handle);
}
于 2015-11-03T17:58:05.753 回答
1
    <?php
   $galleryDir = 'gallery/';
   foreach(glob("$galleryDir{*.jpg,*.gif,*.png,*.tif,*.jpeg}", GLOB_BRACE) as $photo)
   {echo "<a  href=\"$photo\">\n" ;echo "<img style=\"padding:7px\" class=\"uk-card uk-card-default uk-card-hover uk-card-body\" src=\"$photo\">"; echo "</a>";}?>

UIkit php 文件夹库 https: //webshel​​f.eu/en/php-folder-gallery/

于 2021-11-10T17:54:57.240 回答
0
get all the images from a folder in php without database


$url='https://demo.com/Images/sliderimages/';
       $dir = "Images/sliderimages/";
        $file_display = array(
            'jpg',
            'jpeg',
            'png',
            'gif'
        );
        
        $data=array();
        
        if (file_exists($dir) == false) {
            $rss[]=array('imagePathName' =>"Directory  '$dir'  not found!");
            $msg=array('error'=>1,'images'=>$rss);
             echo json_encode($msg);
        } else {
            $dir_contents = scandir($dir);
        
            foreach ($dir_contents as $file) {
                @$file_type = strtolower(end(explode('.', $file)));
                // $file_type1 = pathinfo($file);
                // $file_type= $file_type1['extension'];
                
                if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) {
                   $data[]=array('imageName'=>$url.$file);
               
                   
                }
            }
            if(!empty($data)){
                $msg=array('error'=>0,'images'=>$data);
                echo json_encode($msg);
            }else{
                $rees[]=array('imagePathName' => 'No Image Found!');
                $msg=array('error'=>2,'images'=>$rees);
                echo json_encode($msg);
            }
        }
于 2021-09-04T10:45:38.010 回答
-3

您可以简单地显示您的实际图像目录(不太安全)。只需 2 行代码。

 $dir = base_url()."photos/";

echo"<a href=".$dir.">Photo Directory</a>";
于 2017-10-20T05:16:05.773 回答