Jump to content

reading the contents of a csv file


dennismonsewicz

Recommended Posts

how do i parse the data in a csv file to read every chunk of data?

 

It's already parsed with commas...  comma-separated values.  I'm not sure what you're trying to accomplish but this maybe close:

 

$i=0;
$handle = @fopen("your_file.csv", "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle);
	$pieces = explode(",", $buffer);
	$one = $pieces[0];
	$two = $pieces[1];
	$three = $pieces[2];	
	$your_array[$i]=$one.",".$two.",".$three;
	$i++;
}
}

 

So you can refer to the array key and explode each line.

Link to comment
Share on other sites

thanks for the code... i will be testing it now...

 

this is a good start at least. I am going to be needing to compare two csv files and where ever there is a match up between the two files it is to insert the contents in to a set of db tables. And the ones that could not match up there is supposed to be a error report generated showing the entries that could not be matched up

Link to comment
Share on other sites

this is the code i just updated and i am getting a blank screen:

 

foreach($_FILES['file']['name'] as $n => $name) {
				if(!empty($name)) {
					$target_path = "upload_path/" . $name;
					if(move_uploaded_file($_FILES['file']['tmp_name'][$n], $target_path)) {
						$i=0;
						$handle = @fopen($name, "r");
						if ($handle) {
							while (!feof($handle)) {
								$buffer = fgets($handle);
								$pieces = explode(",", $buffer);
								$one = $pieces[0];
								$two = $pieces[1];
								$three = $pieces[2];
								$your_array[$i]=$one.",".$two.",".$three;
								echo $your_array . "<br />";
								$i++;
								}
							}
						}
					}
				}

Link to comment
Share on other sites

Then you're going to need to have another while loop inside the EOF while loop to grab all the values.  Something like: ***NOT TESTED  p.s.- use print_r() to print arrays not echo.

 

$pieces = explode(",", $buffer);
         $x=0;                                    
         while($pieces[$x])  
 {
    $your_array[$i].=$pieces[$x];								    
         }									
print_r($your_array);
$i++;
}

Link to comment
Share on other sites

ok so it created an infinite loop...

 

updated code:

 

foreach($_FILES['file']['name'] as $n => $name) {
				if(!empty($name)) {
					$target_path = "upload_path/" . $name;
					if(move_uploaded_file($_FILES['file']['tmp_name'][$n], $target_path)) {
						$i=0;
						$handle = @fopen($target_path, "r");
						if ($handle) {
							while (!feof($handle)) {
								$buffer = fgets($handle);
								$pieces = explode(",", $buffer);
								 $x=0;                                    
								 while($pieces[$x])
								 {
    									$your_array[$i].=$pieces[$x];
								}
								print_r($your_array);
								$i++;
								}
							}
						}
					}
				}

Link to comment
Share on other sites

Cause $x is not incremented.  Sorry for that, like I said this is not tested.

 

	
$x=0;                                   
while($pieces[$x])
{
$your_array[$i].=$pieces[$x];
$x++;
}
print_r($your_array);
$i++;

Link to comment
Share on other sites

yeah the code just creates an inifinite loop and causes my browser to crash...

 

updated code:

 

foreach($_FILES['file']['name'] as $n => $name) {
				if(!empty($name)) {
					$target_path = "Upload/" . $name;
					if(move_uploaded_file($_FILES['file']['tmp_name'][$n], $target_path)) {
						$i=0;
						$handle = @fopen($target_path, "r");
						if ($handle) {
							while (!feof($handle)) {
								$buffer = fgets($handle);
								$pieces = explode(",", $buffer);
								 $x=0;                                    
								 while($pieces[$x])
								 {
    									$your_array[$i].=$pieces[$x];
									$x++;
								}
								print_r($your_array);
								$i++;
								}
							}
						}
					}
				}

Link to comment
Share on other sites

ok i have the code update and its reading the file contents

 

updated code:

 

$action		 	= $_GET['action'];
$session_id 	= $_GET['osCAdminID'];

$targetPath		= 'Upload/';
$randomNumber	= rand(1000, 9999);

switch($action) {

	case "upload": 

		foreach($_FILES['file']['tmp_name'] as $key => $value) {	
			if(move_uploaded_file($_FILES['file']['tmp_name'][$key], $targetPath . $randomNumber . '-' . $_FILES['file']['name'][$key])) {
				$aryFiles[] = $randomNumber . '-' . $_FILES['file']['name'][$key];
			} else { $continue = 99; }

			if($continue == 99) {
				echo '<strong>There was a problem uploading your files</strong>';
			} else {
				foreach($aryFiles as $key => $file) {
					$lnct = 0;

					$fileRead = fopen($targetPath . $file, 'r');

					while(!feof($fileRead)) {
						$fileContent[$key][$lnct] = explode(',',fgets($fileRead));

						foreach($fileContent[$key][$lnct] as $subkey => $column) {
							$quoFix = $column;
							if(substr($quoFix, 0, 1) == '"') $quoFix = substr($quoFix, 1, (strlen($quoFix)-1));
							if(substr($quoFix, (strlen($quoFix)-1), 1) == '"') $quoFix = substr($quoFix, 0, (strlen($quoFix)-1));

							if($quoFix) $fileContent[$key][$lnct][$subkey] = $quoFix;
						}

						$lnct++;
					}

					fclose($fileRead);
				}
			}
		}
		print_r ($fileContent);

 

Now i want to compare the contents from array 1 and contents from array 2. The file contents are read line for line and placed into an array. $fileContent[0] is one array and $fileContent[1] is another array. I can't use array_diff() cause the column counts are different in each file...

 

any ideas here?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.