user2636163
user2636163

Reputation: 39

Error reading a CSV file

I am very new PHP. Can some please solve my problem?

The following code works absolutely fine when i tried to execute using xampp in windows. But it dosen't work on Ubuntu when i try executing through ssh terminal.

Following is the php warning. but when i tried it on windows it works fine for all the records in CSV (it gives me insert or update statement for each record in the CSV)

PHP Warning: feof() expects parameter 1 to be resource, boolean given in /home/myetexts/Documents/codes/Pearson/test2.php on line 8
PHP Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /home/myetexts/Documents/codes/Pearson/test2.php on line 9

<?php
    ini_set('max_execution_time', 10000);
    $file = fopen('NZ_Price_list.csv', 'r');
    $count = 0;
    $con=mysql_connect("localhost","root","");
    mysql_select_db('newlocalabc');

    while(!feof($file)){
        $record = fgetcsv($file);
        if(!empty($record[0])){
           // echo 'ISBN: '.$record[0].'<br />';
        $price =round(($record[11])/0.85,2);
        if($record[3]== "Higher Education" || $record[3] == "Vocational Education"){
            $price =round((($record[11])/0.85)/0.97,2);
        }
        $sql = 'SELECT * FROM `products` WHERE `isbn` = '.$record[0];
        $result = mysql_query($sql);
        if(mysql_num_rows($result)){
            $data = mysql_fetch_object($result);

            $nsql = "UPDATE `products` SET `price` = '".$price."', `cover` = 'pics/cover4/".$record[0].".jpg', `cover_big` = 'pics/cover4/".$record[0].".jpg' WHERE `products`.`isbn` = ".$record[0].";";
        }else{
            $nsql = "INSERT INTO `products` (`id`, `isbn`, `title`, `publisher_id`, `description`, `supplier_id`, `price`, `author`, `cover`, `cover_big`, `status_id`, `timestamp`) 
            VALUES (NULL, '".$record[0]."', '".addslashes($record[1])."', '7','Not Available', '72', '".$price."', '".$record[2]."', 'pics/cover4/".$record[0].".jpg', 'pics/cover4/".$record[0].".jpg', '0',CURRENT_TIMESTAMP);";
        }
        echo $nsql.'<br />';
        //echo $price.'<br />';
        //echo '<pre>'; print_r($record);exit;
        }
        unset($record);
        $count++;
    }
    fclose($file);
    ?>

Hoping to hear back from some one soon.

Upvotes: 1

Views: 1000

Answers (1)

SteAp
SteAp

Reputation: 11999

The call

   fopen('NZ_Price_list.csv', 'r');

fails. A failing call doesn't return a so called PHP resource, but a boolean. Possible reasons are these:

  • File does not exist - file_exists()
  • Application can't open file for reading - is_readable()

Please be more specific an e.g. use an absolute file path like this and do some sanity checks:

$filePath = dirname( __FILE__ ) . '/..somePath../NZ_Price_list.csv';

// Ensure, that file exists and is reable
if ( ! file_exists( $filePath )) {
   throw new Exception( 'File does not exist: ' . $filePath , 10001 );
}
if ( ! is_readable( $filePath )) {
    throw new Exception( 'File not readable: ' . $filePath , 10002 );
}

// Then, try to open the file
$fileHandle = fopen( $filePath, 'r');

if ( ! is_resource( $fileHandle )) {
   throw new Exception( 'Failed to open file: ' . $filePath , 10003 );
}

Furtermore, PHP's stat() call might help. stat() provides details of a file - but can fail too...

Upvotes: 2

Related Questions