如何解决PHP解析文件功能-保存到数据库
我今天大部分时间都在工作,所以我通常在迭代和使用键/值方面都取得成功。这次似乎不容易,在php文档和尾随的差异源之间来回运行,虽然没有运气,但这与我来的差不多。
因此在这段代码中,我们检查所有* .torrent文件的../../ uploads ,并解析每个文件并保存为BLOB =>数据库。
该代码可以很好地工作,可以单独上传。因此,我决定将它们循环在一起,并根据它们的DB ID / FILE ID(都匹配)将所有文件发送到数据库。因此,逻辑很容易。
这是我的代码
// Start parsing files => DB => LONGGLOB
foreach (glob("../../uploads/*.torrent") as $filename) {
// Match file ID with database ID (WHERE CLAUS)
$fileid = str_replace('.torrent','',$filename);
$filename1 = str_replace('../../uploads/',$filename);
$fp = fopen('../../uploads/'.$filename1,'rb');
//$fp = fopen($_FILES['torrent']['tmp_name'],'rb');
$data = '';
while (!feof($fp)) {
$data .= fread($fp,8192);
}
fclose($fp);
$arr = bdecode($data);
if ($arr === false) {
die('invalid torrent');
}
// TODO: more validity checks for torrent?
if (!array_key_exists('info',$arr)) {
die('invalid torrent');
}
$arr['info']['private'] = 1;
$infobc = bencode($arr['info']);
if ($infobc === false) {
die('bencoding error');
}
$info_hash = sha1($infobc);
$total_size = 0;
if (array_key_exists('files',$arr['info'])) {
foreach ($arr['info']['files'] as $file) {
if (array_key_exists('length',$file)) {
$total_size += $file['length'];
}
}
} else if (array_key_exists('length',$arr['info'])) {
$total_size += $arr['info']['length'];
}
$description = 'dsfsdfDESCR';
$name = 'dsfsdfNAME';
// BEncode data and send it to DB
$data = bencode($arr);
$an = array_key_exists('anonymous',$_POST);
$db->query_params("UPDATE torrents SET user_id = :user_id,name = :name,descr = :description,anon = :anonymous,data = :data,info_hash = :info_hash,size = :total_size WHERE torrent_id = :torrentid",array('user_id' => $_SESSION['user']['user_id'],'name' => $name,'description' => $description,'anonymous' => $db->encode_bool($an),'data' => $data,'info_hash' => $info_hash,'total_size' => $total_size,'torrentid' => $fileid)) or die('db error');
}
我终于循环了每个文件。但是现在的问题是,它揭示了第一个文件,但没有继续显示并逐个文件地解析(最低ID =>最高ID,任意顺序),从而在雪球效应中产生错误。
我已经做了很多尝试,直到抱怨fopen()第一个参数,所以我解决了这个问题,现在它没有正确地迭代。我相信这是我的逻辑,我听不懂。
因此 $ data = bencode($ arr)需要解析每个图元文件,并按照注释($ fp)的上传格式将其完美发送。任何帮助表示赞赏。
我已经使用所有不同的方法列出了文件,这一次是blob函数。我如何迭代每个文件并解析=>下一个文件??
已注释的部分运行正常。但是我们需要解析每个文件ID。
解决方法
我自己又回答了另一个。我最后一个选择就是停止在这里运行,无论如何,这是需要它的人的解决方案。一个小时后就知道了,非常感谢php.net!
解决方案:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$dir = new DirectoryIterator('../../uploads');
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
$getthelist = $fileinfo->getFilename();
$fp = fopen('../../uploads/' . $getthelist,'rb');
$data = '';
while (!feof($fp)) {
$data .= fread($fp,8192);
}
fclose($fp);
$arr = bdecode($data);
if ($arr === false) {
die('invalid torrent');
}
if (!array_key_exists('info',$arr)) {
die('invalid torrent');
}
$arr['info']['private'] = 1;
$infobc = bencode($arr['info']);
if ($infobc === false) {
die('bencoding error');
}
$info_hash = sha1($infobc);
$total_size = 0;
if (array_key_exists('files',$arr['info'])) {
foreach ($arr['info']['files'] as $file) {
if (array_key_exists('length',$file)) {
$total_size += $file['length'];
}
}
} else if (array_key_exists('length',$arr['info'])) {
$total_size += $arr['info']['length'];
}
$fileid = str_replace('.torrent','',$getthelist);
$data = bencode($arr);
$an = array_key_exists('anonymous',$_POST);
DB::run("UPDATE torrents SET data = :data,info_hash = :info_hash,size = :total_size WHERE torrent_id = :torrentid",array(
'data' => $data,'info_hash' => $info_hash,'total_size' => $total_size,'torrentid' => $fileid
)) or die('db error');
}
}
}
DirectoryIterator()是这里的答案。尽管其他方法有效,但它们却不如这种方法有效或合逻辑。它可以按预期运行。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。