在本地计算机上下载 xml 文件而不保存它 (php)
download an xml file on the local machine without saving it (php)
我正在尝试构建一个 php 脚本,用于从数据库下载包含数据的 xml 文件,问题是试图将其保存在网络服务器中,然后下载它,但没有成功工作因为我没有权限“无法打开流:HTTP 请求失败!HTTP/1.0 403 Forbidden”
我想知道是否可以构建 xml 文件并强制下载而不将其保存在 Web 服务器上。
环顾四周并尝试了不同的方法,但没有出现积极的输出
我对这种事情很陌生(从 php 页面下载文件),尝试使用 readfile() 但它只在页面上显示 xml 内容
我正在使用 php,但也可以使用 js ajax 或 html
代码:
$results = mysqli_query($conn, "SELECT * FROM testdb");
$xml = new DomDocument("1.0","UTF-8");
$content = $xml->createElement("content");
$content = $xml->appendChild($content);
foreach($results as $result) {
$item = $xml->createElement("item");
$title = $xml->createElement("id",htmlspecialchars($result['num_id']));
$title = $item->appendChild($title);
$description = $xml->createElement("uid",htmlspecialchars($result['uid']));
$description = $item->appendChild($description);
$item = $content->appendChild($item);
}
$xml->FormatOutput = true;
$output = $xml->saveXML();
$xml->save("file.xml");
/*$xml = file_get_contents("http://mysite.altervista.org/test_download/file.xml");
file_put_contents("http://mysite.altervista.org/test_download/file.xml", $xml); // now your xml file is saved.
*/
$file_url = "http://mysite.altervista.org/test_download/file.xml";
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
'''
找到了一种显然可以避免来自altervista的权限锁定的方法,只需要使用html(不认为这是一个好方法,但它有效)
<a href='http://mysite.altervista.org/test_download/file.xml' download>download</a>
我正在尝试构建一个 php 脚本,用于从数据库下载包含数据的 xml 文件,问题是试图将其保存在网络服务器中,然后下载它,但没有成功工作因为我没有权限“无法打开流:HTTP 请求失败!HTTP/1.0 403 Forbidden” 我想知道是否可以构建 xml 文件并强制下载而不将其保存在 Web 服务器上。 环顾四周并尝试了不同的方法,但没有出现积极的输出 我对这种事情很陌生(从 php 页面下载文件),尝试使用 readfile() 但它只在页面上显示 xml 内容 我正在使用 php,但也可以使用 js ajax 或 html
代码:
$results = mysqli_query($conn, "SELECT * FROM testdb");
$xml = new DomDocument("1.0","UTF-8");
$content = $xml->createElement("content");
$content = $xml->appendChild($content);
foreach($results as $result) {
$item = $xml->createElement("item");
$title = $xml->createElement("id",htmlspecialchars($result['num_id']));
$title = $item->appendChild($title);
$description = $xml->createElement("uid",htmlspecialchars($result['uid']));
$description = $item->appendChild($description);
$item = $content->appendChild($item);
}
$xml->FormatOutput = true;
$output = $xml->saveXML();
$xml->save("file.xml");
/*$xml = file_get_contents("http://mysite.altervista.org/test_download/file.xml");
file_put_contents("http://mysite.altervista.org/test_download/file.xml", $xml); // now your xml file is saved.
*/
$file_url = "http://mysite.altervista.org/test_download/file.xml";
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
'''
找到了一种显然可以避免来自altervista的权限锁定的方法,只需要使用html(不认为这是一个好方法,但它有效)
<a href='http://mysite.altervista.org/test_download/file.xml' download>download</a>