我目前正在开发一个基于网络的图书馆项目。目前,代码执行它需要执行的操作,除非遇到目录,而不是再次运行该函数,而是什么也不执行。
function insertToTable($dire){
include "./modular/filesLogic.php";
$scanf = scandir($dire);
unset($scanf[array_search('.', $scanf, true)]);
unset($scanf[array_search('..', $scanf, true)]);
for($s = 2; $s<count($scanf); $s++){
$iextension = pathinfo($scanf[$s], PATHINFO_EXTENSION);
$inamewoext = pathinfo($scanf[$s], PATHINFO_FILENAME);
$datec = date("Y-m-d");
$sze = filesize($dire."/".$scanf[$s]);
$seql = "SELECT * FROM files WHERE name='$inamewoext'";
$resi = mysqli_query($conn, $seql);
if(is_dir($scanf[$s])){
$iextension = "dir";
insertToTable($scanf[$s]);
}
if(mysqli_num_rows($resi) <= 0){
$seeql = "INSERT INTO files (name, ftype, dateup, size, downloads, dirGroup) VALUES ('$inamewoext', '$iextension', '$datec', $sze, 0, '$dire')";
$ress = mysqli_query($conn, $seeql);
}
}
}
insertToTable("uploads");
您可能会考虑使用
recursiveIterator
而不是递归函数 - 它可以轻松地浏览文件夹层次结构并允许访问文件名/文件路径。我编写了一个使用 recursiveIterator 的函数,该函数应该可以完成您希望执行的操作。将数据库连接声明为 global
后,可以在函数内访问数据库连接,但您也可以将其作为第二个命名参数添加到 insertTable
函数中。
function insertToTable( $dir=false ){
if( !$dir )return false;
# declare global variable (or include as a function parameter)
global $conn;
# basic sql cmds for use in "prepared statements"
$sql=(object)array(
'select'=>'SELECT * FROM `files` WHERE `name`=?',
'insert'=>'INSERT INTO `files` ( `name`, `ftype`, `dateup`, `size`, `downloads`, `dirGroup` ) VALUES (?,?,now(),?,0,?)'
);
# create the prepared statements
$stmts=(object)array(
'select'=>$conn->prepare($sql->select),
'insert'=>$conn->prepare($sql->insert)
);
# bind placeholders to variables (variables created later)
$stmts->select->bind_param('s',$name);
$stmts->insert->bind_param('ssss',$name,$type,$size,$folder);
# counter variable for info
$counter=0;
# create the recursiveIterators & scan the directory
$dirItr=new RecursiveDirectoryIterator( $dir, RecursiveDirectoryIterator::KEY_AS_PATHNAME );
$recItr=new RecursiveIteratorIterator( $dirItr, RecursiveIteratorIterator::CHILD_FIRST );
# iterate through scan results
foreach( $recItr as $obj => $entity ) {
# process files only
if( $entity->isFile() ){
$type = pathinfo( $entity->getPathName(), PATHINFO_EXTENSION );
$name = pathinfo( $entity->getPathName(), PATHINFO_FILENAME );
$folder= realpath( pathinfo( $entity->getPathName(), PATHINFO_DIRNAME ) );
$size=filesize( $entity->getPathName() );
# query db to see if file already exists
$stmts->select->execute();
$stmts->select->store_result();
# If file does not exist in db, add it.
if( $stmts->select->num_rows==0 ){
$stmts->insert->execute();
$counter++;
printf('Adding: %s to db<br>',$name);
}
$stmts->select->free_result();
}
}
# close statement objects
$stmts->select->close();
$stmts->insert->close();
# leave db open if other tasks are to follow
# otherwise call $conn->close() and return.
return $counter ? sprintf('Scan finished: %d files processed', $counter ) : 'Scan completed: No new files added';
}
$dir=__DIR__ . '/uploads';
print insertToTable( $dir );