0

我想选择文件格式目录,其中 filezise 小于 100kb。请检查我的代码它不起作用

<?php
ob_start();
$dir = '/home/couponsc/public_html/testfile';
$ext = '.mp3';
$search = $_GET['s'];
$results = glob("$dir/*$search*$ext");

    foreach($results as $item) 
    {
        $sizes= filesize($item);
        if($sizes < 100);
        {               
            echo $item;             
        }

    }   



?>
4

2 回答 2

1

if($sizes < 100); 在 if-clause -> empty 语句之后有一个分号。

foreach($results as $item) {
    $sizes= filesize($item);
    if($sizes < 40*1024) {
        echo $item, "\n";
    }
} 

或者多一点spl / lambda fun + recursion:

<?php
$path = 'c:/temp';
$it = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($path),
    RecursiveIteratorIterator::LEAVES_ONLY
);

$it = new FooFilterIterator($it, function($e) { return $e->getSize(); }, function($e) { return $e < 40*1024; } );

foreach($it as $f) {
    printf("% 6d %s\n", $f->getSize(), $f);
}

class FooFilterIterator extends FilterIterator {
    protected $getter;
    protected $filter;

    public function __construct(Iterator $source, $getter, $filter) {
        parent::__construct($source);
        $this->getter = $getter;
        $this->filter = $filter;
    }

    public function accept() {
        $f = $this->filter;
        $g = $this->getter;
        return $f( $g($this->current()) );
    }
}

还有GlobIterator

于 2012-09-13T14:29:15.153 回答
0

filesize()函数返回字节,而不是千字节,因此您的条件不起作用。

正确的条件是:

if($sizes < 102400);
于 2012-09-13T14:34:07.793 回答