0

まず、あいまいな質問のタイトルについてお詫び申し上げます。意味のあるタイトルが思いつきませんでした。

次を使用して、ディレクトリ内の画像ファイルをループしています。

$folder = 'frames/*';
foreach(glob($folder) as $file)
{

}

要件

各ファイルのサイズを測定したいのですが、サイズが より小さい場合は8kb、次のファイルに移動してサイズを確認し、サイズが より大きいファイルが得られるまでそうします8kb。今、私は使用しています

$size = filesize($file);
if($size<8192) // less than 8kb file
{
    // this is where I need to keep moving until I find the first file that is greater than 8kb
   // then perform some actions with that file
}
// continue looping again to find another instance of file less than 8kb

私は見ましたが、私が探しnext()current()いた解決策を思いつくことができませんでした。

したがって、次のような結果になります。

File 1=> 12kb
File 2=> 15kb
File 3=> 7kb // <-- Found a less than 8kb file, check next one
File 4=> 7kb // <-- Again a less than 8kb, check next one
File 5=> 7kb // <-- Damn! a less than 8kb again..move to next
File 6=> 13kb // <-- Aha! capture this file
File 7=> 12kb
File 8=> 14kb
File 9=> 7kb
File 10=> 7kb
File 11=> 17kb // <-- capture this file again
.
.
and so on

アップデート

私が使用している完全なコード

$folder = 'frames/*';
$prev = false;
foreach(glob($folder) as $file)
{
    $size = filesize($file);    
    if($size<=8192)
    {
       $prev = true;
    }

    if($size=>8192 && $prev == true)
    {
       $prev = false;
       echo $file.'<br />'; // wrong files being printed out    
    }
}
4

1 に答える 1

2

あなたがする必要があるのは、以前に分析されたファイルが小さいものか大きいものかを示す変数を保持し、それに応じて反応することです。

このようなもの :

$folder = 'frames/*';
$prevSmall = false; // use this to check if previous file was small
foreach(glob($folder) as $file)
{
    $size = filesize($file);
    if ($size <= 8192) {
        $prevSmall = true; // remember that this one was small
    }

    // if file is big enough AND previous was a small one we do something
    if($size>8192 && true == $prevSmall)
    {
        $prevSmall = false; // we handle a big one, we reset the variable
        // Do something with this file
    }
}
于 2013-02-12T14:04:56.423 に答える