【问题标题】:create a bibtex archive with PHP使用 PHP 创建 bibtex 存档
【发布时间】:2016-02-24 19:32:20
【问题描述】:

我正在尝试创建一个代码,该代码基于来自 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"

谢谢!

【问题讨论】:

    标签: php postgresql bibtex


    【解决方案1】:

    因为文件名存储在代码中的 $name 变量中:

    header('Content-Disposition: attachment; filename="' . $name . '"');
    

    $file变量是一个资源,与打开的文件相连。

    顺便说一句 - 你没有正确关闭文件。

    fclose($fp); // $fp is NOT defined, your pointer is in $file variable
    

    关闭的正确代码是:

    fclose($file);
    

    下一步,重新排列您的代码。 首先 - 标题应该在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
    

    【讨论】:

    • 知道了!像魅力一样工作!关于 $text 变量?为什么它在存档中没有结果而 'echo "@phdthesis{php.......' 没有结果?
    • 你刚刚救了我。万分感谢。很高兴知道你解释了什么。谢谢你,伙计:)
    猜你喜欢
    • 2013-06-25
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 2013-12-09
    • 1970-01-01
    • 2017-10-24
    • 2012-07-25
    相关资源
    最近更新 更多