0

This code works properly but fclose() does not run in the end.

Can anyone spot the issue with my code?

<?php

$ImagePath=$newpath;
$csvfile = fopen($ImagePath, 'r');

$i=0;

while (!feof($csvfile)) {
    $csv_data[] = fgets($csvfile, 1024);
    $csv_array = explode(",", $csv_data[$i]);
    $insert_csv = array();
    $insert_csv['Company_ID'] = $csv_array[0];
    $insert_csv['Close_Value'] = $csv_array[7];

    $table_exists_or_not = "SELECT * FROM '".$insert_csv['Company_ID']."'";
    $table_exists_or_not_result = mysqli_query($table_exists_or_not);

    if(empty($table_exists_or_not_result)){
        $sql = "CREATE TABLE `".$insert_csv['Company_ID']."` ( `date` VARCHAR(8) NOT NULL ,  `value` DECIMAL(11,2) NOT NULL , UNIQUE (`date`)  ) ENGINE = InnoDB";
        $result = mysql_query($sql);
        $qry1="INSERT INTO `".$insert_csv['Company_ID']."` (`date`, `value`) VALUES ('$date', '".$insert_csv['Close_Value']."')";
        $add1=mysql_query($qry1);
    }else{
        $qry1="INSERT INTO `".$insert_csv['Company_ID']."` (`date`, `value`) VALUES ('$date', '".$insert_csv['Close_Value']."') ";
        $add1=mysql_query($qry1);
    }
    $i++;
}

fclose($csvfile);

?>
Jeff Miller
  • 2,405
  • 1
  • 26
  • 41
  • 1
    What do you mean with fclose() didn't ran? Please also submit the occurred errors. – markvdlaan93 Feb 26 '17 at 12:34
  • sir fclose() command doesn't close the open file "$csvfile" – Ritesh Saini Feb 26 '17 at 12:44
  • You sure that you give a legitimate file as I see $imagePath = $newPath? If you pass a non-existing file fclose will return false – markvdlaan93 Feb 26 '17 at 12:55
  • sure sir file is definately exist, My solution is as under but i can't understand it: feof() is, in fact, reliable. However, you have to use it carefully in conjunction with fgets(). A common (but incorrect) approach is to try something like this: $fp = fopen("myfile.txt", "r"); while (!feof($fp)) { $current_line = fgets($fp); // do stuff to the current line here } fclose($fp); ?> The problem when processing plain text files is that feof() will not return true after getting the last line of input. You need to try to get input _and fail_ before feof() returns true. – Ritesh Saini Feb 26 '17 at 13:17
  • What is the value of `$csvfile`? It may be `null`. – Taha Paksu Feb 27 '17 at 09:01

0 Answers0