'PHP generate excel sheet from array and save in tmp folder

i have written bellow code to generate and download excel sheet from array data

$data = array( 
array("NAME" => "John Doe", "EMAIL" => "[email protected]", "GENDER" => "Male", "COUNTRY" => 
      "United States")
     );

$fileName = "codexworld_export_data-" . date('Ymd') . ".xls"; 

header("Content-Disposition: attachment; filename=\"$fileName\""); 
header("Content-Type: application/vnd.ms-excel"); 

$flag = false; 
foreach($data as $row) { 
if(!$flag) { 
    $tbody .= implode("\t", array_keys($row)) . "\n"; 
    $flag = true; 
} 
$tbody .= implode("\t", array_values($row)) . "\n"; 
} 

however i want this generated excel sheet to be downloaded in /tmp folder in the same project folder.

i added bellow code to put the excel sheet in /tmp but it is not working

file_put_contents('tmp/text.xls', $tbody);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source