我有一个 php 脚本,它遍历包含制表符分隔文件的文件夹,逐行解析它们并将数据插入到 mysql 数据库中。由于服务器上的安全限制,我无法使用 LOAD TABLE,而且我无权访问配置文件。该脚本可以很好地解析 1 或 2 个较小的文件,但是在处理多个大文件时,我会收到 500 错误。似乎没有任何错误日志包含与错误有关的消息,至少我的托管服务提供商没有让我访问。下面是代码,我也愿意接受有关替代方法的建议来做我需要做的事情。最终,我希望这个脚本每 30 分钟左右触发一次,插入新数据并在完成后删除文件。
编辑:在进行菲尔建议的更改后,脚本仍然失败,但我现在在我的错误日志“mod_fcgid:120 秒内读取数据超时”中有以下消息,看起来脚本正在超时,知道我可以在哪里更改超时设置?
$folder = opendir($dir);
while (($file = readdir($folder)) !== false) {
$filepath = $dir . "/" . $file;
//If it is a file and ends in txt, parse it and insert the records into the db
if (is_file($filepath) && substr($filepath, strlen($filepath) - 3) == "txt") {
uploadDataToDB($filepath, $connection);
}
}
function uploadDataToDB($filepath, $connection) {
ini_set('display_errors', 'On');
error_reporting(E_ALL);
ini_set('max_execution_time', 300);
$insertString = "INSERT INTO dirty_products values(";
$count = 1;
$file = @fopen($filepath, "r");
while (($line = fgets($file)) !== false) {
$values = "";
$valueArray = explode("\t", $line);
foreach ($valueArray as $value) {
//Escape single quotes
$value = str_replace("'", "\'", $value);
if ($values != "")
$values = $values . ",'" . $value . "'";
else
$values = "'" . $value . "'";
}
mysql_query($insertString . $values . ")", $connection);
$count++;
}
fclose($file);
echo "Count: " . $count . "</p>";
}