我有一个朋友编写的脚本,它从 .txt 文件目录中获取所有内容,并将它们与其他一些信息一起上传到数据库中。
又名:文件名 | 内容
每个文件的内容——简单的文本信息——存储在相应的数据库条目中。到目前为止,它运行得很好,但是根本没有读取一堆新文本文件的内容。 文件名被很好地读取,并且该信息很容易导入到数据库中。这只是实际的内容。我之前导入的旧 .txt 文件仍然可以完美导入。
长话短说- 有谁知道为什么某些 .txt 文件的内容可以读取,而其他文件则无法读取?可能存在编码问题等?(尽管它们来自同一个人并且看起来相同)我正在失去理智。
谢谢!
$dir = 'text';
//createxml(10);exit;
$time_start = microtime(true);
$files = scandir($dir);
natsort($files);
foreach ($files as $v) {
if ($v != "." && $v != ".." && $v != "thumbs" && $v != ".DS_Store") {
//get work done
$text = file_get_contents($dir.'/'.$v);
//get volume, page, county
$ta = explode('.',$v);
$ma = explode('_',$ta[0]);
$last = count($ma)-1;
$volume = '';
$year = '1999';
for ($i = 0; $i < $last; ++$i)
{
$volume .= $ma[$i].'_';
}
$volume = $mysqli->real_escape_string(rtrim($volume,'_'));
$pagenr = $mysqli->real_escape_string($ma[$last]);
$ntext = $mysqli->real_escape_string(getmtext($text));
$pdf = 'http://griffiths.****.ie/gv4/thoms/'.$volume.'/'.$volume.'_pg'.str_pad($pagenr, 4, "0", STR_PAD_LEFT).'.pdf';
$thumb = 'http://griffiths.****.ie/gv4/thoms/'.$volume.'/thumbs2/'.$volume.'_'.str_pad($pagenr, 4, "0", STR_PAD_LEFT).'.jpg';
//create sql
$echo[$volume] .= "('','$year','$pagenr','$volume','$ntext','$pdf','$thumb'),";
$excl[$volume]=true;
}
}
// check if there is volume already in DB
foreach ($excl as $k => $v) {
$volumes .= "'$k',";
}
$volumes = rtrim($volumes,',');
$excls ='';
if ($result = $mysqli->query("SELECT DISTINCT volume FROM thoms_copy2 WHERE volume in ($volumes)")) {
//found volumes already in DB
while ($r = $result->fetch_array(MYSQLI_NUM))
//we only need the new volumes, so we will ignore the rest
unset($echo[$r[0]]);
$result->close();
}
//create mysql string
foreach ($echo as $k => $v) {
$echot .= $v.',';
}
$echot = rtrim($echot,',');
if ($echot) {// if i have something to insert
//insert into DB
$sql = "INSERT INTO `thoms_copy2` (`id`,`year`,`main_page`, `volume`, `texty`, `pdf`, `thumb`) VALUES $echot";
if ($result = $mysqli->query($sql)) {
echo "Done.";
//create the XML file
createxml($mysqli->affected_rows);
} else {
printf("Error message: %s\n", $mysqli->error);
echo "<br><br>$sql";
}
} else { echo "Done. Nothing new."; }
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "<br>$time";
//functions ===============================================================
function getmtext($str) {
$text = '';
$words = str_word_count($str, 1);
foreach ($words as $word) {
if ($word[0] >= 'A' && $word[0] <= 'Z')
if (strlen($word)>1)
$text .= $word.' ';
}
return $text;
}