PHP目录读取,计数&文件大小

时间:2012-01-07 15:14:57

标签: php count directory filesize

我有一段大部分代码来读取目录,输出每个文件夹名称并计算文件以及该目录中文件的总大小。但由于某种原因,它正在跳过某些文件夹,而不是将信息插入我的数据库。

我的代码:

<?php
if ($handle = opendir('E:/')) {

    while (false !== ($entry = readdir($handle))) {
        if ($entry != '$RECYCLE.BIN' && $entry != 'System Volume Information') {
        $exists = mysql_query("SELECT * FROM comics WHERE title LIKE '".$entry."%'");
        $exists1 = mysql_num_rows($exists);
            if ($exists1 != 1) {
            $directory = "E:/".$entry;
            $filecount = count(glob($directory."/"."*.*"));
            $size = 0; 
            $d = new RecursiveIteratorIterator( 
            new RecursiveDirectoryIterator($directory),  
            RecursiveIteratorIterator::SELF_FIRST 
            ); 

            foreach($d as $file){ 
            $size += $file->getSize(); 
            } 

                mysql_query("INSERT INTO comics (title, pages, size, storage_id) VALUES ('".$entry."\n', '".$filecount."', '".number_format($size/1048576, 2)."', '2')");
                print "<li>Inserted <b>".$entry."</b> With A File Count Of <b>".$filecount."</b> & A Total Size Of <b>".number_format($size/1048576, 2)."</b> MB.</li>";
            }
        }
    }
}
?>

它跳过简单的文件夹名称,如Beyond The Wall of Sleep,并且不会将它们添加到数据库,尽管它确实回显输出,就好像它确实输入到数据库中一样。我哪里错了?

输出示例(未添加文件夹但仍会列出):

•Inserted Ay Papi 1 With A File Count Of 22 & A Total Size Of 2.59 MB.
•Inserted Beyond The Wall of Sleep With A File Count Of 26 & A Total Size Of 14.92 MB.
•Inserted Fall of Cthulhu With A File Count Of 22 & A Total Size Of 7.57 MB.
•Inserted Heavy Metal - Fantasy Special With A File Count Of 98 & A Total Size Of 49.66 MB.

1 个答案:

答案 0 :(得分:1)

将条目添加到数据库时:

$sql = "INSERT INTO comics
        (title, pages, size, storage_id)
        VALUES ('".$entry."\n', '".$filecount."', '".number_format($size/1048576, 2)."', '2')";
mysql_query($sql);
print "<li>$sql</li>";

然后获取您要输入MySQL的字符串,转到phpmyadmin或终端mysql,并尝试插入该查询。你会得到mysql错误。

调试它的另一种方法是:

$sql = "INSERT INTO comics
        (title, pages, size, storage_id)
        VALUES ('".$entry."\n', '".$filecount."', '".number_format($size/1048576, 2)."', '2')";
$res = mysql_query($sql);
print "<li>$sql</li>";
if (!$res)
{
  echo "Mysql Error: " . mysql_error();
}