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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致