PHP计算特定条件下的FTP文件

问题描述 投票:0回答:1

我似乎在任何地方都找不到答案。我正在尝试计算满足某些条件的FTP文件夹中的文件数。我正在使用以下内容:

/*
 * Filename examples: 
 * Store Evaluation_10950_2019-12-03_6980.pdf
 * Store Survey_13532_2019-11-29.pdf
 */

$file_list = ftp_nlist($ftp_connection, "."); 
$currentDate = date('Y-m');

$countFiles = count($file_list);

// Title of Completed Evaluatons
echo '<strong>'.$countFiles.' Completed Evaluations:</strong><br><br>'; 

foreach($file_list as $file) {
//Only get Current Month PDF files
    if ((strpos($file, '.pdf') !== false) && (strpos($file, 'Store Evaluation') !== false) && (strpos($file, $currentDate) !== false)) {
        // Remove Store Evaluation_ from string
        $strippedEvals1 = ltrim($eval, 'Store Evaluation_');
        // Remove store number from string
        $strippedEvals2 = strstr($strippedEvals1, '_');
        // Remove _ from string
        $strippedEvals3 = str_replace('_','',$strippedEvals2);
        // Remove everything after the date in string
        $strippedEvalDate = substr_replace($strippedEvals3 ,"", -8); 
        // Get just the store number
        $strippedEvalStoreNum = substr($strippedEvals1, 0, strpos($strippedEvals1, "_")); 
        // Print store number and date
        echo "<strong>".$strippedEvalStoreNum."</strong> (".$strippedEvalDate.")<br>";
    }
}

我能够根据我指定的条件列出所有文件;但是,现在我想对它们进行计数,并说出顶部有多少个。上面的代码显然输出该文件夹中所有文件的数量,而没有条件。

我已经尝试$countFiles = count(strpos($file_list, '.pdf'));只是为了测试一个条件,但是什么也不会产生。正确的解决方法是什么?

php if-statement count ftp conditional-statements
1个回答
0
投票

而不是在循环时回显条件,您可以将其存储在数组中,然后在count()数组上的$output末尾将其输出:

$output = array(); //create an empty array to store our output

foreach($file_list as $file) {
//Only get Current Month PDF files
    if ((strpos($file, '.pdf') !== false) && (strpos($file, 'Store Evaluation') !== false) && (strpos($file, $currentDate) !== false)) {
        // Remove Store Evaluation_ from string
        $strippedEvals1 = ltrim($eval, 'Store Evaluation_');
        // Remove store number from string
        $strippedEvals2 = strstr($strippedEvals1, '_');
        // Remove _ from string
        $strippedEvals3 = str_replace('_','',$strippedEvals2);
        // Remove everything after the date in string
        $strippedEvalDate = substr_replace($strippedEvals3 ,"", -8); 
        // Get just the store number
        $strippedEvalStoreNum = substr($strippedEvals1, 0, strpos($strippedEvals1, "_")); 
        // Print store number and date
        $output[] =  "<strong>".$strippedEvalStoreNum."</strong> (".$strippedEvalDate.")"; //add to array instead of echo
    }
}

echo '<strong>'.count($file_list).' Completed Evaluations</strong><br><br>'; 

echo '<strong>'.count($output).' Evaluations Met My Conditions:</strong><br><br>'; //echo the number of files that met the conditions

echo implode('<br/>', $output); //echo the converted file names
© www.soinside.com 2019 - 2024. All rights reserved.