duanbin4847
2016-04-12 22:37
浏览 41
已采纳

带有键作为文件创建日期的PHP数组

So I've got a folder, path/to/folder, with folders within folders and files within folders, etc.---all within one folder path/to/folder. I want to store all the names of all the images (.gif, .png, .jpg, .jpeg, etc.) in this folder path/to/folder like so (my attempt):

$arr = glob("path/to/folder/*.jpg");//not sure how to specify or "|" here

I also need the dates associated with the images' metadata (date created) like so (see psuedocode below):

foreach($arr as $a){
    $date_of_a = function_getting_dates_of_a($a);//filectime?
    $newArr[$date_of_a]=function_getting_names_of_a($a);//names are filtered by a pattern
                      //^^^^^^^^^^^^^^^^^^^^^^^^^^^I've already created this.
}

This way I can have same name values with different keys, which would be dates, and so two same-name-and-same-date pairs (or tuples depending on how many duplicates) would condense to one unique name-date item. For example,

2016-01-01 => path/to/folder/.../name1.jpg//*duplicate name-date pair
2016-01-02 => path/to/folder/.../name2.jpg
2016-01-01 => path/to/folder/.../name1.jpg//*duplicate name-date pair
2016-01-02 => path/to/folder/.../name1.jpg

would reduce the array to

2016-01-01 => path/to/folder/.../name1.jpg//*only one now
2016-01-02 => path/to/folder/.../name2.jpg
2016-01-02 => path/to/folder/.../name1.jpg

Here, /.../, means "any number of folders within path/to/folder". The data I'm dealing with would imply that 2016-01-01 => path/to/folder/.../name1.jpg and 2016-01-02 => path/to/folder/.../name1.jpg are two identical objects made at different times. I want to, in the end, be able to count how many instances of each name there are using array_count_values.


ATTEMPT

<?php

    $myArray = glob("path/to/folder/*.jpg");
    foreach($myArray as $item){
        $date = filectime($item);//What if no date? Is that possible?
        if(preg_match('/PATTERN/',$item,$match)==1){
            $newArr[$date]=current($match);
        }
    }
    array_count_values($newArr);
    <!-- CODE TO GRAPH ITEMS BY FREQUENCY-->
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhan8001 2016-04-12 23:31
    已采纳

    The following code should create an array of date-time keys:

    <?php
    
    $myArray = glob("/*.jpg");
    $newArr = array();
    foreach($myArray as $item){
        $timestamp = filectime($item);
        if ($timestamp === false) {
            continue;
        }
        $date = new \DateTime();
        $date->setTimestamp($timestamp);
        $formattedDate = $date->format('m-d-Y');
        $newArr[$formattedDate]=$item;
    }
    array_count_values($newArr);
    echo "<pre>";
    print_r($newArr);
    //<!-- CODE TO GRAPH ITEMS BY FREQUENCY-->
    

    And this is what gets printed out when I have multiple JPG files created on the same day:

    Array
    (
        [04-12-2016] => sdfdsf.jpg
    )
    

    As for finding files recursively, you can try these functions, which I've used in my own personal projects:

    function findDirectories($rootPath) {
        $directories = array();
        foreach (glob($rootPath . "/*", GLOB_ONLYDIR) as $directory) {
            $directories[] = $directory;
        }
        return $directories;
    }
    
    function findFiles($rootPath, $extension) {
        $files = array();
        foreach (glob($rootPath . "/*.$extension") as $file) {
            $files[] = $file;
        }
        return $files;
    }
    
    function findFilesRecursive($rootPath,$extension) {
        $files = findFiles($rootPath,$extension);
        $directories = findDirectories($rootPath);
        if (!empty($directories)) {
            foreach ($directories as $key=>$directory) {
                $foundFiles = findFilesRecursive($directory,$extension);
                foreach ($foundFiles as $foundFile) {
                    $files[] = $foundFile;
                }
            }
        }
        return $files;
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题