当此文件夹中有数千条记录时,此计数仅返回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);
}
表格结构:id,title
文件夹结构:主文件夹中的子文件夹
G:\TV
G:\TV\24
G:\TV\Family Guy
答案 0 :(得分:5)
您无法在文件夹上使用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);
}
如何返回插入的新记录数?
检查一下,刚刚移动$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);
}
答案 1 :(得分:1)
您在字符串上调用count()
,而不是数组。
答案 2 :(得分:0)
另外一个很好的替代方法是使用php glob函数。
$dir_path = "/full/directory/path/";
$count = count(glob($dir_path . "*"));
这也将优化您的代码!!