0

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>";

            }

        }
    }
pankaj agarwal
  • 171
  • 2
  • 15
  • 2
    Too much code. Make a simple test that makes a basic csv file. If it works then that part of your code is ok. If it doesn;t, then you know where to look next. – John Conde Oct 10 '15 at 19:56
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Pedro Lobito Oct 10 '15 at 19:57
  • Nothing is in error. only warning and notices. Warning is Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/geoCode/class.ProgressBar.php:108) in /var/www/html/geoCode/index.php on line 126 Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/geoCode/class.ProgressBar.php:108) in /var/www/html/geoCode/index.php on line 127 – pankaj agarwal Oct 10 '15 at 20:05
  • So you are outputting prior to using the `header` function. You cant do that – chris85 Oct 10 '15 at 20:07
  • Sorry, Can you please explain this? @chris85 – pankaj agarwal Oct 10 '15 at 20:09
  • You can't output content prior to using the `header`, http://php.net/manual/en/function.header.php, function. Once you output you've sent a header. See http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – chris85 Oct 10 '15 at 20:13

0 Answers0