简体   繁体   English

计算从一个月内用php创建的文件

[英]count files created from a month in php

I managed to count files in a folder with php. 我设法用php计数了文件夹中的文件。

count(scandir($snap_folder))

But now I would need to count only files created in the last 30 days and return the number. 但是现在我只需要计算最近30天创建的文件并返回数字。

this code returns always 0 even if I have files in the folder ? 即使文件夹中有文件,此代码也始终返回0?

 <?php
$snap_user = $_POST['snap_user'];
if (!file_exists($snap_user)){mkdir($snap_user, 0755, true);};
$snap_folder = $snap_user . '/';

$files = scandir($snap_folder);

$limit = date('Y-m-d', strtotime('-30 days'));
$count = 0;
foreach($files as $file) {
    if (date ("Y-m-d", filemtime($file)) >= $limit && !in_array($file, array('.', '..'))) {
        $count++;
    }
}

echo $count
?>

Is it possible? 可能吗?

The code should look something like that 该代码应该看起来像这样

$files = scandir('folder/');

$limit = date('Y-m-d', strtotime('-30 days'));
$count = 0;
foreach($files as $file) {
  if (date ("Y-m-d", filemtime($file)) >= $limit) {
    $count++;
  }
}

EDIT: 编辑:

$files = scandir('folder/');      
$limit = date('Y-m-d', strtotime('-30 days'));
$count = 0;
foreach($files as $file) {
    if (date ("Y-m-d", filemtime($file)) >= $limit && !in_array($file, array('.', '..'))) {
        $count++;
    }
}

EDIT 2: 编辑2:

$folder = './folder/';
$files = scandir($folder);      
$limit = date('Y-m-d', strtotime('-30 days'));
$count = 0;
foreach($files as $file) {
    $filename = $folder . $file;
    if (date ("Y-m-d", filemtime($filename)) >= $limit && !in_array($file, array('.', '..'))) {
        $count++;
    }
}
echo $count;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM