PHP readdir 和排序

发布于 2024-08-03 04:08:20 字数 1600 浏览 11 评论 0原文

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

我有两个版本的代码。

版本 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;

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

I'm making a little gallery. I want to read the file names off a directory and print the file names below after I've stripped some leading numerals and file extensions.

I have two versions of the code.

Version 1 doesn't sort

$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

Version 2 sorts but I can't manipulate the file names

$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;

Yes, I know I'm dumb. Arghhh!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有一腔孤勇 2024-08-10 04:08:20

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

你有

foreach (...)
      code
      code

并且应该是

foreach (...) {
      code
      code
}

只需将代码放在 $parts 和 foreach 循环后的最后一个 $cfile 之间,只需在循环中添加大括号,以便您可以放置​​更多代码另请注意,两个代码片段中都有不同的 if 条件,您必须决定使用哪一个或 if 将它们组合成一个条件。

$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";
}

EDIT: Your code is missing braces

You have

foreach (...)
      code
      code

and it should be

foreach (...) {
      code
      code
}

Just put the code between $parts and the last $cfile after the foreach loop, just add the braces in the loop so you can put more code in. Also note that you have different if conditions in both code snippets, you have to decide which one use or if to combine them both into a single condition.

$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";
}
空袭的梦i 2024-08-10 04:08:20

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

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/'),' ',$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/'),' ',$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>

Since the comment section doesn't have enough room...

Vinko: I'm editing here to make it easier. You should have

 $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/'),' ',$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;
}

instead of what you tried

I tried this:

 $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/'),' ',$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;

And got this

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

Instead of this:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文