Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>We used this awhile ago, and it works just fine. Just watch your file and directory permissions. csv_upload_mysql_conf.inc is just the DB link. This will parse multiple files at once, and put them in a table called import. Update accordingly.</p> <pre><code>&lt;?php /* The conf file */ include_once "csv_upload_mysql_conf.inc"; $php_self = $_SERVER['PHP_SELF']; $file_open = 0; $file_exts = array ( 'csv'); #Our Form..... $form = &lt;&lt;&lt; EOFFORM &lt;div align='center' style='border: 1px solid #CCC; background-color: #FAFAFA;padding: 10px; color: #006699; width: 620px; font-family: palatino, verdana, arial, sans-serif;' &gt; &lt;table align=center style='border: 1px solid #CCC; background-color: #FFF;padding: 20px; color: #006699;' cellspacing=1&gt;&lt;tbody&gt; &lt;tr&gt;&lt;td&gt; &lt;form enctype='multipart/form-data' action='$php_self' method='post'&gt;&lt;input type='hidden' name='MAX_FILE_SIZE' value='2000000' /&gt;&lt;input type='hidden' name='selected' value='yes' /&gt; Selected file: &lt;input name='userfile[]' type='file' id='userfile[]' multiple='' onChange='makeFileList();' /&gt;&lt;br /&gt;&lt;br /&gt;&lt;input type='submit' value='Upload CSV' /&gt; &lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt; &lt;p&gt; &lt;strong&gt;Files You Selected:&lt;/strong&gt; &lt;/p&gt; &lt;ul id="fileList"&gt;&lt;li&gt;No Files Selected&lt;/li&gt;&lt;/ul&gt; &lt;script type='text/javascript'&gt; function makeFileList() { var input = document.getElementById('userfile[]'); var ul = document.getElementById('fileList'); while (ul.hasChildNodes()) { ul.removeChild(ul.firstChild); } for (var i = 0; i &lt; input.files.length; i++) { var li = document.createElement('li'); li.innerHTML = input.files[i].name; ul.appendChild(li); } if(!ul.hasChildNodes()) { var li = document.createElement('li'); li.innerHTML = 'No Files Selected'; ul.appendChild(li); } } &lt;/script&gt; EOFFORM; #End Form; if(!isset($_POST['selected'])){ echo "$form"; } elseif($_POST['selected'] == "yes"){ $uploaddir = 'uploads/'; if(count($_FILES['userfile']['name'])) { foreach ($_FILES['userfile']['name'] as $key =&gt; $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES['userfile']['tmp_name'][$key]; $name = $_FILES['userfile']['name'][$key]; $f_type = trim(strtolower(end(explode('.', $name)))); if (!in_array($f_type, $file_exts)) die("Sorry, $f_type files not allowed"); } $uploadfile = $uploaddir . $name; if (! file_exists($uploadfile)) { if (move_uploaded_file($tmp_name, $uploadfile)) { print "File is valid, and was successfully uploaded. "; $flag = 1; chmod($uploadfile, 0777); } else { print "File Upload Failed. "; $flag = 0; } $flag = 1; if ($flag == 1) { echo "\n parsing Data..."; flush(); if (file_exists($uploadfile)) { $fp = fopen($uploadfile, 'r') or die (" Can't open the file"); $fileopen = 1; $length = calculate_length($uploadfile); } $replace = "REPLACE"; $field_terminater = ","; $enclose_option = 1; $enclosed = '"'; $escaped = '\\\\'; $line_terminator = 1; $local_option = 1; $sql_query = 'LOAD DATA'; if ($local_option == "1") { $sql_query .= ' LOCAL'; } $sql_query .= ' INFILE \'' . $uploadfile . '\''; if (!empty($replace)) { $sql_query .= ' ' . $replace; } $sql_query .= ' INTO TABLE ' . "`import`"; if (isset($field_terminater)) { $sql_query .= ' FIELDS TERMINATED BY \'' . $field_terminater . '\''; } if (isset($enclose_option) &amp;&amp; strlen($enclose_option) &gt; 0) { $sql_query .= ' OPTIONALLY'; } if (strlen($enclosed) &gt; 0) { $sql_query .= ' ENCLOSED BY \'' . $enclosed . '\''; } if (strlen($escaped) &gt; 0) { $sql_query .= ' ESCAPED BY \'' . $escaped . '\''; } if (strlen($line_terminator) &gt; 0){ $sql_query .= ' LINES TERMINATED BY \'' . '\r\n' . '\''; } $result = mysql_query ($sql_query); echo mysql_error() ; if(mysql_affected_rows() &gt; 1) { echo " &lt;div align=center&gt;&lt;b&gt;&lt;font color=#66CC33&gt;The csv data was added.&lt;/font&gt;&lt;/div&gt; "; } else { error_log(mysql_error()); echo " &lt;div align=center&gt;&lt;b&gt;&lt;font color=#E96B10&gt; Couldn't enter the data to db &lt;/font&gt;&lt;/div&gt;"; } if ($file_open ==1) { fclose($fp) or die("Couldn't close the file"); } } } } echo "&lt;meta http-equiv='refresh' content='0; url=index.php'&gt;"; } } function calculate_length($fp) { $length = 1000; $array = file($fp); for($i=0;$i&lt;count($array);$i++) { if ($length &lt; strlen($array[$i])) { $length = strlen($array[$i]); } } unset($array); return $length; } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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