djanim8 Posted July 22, 2007 Share Posted July 22, 2007 I have the following code after uploading a csv (text) file: $file = fopen($_FILES['theFile']['tmp_name'], 'rb') or die("Can't open file"); $theData = fread($file,filesize($_FILES['theFile']['tmp_name'])); fclose($file); $delimiter = "\n"; $splitcontents = explode($delimiter, $theData); for whatever reason, its not reading more than two lines.. it has about 12,000 lines in it, and needs to be split by the "\n" then I loop through that array and split it by the ",".. its only reading the first 2 lines.. what am I doing wrong? Link to comment https://forums.phpfreaks.com/topic/61186-not-reading-whole-file/ Share on other sites More sharing options...
cooldude832 Posted July 22, 2007 Share Posted July 22, 2007 try file_get_content() instead of reading it for the size of it. also there are a lot of functions built around csv reading Link to comment https://forums.phpfreaks.com/topic/61186-not-reading-whole-file/#findComment-304471 Share on other sites More sharing options...
cooldude832 Posted July 22, 2007 Share Posted July 22, 2007 12,000 lines is a lot and I think the handlers have a max input for fread() (i don't know what it is in bytes), but you might have to open the first have and then the second half seperatly Link to comment https://forums.phpfreaks.com/topic/61186-not-reading-whole-file/#findComment-304472 Share on other sites More sharing options...
djanim8 Posted July 22, 2007 Author Share Posted July 22, 2007 ok I've tried a couple different things, but I don't think the server is running 5.0.. do I tried this: $row = 1; $handle = fopen($_FILES['theFile']['tmp_name'], "r"); while (($data = fgetcsv($handle, , "\n")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; while (($stuff = fgetcsv($data, , ",")) !== FALSE) { echo $stuff[1]."<br>"; } } fclose($handle); and still no luck any other help? Link to comment https://forums.phpfreaks.com/topic/61186-not-reading-whole-file/#findComment-304495 Share on other sites More sharing options...
Barand Posted July 22, 2007 Share Posted July 22, 2007 try <?php $row = 1; $handle = fopen($_FILES['theFile']['tmp_name'], "r"); echo '<pre>'; while ($data = fgetcsv($handle, 1024, ",")) { $num = count($data); echo "\n$num fields in line $row | "; $row++; foreach ($data as $field) echo "$field | "; } echo '</pre>'; fclose($handle); ?> Link to comment https://forums.phpfreaks.com/topic/61186-not-reading-whole-file/#findComment-304556 Share on other sites More sharing options...
djanim8 Posted July 22, 2007 Author Share Posted July 22, 2007 ok this code seems to work, but when I modify it, it doesn't.. man I'm so confused!! basically what I'm trying to do is upload a csv file from an excel spreadsheet that has information for a music collection, I'm trying to upload it line by line and changed the code to this: <?PHP $handle = fopen($_FILES['theFile']['tmp_name'], "r"); $mySQL1 = "INSERT INTO songs (title,artist,disc,track,genre1,genre2)"; while ($data = fgetcsv($handle, 1024, ",")) { $mySQL2 = " VALUES ("; foreach ($data as $field) $mySQL2 .= "'$field',"; $mySQL = $mySQL1.substr($mySQL2,strlen($mySQL2)-2).")"; echo $mySQL."<br>"; } ?> right now I'm just trying to get it to echo the string for the SQL statement, and it always returns this: INSERT INTO songs (title,artist,disc,track,genre1,genre2)',) now what am I missing? LOL Link to comment https://forums.phpfreaks.com/topic/61186-not-reading-whole-file/#findComment-304806 Share on other sites More sharing options...
Barand Posted July 22, 2007 Share Posted July 22, 2007 "VALUES" needs to be outside the loop eg INSERT INTO songs (title,artist,disc,track,genre1,genre2) VALUES ('aaa', 'bbbb', 1, 1, 'ccc', 'dddd') , ('qqq', 'bbbb', 1, 2, 'ccc', 'dddd') Not tested but try <?php $handle = fopen($_FILES['theFile']['tmp_name'], "r"); $count = 0; $mySQL1 = ''; while ($data = fgetcsv($handle, 1024, ",")) { if ($count%20 == 0 ) { if $mySQL1) mysql_query ($mySQL1); // write to db every 20 records; $mySQL1 = "INSERT INTO songs (title,artist,disc,track,genre1,genre2) VALUES \n"; $first = true; } $mySQL1 .= $first ? '' : ",\n"; // comma separator at end of lines - not before first $first = false; $mySQL1 .= vsprintf ("('%s', '%s', '%s', '%s', '%s', '%s')", $data); } // write last batch mysql_query ($mySQL1); ?> That's the slow way. Have a look at MySql's LOAD DATA INFILE command. Link to comment https://forums.phpfreaks.com/topic/61186-not-reading-whole-file/#findComment-304841 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.