使用 PHP 创建一个 bibtex 档案
create a bibtex archive with PHP
我正在尝试创建一个代码,该代码基于来自 BD 的信息创建一个 bibtex 存档。这就是我得到的:
<?php
include("classe/conexao.php");
session_start();
$_SESSION[id_tese_especifica] = $_GET['id'];
$result = pg_query("SELECT titulo, id, data, autor_nome FROM teses ORDER BY data DESC");
$arr = pg_fetch_array($result);
echo "@phdthesis{phpthesis,
author={" . $arr[0] . "},
title={" . $arr[6] . " " . $arr[3] . "},
month={" . $arr[2] . "}";
$name = $_GET['id'] . ".bib";
$file = fopen($name, 'a');
$text = "test (it doesn't appears on archive and I don't know why, so I used the echo above and worked, but this is what should be on archive, or isn't?)";
fwrite($file, $text);
readfile($file);
fclose($fp);
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Expires: 0');
?>
之后,它会下载一个名为 'Resource id #6' 的存档,为什么?名称应基于此:$name = $_GET['id'] . ".bib"
.
谢谢!
因为文件名存储在代码中的 $name
变量中:
header('Content-Disposition: attachment; filename="' . $name . '"');
而$file
变量是一个资源,与打开的文件相关联。
顺便说一下 - 您没有正确关闭文件。
fclose($fp); // $fp is NOT defined, your pointer is in $file variable
关闭的正确代码是:
fclose($file);
下一步,重新排列你的代码。
首先 - headers 应该发送 BEFORE 任何输出。
您目前遇到的是一些错误的混合,它们意外地向您展示了您想要的东西。
正确的代码应该是:
$name = $_GET['id'] . ".bib";
// first of all - set proper headers:
header('Content-Disposition: attachment; filename="' . $name . '"');
header('Expires: 0');
// next - do a query
$result = pg_query("SELECT titulo, id, data, autor_nome FROM teses ORDER BY data DESC");
$arr = pg_fetch_array($result);
// use echo for testing purposes only
// cause echo considered as a content of your file
echo "@phdthesis{phpthesis,
author={" . $arr[0] . "},
title={" . $arr[6] . " " . $arr[3] . "},
month={" . $arr[2] . "}";
$fp = fopen($name, 'a');
$text = "test (it doesn't appears on archive and I don't know why, so I used the echo above and worked, but this is what should be on archive, or isn't?)";
fwrite($fp, $text);
fclose($fp); // don't forget to close file for saving newly added data
readfile($name); // readfile takes a filename, not a handler.
die(); // end your script cause in other case all other data will be outputted too
我正在尝试创建一个代码,该代码基于来自 BD 的信息创建一个 bibtex 存档。这就是我得到的:
<?php
include("classe/conexao.php");
session_start();
$_SESSION[id_tese_especifica] = $_GET['id'];
$result = pg_query("SELECT titulo, id, data, autor_nome FROM teses ORDER BY data DESC");
$arr = pg_fetch_array($result);
echo "@phdthesis{phpthesis,
author={" . $arr[0] . "},
title={" . $arr[6] . " " . $arr[3] . "},
month={" . $arr[2] . "}";
$name = $_GET['id'] . ".bib";
$file = fopen($name, 'a');
$text = "test (it doesn't appears on archive and I don't know why, so I used the echo above and worked, but this is what should be on archive, or isn't?)";
fwrite($file, $text);
readfile($file);
fclose($fp);
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Expires: 0');
?>
之后,它会下载一个名为 'Resource id #6' 的存档,为什么?名称应基于此:$name = $_GET['id'] . ".bib"
.
谢谢!
因为文件名存储在代码中的 $name
变量中:
header('Content-Disposition: attachment; filename="' . $name . '"');
而$file
变量是一个资源,与打开的文件相关联。
顺便说一下 - 您没有正确关闭文件。
fclose($fp); // $fp is NOT defined, your pointer is in $file variable
关闭的正确代码是:
fclose($file);
下一步,重新排列你的代码。 首先 - headers 应该发送 BEFORE 任何输出。 您目前遇到的是一些错误的混合,它们意外地向您展示了您想要的东西。
正确的代码应该是:
$name = $_GET['id'] . ".bib";
// first of all - set proper headers:
header('Content-Disposition: attachment; filename="' . $name . '"');
header('Expires: 0');
// next - do a query
$result = pg_query("SELECT titulo, id, data, autor_nome FROM teses ORDER BY data DESC");
$arr = pg_fetch_array($result);
// use echo for testing purposes only
// cause echo considered as a content of your file
echo "@phdthesis{phpthesis,
author={" . $arr[0] . "},
title={" . $arr[6] . " " . $arr[3] . "},
month={" . $arr[2] . "}";
$fp = fopen($name, 'a');
$text = "test (it doesn't appears on archive and I don't know why, so I used the echo above and worked, but this is what should be on archive, or isn't?)";
fwrite($fp, $text);
fclose($fp); // don't forget to close file for saving newly added data
readfile($name); // readfile takes a filename, not a handler.
die(); // end your script cause in other case all other data will be outputted too