There is an XML file in the files folder.

By clicking on the button, it is "going" and should be downloaded.

How can this be done with a header (or some other way) so that when saving the page does not appear?

    1 answer 1

    if you know the header function, this is good. but do not be lazy to read the documentation for it:

    If you need to warn the user about the need to save sent data, such as a generated PDF file, you can use the Content-Disposition header, which substitutes the recommended file name and causes the browser to show the download dialog.
    Example # 1 Download Dialog

      // Будем передавать PDF header('Content-Type: application/pdf'); // Он будет называться downloaded.pdf header('Content-Disposition: attachment; filename="downloaded.pdf"'); // Исходный PDF-файл original.pdf readfile('original.pdf'); 

    in your case, the mime-type should specify text/xml or application/xml , well, and if the file is going to dynamically, then output it via echo/print :

      $xml = new SimpleXMLElement("<root></root>"); // формируем xml // .... // сохраняем в файл $xml->asXML("/path/to/your/file.xml"); // отправляем заголовки для скачивания header('Content-Type: application/xml'); header('Content-Disposition: attachment; filename="myfile.xml"'); // скидываем сам файл print $xml->asXML(); 
    • If the file is in a folder, then where to specify the path to the file? - Lyavontiy
    • @Lyavontiy if the file is ready in a folder, then generally just make a link to its <a href="/files/my/file.xml" target="_blank">скачать</a> - teran
    • if the directory is not accessible from the outside, then the path must be specified in the readfile('path/to/my/file.xml') - teran
    • By pressing a button, the file should be created and downloaded at the end - Lyavontiy
    • @Lyavontiy slightly added the answer - teran