我有一段主要工作的代码来读取目录,输出每个文件夹的名称,并计算该目录中的文件和文件的总大小。但由于某种原因,它跳过了一些文件夹,没有将信息插入我的数据库。
我的代码:
<?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.
当您将条目添加到DB:时
$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();
}