Sho Gum Lew
Sho Gum Lew

Reputation: 339

CSV cannot be download to a specific location

im generating a CSV from a array, but it gets automatically downloaded to a temp folder, Is there a way i could define a download location for the file to download.

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', ',');

Upvotes: 0

Views: 652

Answers (2)

Rasclatt
Rasclatt

Reputation: 12505

According to the PHP Manual, you should be able to save your csv to a new place with fopen().

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

Upvotes: 1

Len_D
Len_D

Reputation: 1420

The user's browser settings control where downloaded files are saved. You have no access to this or control over it. I'm afraid there is nothing you can do.

Upvotes: 0

Related Questions