We are trying to generate a CSV file using PHP. But its not generating. We gave 777 permission to the folder and gave hard coded path in header. but its not generating. Can any body tell me whats i am doing wrong?
if ($ext === 'csv') {
if (($handle = fopen($tmpName, 'r')) !== FALSE) {
// necessary if a large csv file
set_time_limit(0);
$row = 1;
$file = fopen('ids.txt', 'w');
$num_records =count(file($tmpName));
// $calc = 100/$num_records;
$calc=100/ $num_records;
echo "Total ".$num_records." records found.<br/>";
$p = new ProgressBar();
$output = fopen("php://output",'w') or die("Can't open php://output");
header("Content-Type:application/csv");
header("Content-Disposition:attachment;filename=pressurecsv.csv");
while ((($data = fgetcsv($handle, 1000, ',')) !== FALSE) && $row != 30) {
// number of fields in the csv
$address = implode(",", $data);
$p->setProgressBarProgress($x);
usleep(1000000*0.1);
$cityclean = str_replace(" ", "+", $address);
$details_url = "https://maps.googleapis.com/maps/api/geocode/json?address=" . $cityclean . "&sensor=false&key=".$api_array[$api_key_status];
// echo $details_url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$geoloc = json_decode(curl_exec($ch), true);
// print_r($geoloc);
if ( $geoloc['status'] === "OVER_QUERY_LIMIT" )
{
echo "OVER QUERY LIMIT. Using another key.";
$api_key_status++;
}
//print_r(address_reformat($geoloc['results'][0]));
//convert_to_csv(address_reformat($geoloc['results'][0]), 'report.csv', ',');
$address_tmp=address_reformat($geoloc['results'][0]);
if(isset($geoloc['results'][0]['geometry']['location']['lat']))
// foreach($address_tmp as $product)
{
//print_r($address_tmp);
fputcsv($output, $address_tmp);
}
$lat = $geoloc['results'][0]['geometry']['location']['lat'];
$lng = $geoloc['results'][0]['geometry']['location']['lng'];
// echo $address . "------Latitude: " . $lat . " ---- Longitude: " . $lng;
fwrite($file, $address . "; " . $lat . " ; " . $lng . "\n");
// echo $row." address coordinate fetched.";
$p->getContent();
// echo $address;
echo "<br/>";
// echo $details_url;
$x += $calc;
//$x++;
$row++;
}
$p->setProgressBarProgress(100);
fclose($file);
fclose($handle);
fclose($output) or die("Can't close php://output");
echo "\n To download generated file <a href='ids.txt'>Click Here</a> . \n";
echo " <iframe src='data.php' height='100%' width='100%' frameBorder='0'></iframe>";
}
}
}