CSV 无法下载到特定位置

CSV cannot be download to a specific location

我正在从数组生成 CSV,但它会自动下载到临时文件夹,有没有办法为要下载的文件定义下载位置。

while($finalRes = mysql_fetch_assoc($excute))
            {
            $tables[] = $finalRes;

            }

    function convert_to_csv($input_array, $output_file_name, $delimiter)
{
    /** open raw memory as file, no need for temp files */
    $temp_memory = fopen('php://memory', 'w');
    /** loop through array  */
    $header = array("XXX","XX","XX","XX","XX","XX","XX","XX");
    fputcsv($temp_memory, $header, $delimiter);
    foreach ($input_array as $line) {
        /** default php csv handler **/
        fputcsv($temp_memory, $line, $delimiter);
    }
    /** rewrind the "file" with the csv lines **/
    fseek($temp_memory, 0);
    /** modify header to be downloadable csv file **/
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false);
    header("Content-Transfer-Encoding: binary");
    header('Content-Type: application/csv');
    header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
    /** Send file to browser for download */
    fpassthru($temp_memory);
}

convert_to_csv($tables, 'report.csv', ',');

用户的浏览器设置控制下载文件的保存位置。您无权访问或控制它。恐怕你无能为力了。

根据 PHP 手册,您应该可以使用 fopen().

将 csv 保存到新位置
$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);
// Here is where you save the name (and directory)
// mode determines the type of file (read/write, etc)
$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);