Jump to content

not reading whole file????


djanim8

Recommended Posts

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

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?

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);
?>

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

 

"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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.