PHP创建文件和下载文件

时间:2016-07-16 18:54:08

标签: php html

我正在尝试创建一个程序,它允许您上传2个文件,运行一些代码然后创建一个新文件并使用新数据下载它。

我已经搜索过,似乎我也在做同样的事情,但它不会创建文件,只会在网站上打印文字。

这是代码。

    header("Content-type: text/plain");
    header("Content-Disposition: attachment; filename=unique_ips.txt");
    echo 'IP addresses only found in file A:' . "\n";
    echo $array_a_result . "\n\n";
    echo 'IP addresses only found in file B:' . "\n";
    echo $array_b_result;
    exit();

它似乎并不关心第二个标题。

2 个答案:

答案 0 :(得分:0)

请记住,header()之前不要回复任何内容。我刚刚测试了下面的代码。应该按预期工作!

<?php
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=myfile.txt');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
flush();
echo 'IP addresses only found in file A:' . "\n";
echo $array_a_result . "\n\n";
echo 'IP addresses only found in file B:' . "\n";
echo $array_b_result;

答案 1 :(得分:0)

在您发布的代码之前,没有办法想象您发生了什么,但是,如果您正在进行下载,则应该注意不要输出任何文本(甚至是空格)直到那部分代码完全运行之后。在<?php之前甚至只有一个空格会引发错误。以下是您可能想要尝试的功能(如果您愿意):

    <?php

        /**
         * FULL-NAME OF THE FILE TO DOWNLOAD
         * @param $downloadFileName
         * NEW-NAME FOR THE FILE... DO NOT USE EXTENSIONS LIKE .pdf OR .doc OR .txt
         * @param null $newFileName
         * @return bool
         *
         */
        function processDownload($downloadFileName, $newFileName=null) {
            $ext              = pathinfo($downloadFileName, PATHINFO_EXTENSION);
            if(!$newFileName){
                $newFileName  = basename($downloadFileName);
            }else{
                $newFileName .= "." . $ext;
            }
            if(file_exists($downloadFileName)){
                $size       = @filesize($downloadFileName);
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename=' . $newFileName );
                header('Content-Transfer-Encoding: binary');
                header('Connection: Keep-Alive');
                header('Expires: 0');
                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                header('Pragma: public');
                header('Content-Length: ' . $size);
                readfile($downloadFileName);
                return TRUE;
            }
            return FALSE;
        }

如果您选择试用此功能,建议将其作为脚本顶部的第一个函数调用,如下所示:

<?php  
        // NOTICE THAT THERE IS NO WHITE-SPACE BEFORE PHP.
        // AND NO OUTPUT WAS SENT (IE: NOTHING ECHOED OUT...)

        processDownload(__DIR__ . "/downloads/unique_ips.txt", "Unique-IPs");

        // FROM HERE ON, YOU MAY ECHO WHATEVER YOU WISH...
        // echo $whatever;
        // echo $youWish;
        // echo $may;
        // echo $nowBeEchoed;
        // echo $afterCalling;
        // echo $theFunctionAbove;

        echo 'IP addresses only found in file A:' . "\n";
        echo $array_a_result . "\n\n";
        echo 'IP addresses only found in file B:' . "\n";
        echo $array_b_result;


        function processDownload($downloadFileName, $newFileName=null) {
            $ext              = pathinfo($downloadFileName, PATHINFO_EXTENSION);
            if(!$newFileName){
                $newFileName  = basename($downloadFileName);
            }else{
                $newFileName .= "." . $ext;
            }
            if(file_exists($downloadFileName)){
                $size       = @filesize($downloadFileName);
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename=' . $newFileName );
                header('Content-Transfer-Encoding: binary');
                header('Connection: Keep-Alive');
                header('Expires: 0');
                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                header('Pragma: public');
                header('Content-Length: ' . $size);
                readfile($downloadFileName);
                return TRUE;
            }
            return FALSE;
        }