PHP readdir和排序

时间:2009-09-02 06:10:36

标签: php sorting preg-replace readdir

我正在做一个小画廊。我想从目录中读取文件名,并在删除一些前导数字和文件扩展名后打印下面的文件名。

我有两个版本的代码。

版本1不排序

$current_dir = "$DOCUMENT_ROOT"."/weddings2/";   
$dir = opendir($current_dir);        // Open the sucker

while ($file = readdir($dir))            // while loop
  {
$parts = explode(".", $file);                    // pull apart the name and dissect by period
if (is_array($parts) && count($parts) > 1) {    // does the dissected array have more than one part
    $extension = end($parts);        // set to we can see last file extension
    $bfile= substr($file, 2); //strips the first two characters
    $cfile= preg_replace(('/\d/'),' ',$bfile);//remove numbers
    $cfile= preg_replace(('/_/'),' ',$cfile); 
    $cfile= preg_replace(('/.jpg/'),' ',$cfile);
            if ($extension == "jpg" OR $extension == "JPG")    // is extension ext or EXT ?
    echo "<td><img src=\"weddings2/$file\"><br />$cfile</td>\n";

    }
}
closedir($dir);        // Close the directory after we are done

版本2排序,但我无法操作文件名

$current_dir = "$DOCUMENT_ROOT"."/weddings2/";    

$dir = opendir($current_dir);        // Open the sucker

$files = array();
while ($files[] = readdir($dir));
sort($files);
closedir($dir);

foreach ($files as $file)
      if ($file <> "." && $file <> ".." && !preg_match("/^hide/i",$file))


$table_cell .= "<td><img src='".'weddings2/'. rawurlencode($file) ."'><br />$cfile</td>\n";


echo $table_cell;

是的,我知道我很蠢。 Arghhh!

2 个答案:

答案 0 :(得分:5)

编辑:您的代码缺少大括号

你有

foreach (...)
      code
      code

它应该是

foreach (...) {
      code
      code
}

只需将代码放在$ parts和foreach循环之后的最后一个$ cfile之间,只需在循环中添加大括号,这样就可以放入更多代码。另请注意,如果两个代码段中的条件不同,则表示决定使用哪一个或是否将它们组合成一个条件。

$current_dir = "$DOCUMENT_ROOT"."/weddings2/";    

$dir = opendir($current_dir);        // Open the sucker

$files = array();
while ($files[] = readdir($dir));
sort($files);
closedir($dir);

foreach ($files as $file) {

      //MANIPULATE FILENAME HERE, YOU HAVE $file...

      if ($file <> "." && $file <> ".." && !preg_match("/^hide/i",$file))
      echo "<td><img src='".'weddings2/'. rawurlencode($file) ."'><br />$cfile</td>\n";
}

答案 1 :(得分:0)

由于评论部分没有足够的空间......

Vinko:我在这里编辑是为了让它变得更容易。你应该

 $current_dir = "$DOCUMENT_ROOT"."/weddings2/";    
 $dir = opendir($current_dir);        // Open the sucker
 $files = array();
 while ($files[] = readdir($dir));
 sort($files);
 closedir($dir);

 foreach ($files as $file)
 {

    $bfile= substr($file, 2); //strips the first two characters
    $cfile= preg_replace(('/\d/'),'&nbsp;',$bfile);
    $cfile= preg_replace(('/_/'),' ',$cfile);
    $cfile= preg_replace(('/.jpg/'),' ',$cfile);

    if ($file <> "." && $file <> ".." && !preg_match("/^hide/i",$file))

    // echo "<td><img src=\"weddings2/$file\"><br />$cfile</td>\n";
    //echo "<td><img src=\"weddings2/$file\"><br />$cfile</td>\n";

    $table_cell .= "<td><img src='".'weddings2/'. rawurlencode($file) ."'><br />$cfile</td>\n";
    //$table_cell .= "  <li><a href='" .'pdfs/'. rawurlencode($file) ."'>$file</a></li>\n";

    echo $table_cell;
}

而不是你试过的

我试过这个:

 $current_dir = "$DOCUMENT_ROOT"."/weddings2/";    
 $dir = opendir($current_dir);        // Open the sucker
 $files = array();
 while ($files[] = readdir($dir));
 sort($files);
 closedir($dir);

  foreach ($files as $file)


    $bfile= substr($file, 2); //strips the first two characters
    $cfile= preg_replace(('/\d/'),'&nbsp;',$bfile);
    $cfile= preg_replace(('/_/'),' ',$cfile);
    $cfile= preg_replace(('/.jpg/'),' ',$cfile);

      if ($file <> "." && $file <> ".." && !preg_match("/^hide/i",$file))

   // echo "<td><img src=\"weddings2/$file\"><br />$cfile</td>\n";
    //echo "<td><img src=\"weddings2/$file\"><br />$cfile</td>\n";

$table_cell .= "<td><img src='".'weddings2/'. rawurlencode($file) ."'><br />$cfile</td>\n";
//$table_cell .= "  <li><a href='" .'pdfs/'. rawurlencode($file) ."'>$file</a></li>\n";

echo $table_cell;

得到了这个

<pre>
<td><img src='weddings2/36%20And%20they%20lived%20happily%20ever%20after.jpg'><br /> And they lived happily ever after </td></pre>

而不是:

<pre>
<td><img src="weddings2/05Wedding_Chapel.jpg"><br />Wedding Chapel </td>
<td><img src="weddings2/06Bride_Flowers.jpg"><br />Bride Flowers </td>
<td><img src="weddings2/09%20Bridemaids%20on%20the%20lawn.jpg"><br /> Bridemaids on the lawn </td>
<td><img src='weddings2/36%20And%20they%20lived%20happily%20ever%20after.jpg'><br /> And they lived happily ever after </td>
</pre>