Php 保存CSV文件而不是下载

Php 保存CSV文件而不是下载,php,mysql,csv,Php,Mysql,Csv,所以我有这段代码从mysql数据库生成一个CSV文件。但是,它下载代码而不是保存到目录中以供进一步使用(需要通过phpmailer发送该文件)。 我应该做哪些更改才能将文件保存到目录中 $array = array(); if(file_exists('records_monthly.csv')) { unlink('records_monthly.csv'); } # Headers $array[] = array("Serial Number","Donat

所以我有这段代码从mysql数据库生成一个CSV文件。但是,它下载代码而不是保存到目录中以供进一步使用(需要通过phpmailer发送该文件)。 我应该做哪些更改才能将文件保存到目录中

$array = array();
if(file_exists('records_monthly.csv'))
    {
        unlink('records_monthly.csv');
    }
# Headers
$array[] = array("Serial Number","Donation Type", "Amount", "Status", "Date", "Orderref", "DIN");
$serial=1;
try
{
    $s = $conn->query("SELECT c.firstname AS firstname, c.lastname as lastname, c.address AS address, c.city AS city, c.postalnumber AS postalnumber, c.email AS cemail, d.donation_type as donation_type, d.donation_amount as donation_amount, d.orderref as orderref, d.status as status, d.donation_on AS donation_on, d.din as din from customers c, donations d where c.email = d.donator");
}
catch(PDOException $e)
{
    echo $e->getMessage();
}
while($donations = $s->fetch(PDO::FETCH_OBJ))
{
    if($donations->status == 0)
    {
        $array[] = array($serial++,$donations->donation_type,$donations->donation_amount,"Failed",$donations->donation_on,$donations->orderref,$donations->din);    
    }
    else
    {
        $array[] = array($serial++,$donations->donation_type,$donations->donation_amount,"Success",$donations->donation_on,$donations->orderref,$donations->din);
    }

}

array_to_csv_download($array,"records_monthly.csv",",");

function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
    // open raw memory as file so no temp files needed, you might run out of memory though
    $f = fopen('php://memory', 'w'); 
    // loop over the input array
    foreach ($array as $line) { 
        // generate csv lines from the inner arrays
        fputcsv($f, $line, $delimiter); 
    }
    // rewind the "file" with the csv lines
    fseek($f, 0);
    // tell the browser it's going to be a csv file
 header('Content-Type: application/csv');
    // tell the browser we want to save it instead of displaying it
   header('Content-Disposition: attachement; filename="'.$filename.'";');
    // make php send the generated csv lines to the browser
   fpassthru($f);
}

直接写入命名文件,而不是写入
php://memory
并且不将标题和输出发送到浏览器

function array_to_csv_without_download($array, $filename = "export.csv", $delimiter=";") {
    $f = fopen($filename, 'w'); 
    // loop over the input array
    foreach ($array as $line) { 
        // generate csv lines from the inner arrays
        fputcsv($f, $line, $delimiter); 
    }
   fclose($f);
}

直接写入命名文件,而不是写入
php://memory
并且不将标题和输出发送到浏览器

function array_to_csv_without_download($array, $filename = "export.csv", $delimiter=";") {
    $f = fopen($filename, 'w'); 
    // loop over the input array
    foreach ($array as $line) { 
        // generate csv lines from the inner arrays
        fputcsv($f, $line, $delimiter); 
    }
   fclose($f);
}

好的,但是这个文件在之前的目录中不存在。因为cron作业每月运行一次此脚本,它删除了以前的文件,然后写入此新文件。如果该文件之前存在或不存在,又有什么关系?此代码创建一个新文件。。。。这就是
w
标志告诉PHP要做的事情:
'w'仅用于编写;将文件指针放在文件的开头,并将文件截断为零长度__如果该文件不存在,请尝试创建它\u
好了,又出现了一个愚蠢的问题。它在我的笔记本电脑上运行完全正常,但是在我的服务器上,它不工作,如果执行var_dump($f),则返回bool false,这可能是什么问题。检查文件权限,您的Web服务器用户/组可能没有该文件夹权限的写入权限,但该文件在当前目录中不存在。因为cron作业每月运行一次此脚本,它删除了以前的文件,然后写入此新文件。如果该文件之前存在或不存在,又有什么关系?此代码创建一个新文件。。。。这就是
w
标志告诉PHP要做的事情:
'w'仅用于编写;将文件指针放在文件的开头,并将文件截断为零长度__如果该文件不存在,请尝试创建它\u
好了,又出现了一个愚蠢的问题。它在我的笔记本电脑上运行完全正常,但是在我的服务器上,它不工作,如果执行var_dump($f),则返回bool false,这可能是什么问题。检查文件权限,您的Web服务器用户/组可能没有该文件夹权限的写入权限,但该文件在当前目录中不存在。因为cron作业每月运行一次此脚本,它删除了以前的文件,然后写入此新文件。如果该文件之前存在或不存在,又有什么关系?此代码创建一个新文件。。。。这就是
w
标志告诉PHP要做的事情:
'w'仅用于编写;将文件指针放在文件的开头,并将文件截断为零长度__如果该文件不存在,请尝试创建它\u
好了,又出现了一个愚蠢的问题。它在我的笔记本电脑上运行完全正常,但是在我的服务器上,它不工作,如果执行var_dump($f),则返回bool false,这里可能存在什么问题。检查文件权限,您的Web服务器用户/组可能没有该文件夹的写入权限