I need a little help. I have a Model file, this imports a CSV file in to the database. But my model file takes this CSV from the C:\wamp\www\cakephp-2.5.6\app\tmp\uploads\Kontaktid
and if CSV file named export.csv
not exsist in this folder, then my function keep adding empty rows in to database.
Can I do something like this (I tried to add this in to Controller, but this is not working) ?
If empty(Kontaktid) {
echo ("CSV file is not found");exit;
}
This is my Controller file:
function import() {
$this->Kontaktid->import('export.csv');
}
And this is my Model file:
<?php
class Kontaktid extends AppModel {
public $validate = array(
'title' => array('rule' => 'notEmpty'),
'body' => array('rule' => 'notEmpty')
);
var $name = 'Kontaktid';
function import($filename) {
// set the filename to read CSV from
$filename = TMP . 'uploads' . DS . 'Kontaktid' . DS . $filename;
// open the file
$handle = fopen($filename, "r");
// read the 1st row as headings
$header = fgetcsv($handle);
// read each data row in the file
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$this->query ("INSERT into kontaktids(id,Eesnimi,Perenimi,Maakond,Linn,Tanav,Maja,Telefon,lisanumbrid) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]')");
$this->query;
}
print "Import done";
// close the file
fclose($handle);
}
}
Thanks for helping !