Hello guys

this forum helped me alone for completing my projects that is y i am going to post one article hope you like it.........


1->create an excel sheet and enter data in that then at the time of saving saving it as a csv format and below is the code that will going to work perfectly without any problem.

function findexts ($filename) 
 { 
 $filename = strtolower($filename) ; 
 $exts = split("[/\\.]", $filename) ; 
 $n = count($exts)-1; 
 $exts = $exts[$n]; 
 return $exts; 
 } 
 
if ($_FILES["file"]["size"] < 200000)
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
	   $path = "upload/" . $_FILES["file"]["name"];
	   	
	  }
		if(findexts($path) == csv){
		$file=file_get_contents($path);
		$rows=explode("\n", $file); 
		$data = array();
		print_r($rows);
		$count =count($rows)-1;
		for($i=1;$i<$count;$i++){	
		$sql = "insert into tblbooks(field1,field2,field3)values('".str_replace(",","','",$rows[$i])."')";
		
		$DB_site->query($sql);
		}
		unlink($path);
		unset($rows);
		}
		else{
			echo "invalid file";
			error_reporting(0);
			unlink($path);
		}
	}
}
else{
  echo "Invalid file";
}

i hope you like it

i think this example may help you

//this is where the creating of the csv takes place
$cvsData = $fn . "," . $ln . "," . $address . "," . $xyz . "," . $asd ."\n";

then we open the file:
$fp = fopen("formTest.csv","a"); // $fp is now the file pointer to file $filename

And then we write the form contents to the file:
if($fp){
fwrite($fp,$cvsData); // Write information to the file

fclose($fp); // Close the file
And close the connection or file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.