可以优化此循环浏览文件夹中照片的代码吗?

发布于 2024-07-30 07:36:49 字数 661 浏览 3 评论 0原文

我已经编写了这段代码,现在已经 2 年没有使用 PHP 来循环遍历照片文件夹并按字母顺序将它们写入页面。 这是一个相当简单的请求,但我花了 15 分钟的时间来写。

if ($handle = opendir('photos')) {
  $count = 0;
  $list[] = array();
  while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..") {
      $list[$count] = $file;
      $count ++;
    }
  }
  closedir($handle);
  asort($list);
  $sorted_list = array();
  $sorted_list = array_values($list);
  foreach ($sorted_list as $i => $value) {
    echo "<li><img src=\"photos/$sorted_list[$i]\" alt=\"$sorted_list[$i]\" title=\"\"></li>\n";
  }
}

我是不是完全写错了? 有什么方法可以改进代码吗? 很高兴收到任何建设性的反馈。

I have written this code having not used PHP for 2 years now to loop through a folder of photos and write them to the page in alphabetical order. It is a fairly simple request but it took me the best part of 15 minutes to write.

if ($handle = opendir('photos')) {
  $count = 0;
  $list[] = array();
  while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..") {
      $list[$count] = $file;
      $count ++;
    }
  }
  closedir($handle);
  asort($list);
  $sorted_list = array();
  $sorted_list = array_values($list);
  foreach ($sorted_list as $i => $value) {
    echo "<li><img src=\"photos/$sorted_list[$i]\" alt=\"$sorted_list[$i]\" title=\"\"></li>\n";
  }
}

Have I written it totally the wrong way? Are there ways I can improve the code? Any constructive feedback gladly received.

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

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

发布评论

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

评论(5

白龙吟 2024-08-06 07:36:49

您可以利用 scandir() 函数,该函数将处理读取目录并对结果进行排序。

$files = scandir('photos');
if ($files !== false) 
{
    foreach($files as $f) {
        if ($f == '..' || $f == '.') continue;      
        echo '<li><img src="photos/'.$f.'" alt="'.$f.'" title=""></li>'."\n";
    }
}

为了便于阅读,我对其进行了一些编辑。

You could take advantage of the scandir() function, which will handle reading the directory as well as sorting the results.

$files = scandir('photos');
if ($files !== false) 
{
    foreach($files as $f) {
        if ($f == '..' || $f == '.') continue;      
        echo '<li><img src="photos/'.$f.'" alt="'.$f.'" title=""></li>'."\n";
    }
}

I edited it a bit for readability.

满意归宿 2024-08-06 07:36:49

试试这个:

$photos = glob('photos/*');
foreach($photos as $photo) {
    echo "<li><img src=\"{$photo}" alt=\"{$photo}\" title=\"\"></li>\n";

}

http://us.php.net/manual/en/function .glob.php

Try this:

$photos = glob('photos/*');
foreach($photos as $photo) {
    echo "<li><img src=\"{$photo}" alt=\"{$photo}\" title=\"\"></li>\n";

}

http://us.php.net/manual/en/function.glob.php

|煩躁 2024-08-06 07:36:49

您不需要 $count。 这将为您提供相同的结果

$list[] = array();
while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..") {
        $list[] = $file;
    }
}

将排序和显示替换为:

sort($list);
for ($i = 0; $i < count($list); $i++) {
    echo "<li><img src=\"photos/{$list[$i]}\" alt=\"{$list[$i]}\" title=\"\"></li>\n";
}

You don't need the $count. This will give you the same result

$list[] = array();
while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..") {
        $list[] = $file;
    }
}

Replace sorting and displaying with just:

sort($list);
for ($i = 0; $i < count($list); $i++) {
    echo "<li><img src=\"photos/{$list[$i]}\" alt=\"{$list[$i]}\" title=\"\"></li>\n";
}
莫多说 2024-08-06 07:36:49

您可以替换

foreach ($sorted_list as $i => $value) {
    echo "<li><img src=\"photos/$sorted_list[$i]\" alt=\"$sorted_list[$i]\" title=\"\"></li>\n";
  }

foreach ($sorted_list as $value) {
    echo "<li><img src=\"photos/$value\" alt=\"$value\" title=\"\"></li>\n";
}

然后您不需要调用 array_values(),因为数组键不按数字顺序并不重要。

You can replace

foreach ($sorted_list as $i => $value) {
    echo "<li><img src=\"photos/$sorted_list[$i]\" alt=\"$sorted_list[$i]\" title=\"\"></li>\n";
  }

with

foreach ($sorted_list as $value) {
    echo "<li><img src=\"photos/$value\" alt=\"$value\" title=\"\"></li>\n";
}

Then you don't need to call array_values(), because it doesn't matter that the array keys aren't in numeric order.

洛阳烟雨空心柳 2024-08-06 07:36:49

更简单的方法是使用 scandir 函数:

$dir = 'photos';
$files = array_diff( scandir($dir), array(".", "..") );
foreach ($files as $i => $value) {
    echo "<li><img src=\"photos/$value\" alt=\"$value\" title=\"\"></li>\n";
}

祝你好运!

A simplier way is using the scandir function:

$dir = 'photos';
$files = array_diff( scandir($dir), array(".", "..") );
foreach ($files as $i => $value) {
    echo "<li><img src=\"photos/$value\" alt=\"$value\" title=\"\"></li>\n";
}

good luck!

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