file_get_contents适用于某些.txt文件,而不适用于其他文件


file_get_contents working for some .txt files and not others?

我有一个朋友写的脚本,它从.txt文件目录中获取所有内容,并将它们与其他信息一起上传到数据库中。

aka:filename|内容

每个文件的内容——简单的文本信息——都存储在相应的数据库条目中。到目前为止,它运行得很好,但一堆新文本文件的内容根本没有被读取文件名读起来很好,而且信息很容易导入数据库。这只是实际内容。我以前导入的旧.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;
}

否,file_get_contents等于fopen+fread+fclose的组合,因此它提供了字节。如果你有一个错误的字符集,它不会影响这个事实,即你的文件由字节组成(它将由file_get_contents返回)。由于你不是脚本作者,很难说问题出在哪里,但你应该确保你的文件可以访问你的脚本(例如,拥有正确的权限)。