Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>How to upload CSV file using PHP (<strong>Working Code</strong>)</p> <p>Query Library </p> <pre><code>&lt;?php class query{ function mysql_query_string($string){ $enabled = true; $htmlspecialchars = false; # Convert special characters to HTML entities /**************************************************************** The translations performed are: '&amp;' (ampersand) becomes '&amp;amp;' '"' (double quote) becomes '&amp;quot;' when ENT_NOQUOTES is not set. ''' (single quote) becomes '&amp;#039;' only when ENT_QUOTES is set. '&lt;' (less than) becomes '&amp;lt;' '&gt;' (greater than) becomes '&amp;gt;' *****************************************************************/ if($htmlspecialchars){ # Convert special characters to HTML entities $string = htmlspecialchars($string, ENT_QUOTES); } else{ /**************************************************************** '"' (double quote) becomes '&amp;quot;' ''' (single quote) becomes '&amp;#039;' ****************************************************************/ //$string = str_replace('"',"&amp;quot;",$string); //$string = str_replace("'","&amp;#039;",$string); } if($enabled and gettype($string) == "string"){ # Escapes special characters in a string for use in a SQL statement return mysql_real_escape_string(trim($string)); } elseif($enabled and gettype($string) == "array"){ $ary_to_return = array(); foreach($string as $str){ $ary_to_return[]=mysql_real_escape_string(trim($str)); } return $ary_to_return; } else{ return trim($string); } } } ?&gt; </code></pre> <p>Call Csv Method</p> <pre><code> public function csvFileSubmitData(){ $this-&gt;load-&gt;library('query'); $query=new query(); $root = DIR_PATH.'public/administrator/csv/'; $fileToUpload= (isset($_FILES['fileToUpload']) and $_FILES['fileToUpload']['size'] &gt; 0 and $_FILES['fileToUpload']['error'] == 0) ? $_FILES['fileToUpload'] : ""; if(is_array($fileToUpload)){ # CHECK UPLOADED FILE 1 FOR VALIDATION $fileToUpload['name'] = str_replace(" ","_",$fileToUpload['name']); $fileToUpload['name'] = str_replace("&amp;","and",$fileToUpload['name']); # CHECK FILE TYPE IF IT IS IMAGE JPG,GIF,PNG ETC $fnarr = explode(".", $fileToUpload['name']); } $rand = rand(1000,10000); $filecsv = $rand."_".$fileToUpload['name']; $file1 = $root.$filecsv; move_uploaded_file($fileToUpload['tmp_name'],$file1); $fieldseparator = ","; $lineseparator = "\n"; $csvfile = $file1; $addauto = 0; $save = 0; $outputfile = "output.sql"; if(!file_exists($csvfile)) { echo "File not found. Make sure you specified the correct path.\n"; exit; } $file = fopen($csvfile,"r"); if(!$file) { echo "Error opening data file.\n"; exit; } $size = filesize($csvfile); if(!$size) { echo "File is empty.\n"; exit; } $csvcontent = fread($file,$size); fclose($file); $lines = 1; $queries = ""; $linearray = array(); $values = ""; $m =0; $linestext = split($lineseparator,$csvcontent); foreach($linestext as $line){ if($m++==0){ continue; } $lines++; $line = trim($line," \t"); if($line == ''){ break; } $linearray = explode($fieldseparator,$line); $topicname = $linearray[0]; $question = $linearray[1]; $answer1 = $linearray[2]; if(isset($linearray[1]) and $linearray[1] != ''){ $topicname = $query-&gt;mysql_query_string($linearray[0]); $question = $query-&gt;mysql_query_string($linearray[1]); $answer_type = $query-&gt;mysql_query_string($linearray[2]); } //Save Csv data in your table like this //query(insert into topics SET `topic`='".$topicname."',`question`='".$question."'); }} </code></pre> <p>If you are using Codeignitor Framework so this code is too easy to integrate ,No hard&amp;fast rule you can also use this code plain PHP as well as .....</p> <p>Thanx AbdulSamad</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload