最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to download a txt file

programmeradmin0浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

I have tried some things... But can't download a txt file without getting all html code. I have a <a href='download/?cpf=1231231&nome=YanF'>Download</a> to link do the code that will do all download code.

Here what I have so far:

<?php 

                ob_clean();

                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);
                }              

                ob_clean();     
        ?>

EDIT --- I DID IT !!

After trying some more I changed what I was doing.

I created a template and a blank page, in that template I do what is above and redirect my <a> to that page. And works... Don't know if there's other way but that's what I could do to make work like I want.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

I have tried some things... But can't download a txt file without getting all html code. I have a <a href='download/?cpf=1231231&nome=YanF'>Download</a> to link do the code that will do all download code.

Here what I have so far:

<?php 

                ob_clean();

                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);
                }              

                ob_clean();     
        ?>

EDIT --- I DID IT !!

After trying some more I changed what I was doing.

I created a template and a blank page, in that template I do what is above and redirect my <a> to that page. And works... Don't know if there's other way but that's what I could do to make work like I want.

Share Improve this question edited May 14, 2020 at 15:57 YanF asked May 13, 2020 at 22:08 YanFYanF 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Try : header('Content-Type: text/plain');

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论