Following on from a previous question I posted, which I managed to get working great, I'm now stuck trying to check and store the date of the csv file and then execute the script if the csv file is newer. I plan on running a cron job when this is working properly in order to check regularly for a newer csv file. I just can't seem to be able to work out why the date and csv file aren't being updated into the database when I manually add a modified csv file to the directory and then run the script manually.
Any help greatly appreciated, S.
Current code:
$stat = stat('../data/filename.csv');
$datetime = date ("F d Y H:i:s.", $stat['mtime']);
$datetime = mysql_real_escape_string($datetime);
$datetime = strtotime($datetime);
$datetime = date('Y-m-d H:i:s',$datetime);
echo "<br />Last modified: " . $datetime;
$query=mysql_query("select * from iscsvtime");
$dateResult=mysql_fetch_array($query);
$stored = $dateResult['added'];
if (($stored <= $datetime) && ($handle = fopen("../data/filename.csv", "r")) !== FALSE) {
$i=0;
echo "<br />Stored date: " . $stored;
mysql_query("INSERT INTO iscsvtime(added) VALUES ('$datetime')");
mysql_query('TRUNCATE TABLE isstock;');
while (($data = fgetcsv($handle, 0, ",")) !== FALSE)
{
if($i>0){
$q="insert into isstock set feedid='".$data[0]."', vehicleid='".$data[1]."', registration='".$data[2]."', colour='".$data[3]."', fueltype='".$data[4]."', year='".$data[5]."', mileage='".$data[6]."', bodytype='".$data[7]."', doors='".$data[8]."', make='".$data[9]."', model='".$data[10]."', variant='".$data[11]."', enginesize='".$data[12]."', price='".$data[13]."', previousprice='".$data[14]."', transmission='".$data[15]."', picturerefs='".$data[16]."', servicehistory='".$data[17]."', previousowners='".$data[18]."', description='".$data[19]."', fourwheeldrive='".$data[20]."', options='".$data[21]."', comments='".$data[22]."', new='".$data[23]."', used='".$data[24]."', site='".$data[25]."', origin='".$data[26]."', v5='".$data[27]."', condit='".$data[28]."', exdemo='".$data[29]."', franchiseapproved='".$data[30]."', tradeprice='".$data[31]."', tradepriceextra='".$data[32]."', servicehistorytext='".$data[33]."', capid='".$data[34]."'";
mysql_query($q);
}
?>
<tr>
<?php
foreach($data as $rec){
echo '<td>'.$rec.'</td>';
}
?>
</tr>
<?php
$i++;
}
}
fclose($handle);
EDIT: Turns out I managed to fix this myself; just had to change the if statement slightly and change the INSERT INTO iscsvtime to an UPDATE. Updated code below:
if (($datetime > $stored) && ($handle = fopen("../data/filename.csv", "r")) !== FALSE) {
$i=0;
mysql_query("UPDATE iscsvtime SET added = '$datetime' WHERE id = '0'");