PHP GLOB跳过每个文件夹中的第一个图像

时间:2016-10-19 13:46:08

标签: php glob

我试图理解为什么GLOB会在我指导的任何文件夹中跳过第一张图片。我通过放置一个名为的虚拟图像来解决它,因此它首先列出,但我想以更有效的方式解决这个问题。这是我的代码。

   <?php

    // This retrieves images from a selected folder - anchor with a data-xx-xx    attribute
    // via jQuery - click - the data-xx-x value is put into a hidden field :   foldpath

    // It all works fine BUT, it always fails to return the first image in a folder
    // I get around it by placing within each folder a dummy image called aa.jpg 
    // which hopefully will always be the first image but it is not really a 
    // satisfactory solution.

if($_POST && isset($_POST["foldsub"]) && isset($_POST["foldpath"]) ){
    // hidden field $_POST["foldpath"];
    //A typical path might be :

    // ../images/products/CLIENTS_images/bridal_whatnots/bridal_belts
    //the ../ prefix is reoved via php when the page is displayed and replaced by DIR
    // for testing between admin folders and the main root folders I use the relative paths.

    $fp = $_POST["foldpath"]; 
    $files = glob($fp ."/" ."*.*");
    //this is a pop up page which needs to stay open after the selection has been made
    echo "<script> $(\"#imgstuff\").css({\"display\":\"block\"});</script>";
    //the form closed on whgen the close x is selected
    }else
    {
        //default
   $files = glob("../images/bannerImgs/*.*");      
    }    

  for ($i=0; $i<count($files); $i++)    
{

$image = $files[$i];

$supported_file = array(
      'jpg',
      'jpeg',
      'png'
);

$ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
if (in_array($ext, $supported_file)) {
   // print $image ."<br />";
$path = $image;
$path1 = DIR;
if (strpos($path, $path1) !== false) {
    $path2 = $path;
}else{
$path2 = str_replace('../', '', $path);
$path2 = $path1.$path2;
}

$fn = basename($path);

$fn = basename($path, PATHINFO_EXTENSION);
if($fn!="aa.jpg"){
 echo '<p class="imgdet">';
// echo '<span class="imglongpath">' . $path2 .'</span>';
    echo '<img src="'.$image .'" alt="Random image" />';
    echo '<span class="imgname">' . $fn .'</span>';
  // echo '<span class="imgmesg">Now click Confirm / Set Choice</span>';
   // "<br /><br />";
echo "</p>";
}
} else {
    continue;
 }    
}    
?>

0 个答案:

没有答案