I've been struggling with a stange problem with fgetcsv.
I'm using a straight-forward fopen in read-only mode, looping through the file and building an sql statement as I go through. All neccessary validations are carried out before presenting the user with the option to 'Commit' to importing the data into mySQL.
The CSV file is 6.47kb and contains 25 records
The problem occurs at the line where
$data[] = fgetcsv($file_handle);
Once this line has been executed, the csv file is emptied/cleared and the file size is 0 bytes
I have proved this by breaking out of or exiting from code at various points and watching the file on the server. See below
if(($file_handle = fopen($_upload_dir.$filename, 'rb')) !== FALSE)
{
# If I break out here, the file is safe
# loop through the file until EOF
while (!feof($file_handle) )
{
# read each line into a 2-dimensional array.
# the first line is the column headers, If we are using them
# subsequent lines are the data
# rows and columns are zero based so...
# $line_of_text[0][2] is the 3rd column header
# $line_of_text[5][2] is the 3rd field of the 4th record
# If I break out here, the file is safe
$data[] = fgetcsv($file_handle);
# If I break out here, the file has been cleared of all content and becomes 0 bytes
Even more strange is that the complete data is successfully read and my full sql statement is written.
Why does the file get emptied when I am clearly opening in read-only mode?