Reputation: 1567
I have a mostly-working piece of code to read a directory, output each folder name and count the files and the total size of the files in that directory. But for some reason it is skipping over some folders and not inserting the information into my database.
My Code:
<?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>";
}
}
}
}
?>
It skips over simple folder name like Beyond The Wall of Sleep
and does not add them to the database although it does echo the output as if it did input it into the database. Where am I going wrong?
Output Example (Folders not added but they still get listed):
•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.
Upvotes: 0
Views: 733
Reputation: 69
When you add the entry to the 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>";
Then get the string you are trying to input into MySQL, go to phpmyadmin or to terminal mysql, and try to insert that query. You will get mysql error.
Another way to debug it would be:
$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();
}
Upvotes: 1