【问题标题】:Trying to sort out date based folder names in PHP尝试在 PHP 中整理基于日期的文件夹名称
【发布时间】:2014-08-30 09:12:55
【问题描述】:

我有这样的文件夹结构

Folder/
  09_06_2014/
    Main Body/
     page001.JPG 

Folder/
  03_06_2014/
    Main Body/
     page001.JPG

Folder/
  09_07_2014/
    Main Body/
     page001.JPG

我想要做的是从最近的文件夹 (09_07_2014) 中选择图片 page001.JPG,以便有一个带有后续条目的下拉菜单。

我的代码块看起来像这样。

$dp = opendir('Folder/.');
while ($file = readdir ($dp)) {
  if ($file != '.' && $file != '..') {
     echo "<pre>";
     echo "<a href='";
     echo "$file/page001.JPG'>$file</a>\n";
     echo "</pre>";
  }
}
closedir($dp);

有人建议我将文件夹名称加载到 3 维数组中,但这超出了我的知识深度 :( 任何建议都会很棒

【问题讨论】:

  • 您应该将您的文件夹命名为 yyyy_mm_dd,这样排序就很容易了。改变是否为时已晚?
  • 他们是从我们无权更改的脚本中以这种方式运行的。所以一成不变是准确的:)

标签: php sorting date directory readdir


【解决方案1】:

我已经为你实现了一些代码。我写得尽可能简单(至少我希望我做到了;-))。没有 oop - 只是程序代码。
最简单的方法是重命名文件夹,如前所述。它不是高性能的 - 而是用于学习目的。

希望对你有所帮助!

<?php
//
// Setup.
//
date_default_timezone_set('Europe/Vienna');
$rootDirectory = 'Folder/'; // Needs trailing slash!
$fileToInclude = 'Main Body/page001.JPG';

//
// Execute.
//
if(!is_dir($rootDirectory)) {
    die('Directory "'. $rootDirectory .'" does not exist');
}

// Find directories in $rootDirectory.
$directories = array();
if($fh = opendir($rootDirectory)) {
    while(false !== ($file = readdir($fh))) {
        if($file != '.' && $file != '..') {
            $absoluteFilePath = $rootDirectory . $file;
            if(is_dir($absoluteFilePath)
              && strlen($file) == 10
              && substr_count($file, '_') == 2) {
                $directories[] = $file;
            }
        }
    }
    closedir($fh);
}

// Check if directories are found.
if(count($directories) == 0) {
    die('No directories found');
}

// Sort directories.
usort($directories, function($a, $b) {
    // Transform directory name into UNIX timestamps.
    $aTime = mktime(0, 0, 0, substr($a, 3, 2), substr($a, 0, 2), substr($a, -4));
    $bTime = mktime(0, 0, 0, substr($b, 3, 2), substr($b, 0, 2), substr($b, -4));

    // Compare.
    if($bTime > $aTime) {
        return 1;
    } elseif($bTime < $aTime) {
        return -1;
    } else {
        return 0;
    }
});

// Loop through directories in that order.
foreach($directories as $directory) {
    // Determine path to directory.
    $absoluteDirectoryPath = $rootDirectory . $directory;
    if(substr($directory, -1) != DIRECTORY_SEPARATOR) {
        $absoluteDirectoryPath .= DIRECTORY_SEPARATOR;
    }

    // Find $fileToInclude in $absoluteDirectoryPath.
    $filePath = $absoluteDirectoryPath . $fileToInclude;
    if(file_exists($filePath)) {
        echo('<a href="'. $filePath . '">' . $filePath . '</a><br />' . "\n");
    }
}
?>

【讨论】:

    【解决方案2】:

    将日期文件夹重命名为 YYYYMMDD 格式的最简单方法。这样,就必须直接按降序对字符串进行排序。

    但是,如果您无法重命名文件夹,请将它们放入数组中并编写自定义排序函数并使用 usort

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多