我有php代码从路径名读取所有pdfs文件:pdf_folder/2020/并将其发送到我的数据库
我需要修改代码以读取3条路径:
- pdf_folder/2020/
- pdf_folder/2021/
- pdf_folder/2022/
我的代码:
<?php
include('config.php');
$extentions = array('pdf');
$dir = new DirectoryIterator('pdf_folder/2020/');
//SET Statues to Zero
$sql5 = "UPDATE pdfs SET status=0";
$query5 = mysqli_query($con,$sql5);
foreach($dir as $fileinfo){
if($fileinfo->isFile()){
$extention = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
if(in_array($extention,$extentions)){
$name = $fileinfo->getFilename();
$sql2 = "SELECT name FROM pdfs WHERE name = '$name' LIMIT 1";
$query2 = mysqli_query($con,$sql2);
if(mysqli_num_rows($query2) > 0){
while($row = mysqli_fetch_assoc($query2)){
$name2 = $row["name"];
$sql3 = "UPDATE pdfs SET status=1 WHERE name = '$name2' ";
$query3 = mysqli_query($con,$sql3);
}
}else{
$sql4 = "INSERT INTO pdfs(name) VALUES('$name')";
$query4 = mysqli_query($con,$sql4);
}
echo'
<a href="/php/test/'.$fileinfo->getFilename().'"> '.$fileinfo->getFilename().'</a><br>
';
}
}
}
我尝试了这个代码,但不起作用:
<?php
$dirs = scandir('pdf_folder');
$folders_to_process = ['2021', '2022', '2020'];
foreach ($dirs as $folder) {
if ($folder == '..' || $folder == '.' || !is_dir($folder) || !in_array($folder, $folders_to_process)) continue;
$extentions = array('pdf');
$dir = new DirectoryIterator($folder);
您应该能够通过在开始检查文件夹之前创建语句来稍微重新考虑代码,以利用Prepared Statements
。下面使用一个年份数组来形成外循环(如上所述(,在该循环的每次迭代中,年份都会发生变化,因此您的原始代码现在在不同的文件夹中运行。
我已经测试了以下内容,它似乎工作正常。
<?php
include('config.php');
# Folders for years of interest to scan
$years=array(2020,2021,2022);
# for the output
$li=array();
# Allowed file extensions
$extensions = array('pdf');
# The various SQL cmds used with placeholders where required for use within prepared statements
$sql=(object)array(
'status' => 'update `pdfs` set `status`=0',
'select' => 'select `name` from `pdfs` where `name`=? limit 1',
'update' => 'update `pdfs` set `status`=1 where `name` = `name`=?',
'insert' => 'insert into `pdfs` ( `name` ) values ( ? )'
);
# The prepared statements
$stmt=(object)array(
'status' => $con->prepare( $sql->status ),
'select' => $con->prepare( $sql->select ),
'update' => $con->prepare( $sql->update ),
'insert' => $con->prepare( $sql->insert )
);
# set all PDF status to zero & bind other statements to $name variable
# in mySQLi this variable does not need exist at this stage, in PDO it does.
$stmt->status->execute();
$stmt->select->bind_param('s',$name);
$stmt->update->bind_param('s',$name);
$stmt->insert->bind_param('s',$name);
# iterate through all the years and construct new folder path to scan
foreach( $years as $year ){
$dir=new DirectoryIterator( sprintf( '%s/pdf_folder/%s', __DIR__, $year ) );
#iterate through the files found
foreach( $dir as $info ){
if( !$info->isDot() ){
# is the file extension OK?
$ext=strtolower( pathinfo( $info->getFilename(), PATHINFO_EXTENSION ) );
if( in_array( $ext, $extensions ) ){
# the $name variable is now populated for the SQL prepared statements to execute.
$name=$info->getFilename();
$stmt->select->execute();
$stmt->select->store_result();
if( $stmt->select->num_rows > 0 ){
$stmt->update->execute();
}else{
$stmt->insert->execute();
}
$li[]=sprintf('<li><a href="/pdf_folder/%2$d/%1$s" target="_blank">%1$s</a></li>', $name, $year );
}
}
}
}
# print the HTML list of hyperlinks
printf('<ul>%s</ul>',implode(PHP_EOL,$li));
您可以使用scandir((函数将pdf_folder收集到一个数组中,然后循环使用它,并在检查后执行现有操作。
例如,这将有所帮助:
<?php
$dirs = scandir('pdf_folder');
$folders_to_process = ['2021', '2022', '2020'];
foreach ($dirs as $folder) {
if ($folder == '..' || $folder == '.' || !is_dir($folder) || !in_array($folder, $folders_to_process)) continue;
$extentions = array('pdf');
$dir = new DirectoryIterator($folder);
//SET Statues to Zero
$sql5 = "UPDATE pdfs SET status=0";
$query5 = mysqli_query($con, $sql5);
foreach ($dir as $fileinfo) {
if ($fileinfo->isFile()) {
$extention = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
if (in_array($extention, $extentions)) {
$name = $fileinfo->getFilename();
$sql2 = "SELECT name FROM pdfs WHERE name = '$name' LIMIT 1";
$query2 = mysqli_query($con, $sql2);
if (mysqli_num_rows($query2) > 0) {
while ($row = mysqli_fetch_assoc($query2)) {
$name2 = $row["name"];
$sql3 = "UPDATE pdfs SET status=1 WHERE name = '$name2' ";
$query3 = mysqli_query($con, $sql3);
}
} else {
$sql4 = "INSERT INTO pdfs(name) VALUES('$name')";
$query4 = mysqli_query($con, $sql4);
}
echo '
<a href="/php/test/' . $fileinfo->getFilename() . '"> ' . $fileinfo->getFilename() . '</a><br>
';
}
}
}
}