0

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.

ioan
  • 295
  • 2
  • 7
  • 23
  • Can you show a `var_dump($file_name);` please – RiggsFolly Nov 20 '18 at 17:56
  • I suggest you code this line on one line `header("Content-Disposition: attachment; filename='".basename($file_name).".txt'");` and not over 2 lines, we are not using punch cards anymore – RiggsFolly Nov 20 '18 at 17:57
  • $file_name is just a string, like "Joel S Akers" i use it to name the new file. I have that header line on one line now but it didn't seem to affect anything. – ioan Nov 20 '18 at 23:53

0 Answers0