PHP file download taking all html code instead of file content

Asked

Viewed 49 times

0

Well, as the title implies.

My code is downloading all the html from my page instead of just the content I write in the file.

if(isset($_GET['cpf'])){
                //config
                $namefile = $_GET['nome'].'.txt';
                $content = $_GET['cpf'];

                //save file
                $file = fopen($namefile, "w") or die("Unable to open file!");
                fwrite($file, $content);

                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename="'.basename($namefile).'"');
                header('Expires: 0');
                header('Pragma: public');
                header('Content-Length: '. filesize($file));
               //   ob_clean(); 
                flush();
                readfile($file);

                fpassthru($file);
                fclose($file);
                exit();
            }

I’ve looked over 10 different topics, but none related to my problem.

The ob_clean() delete all, download a blank file.

And yes, I’m trying to download a file txt

1 answer

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.