Skip to content
Advertisement

download an xml file on the local machine without saving it (php)

i’m trying to build a php script that download an xml file with data from a db, the problem is trying to save it in the webserver and then download it dosen’t work cause i don’t have the permission “failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden” i was wondering if it was possible to build the xml file and force the download without saving it on the web server. looked around and tryed different method but no positive output came out i’m new to this kind of things (downloading file fromm a php page), tryed using readfile() but it just show the xml content on the page i’m using php but also js ajax or html is fine

the code:

$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);  
'''

Advertisement

Answer

found a way that apparently avoid that permission lock from altervista, only need to use html (don’t think it’s a good way to do it but it works)

<a href='http://mysite.altervista.org/test_download/file.xml' download>download</a>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement