My goal is to have an html form submit some text, then php to create/update a file on the server, then download the new file to the user. Everything works, except instead of the updated file it downloads a file containing the html code of the page.
I want to download the file at the specified path, not the page that's running the code.
if(isset($_POST["download"])){
$file_name = session_to_file(); //update the file and return its name
$file_url = $url."/docs/".$file_name.".txt"; //the full url to the file starting with http
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment;
filename='".basename($file_name).".txt'"); //name of the downloaded file
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file_url); //download the file from the server
}
Ideally, I need a php or js function like download_file("path/filename"); but from other SO posts it seems more complicated, I tried their examples but it always downloads the wrong thing.