rac*_*420 2 php directory count
当此文件夹中有数千条记录时,此计数为onyl返回1.
// TV Shows
$dir = 'G:/TV';
if ($handle = opendir($dir)) {
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
$check = mysql_query("SELECT * FROM tv_shows WHERE title = '$file'");
$checkcount = mysql_num_rows($check);
if ($checkcount == 0) {
mysql_query("INSERT INTO tv_shows (id, title) VALUES ('', '$file')");
}
}
echo count($dir)." Records Traversed!<br/>";
closedir($handle);
}
Run Code Online (Sandbox Code Playgroud)
表结构:id,title
文件夹结构:主文件夹中的子文件夹
G:\TV
G:\TV\24
G:\TV\Family Guy
Run Code Online (Sandbox Code Playgroud)
您无法count()在文件夹上使用.试试这个
// TV Shows
$dir = 'G:/TV';
$count = 0;
if ($handle = opendir($dir)) {
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
$count++;
$check = mysql_query("SELECT * FROM tv_shows WHERE title = '$file'");
$checkcount = mysql_num_rows($check);
if ($checkcount == 0) {
mysql_query("INSERT INTO tv_shows (id, title) VALUES ('', '$file')");
}
}
echo $count." Records Traversed!<br/>";
closedir($handle);
}
Run Code Online (Sandbox Code Playgroud)
如何返回插入的新记录数?
检查一下,刚刚移动 $count++
// TV Shows
$dir = 'G:/TV';
$count = 0;
if ($handle = opendir($dir)) {
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
$check = mysql_query("SELECT * FROM tv_shows WHERE title = '$file'");
$checkcount = mysql_num_rows($check);
if ($checkcount == 0) {
$count++;
mysql_query("INSERT INTO tv_shows (id, title) VALUES ('', '$file')");
}
}
echo $count." Records Traversed!<br/>";
closedir($handle);
}
Run Code Online (Sandbox Code Playgroud)