1

How to upload CSV data to wordpress custom table wp_sonali_data. Bellow is the code what I'm trying. But no luck please help

Here is my custom table column

id
brcode
brname
dist

Here is my uploader

   <form method="post" action="">
<p>Choose your CSV file</p><br />
<input type="file" name="file" />
<input type="submit" name="submit" value="submit"/>

</form>


<?php

    //Upload CSV File
    if (isset($_POST['submit'])) {

    global $wordpress,$wpdb;
        $datafile= $_FILES['file']['tmp_name'];
        $file=$upload_dir['basedir'].'/'.$_FILES['file']['name'];
        $fileurl=$upload_dir['baseurl'].'/'.$_FILES['file']['name'];
        if (!move_uploaded_file($_FILES['file']['tmp_name'],$file)){
            print_r('Failed to move uploaded file.');
            }
        $sql="
        LOAD DATA LOCAL INFILE '".$fileurl."' INTO TABLE wp_sonali_data
        FIELDS TERMINATED BY ',' 
        LINES TERMINATED BY '\r\n'
        (id,brcode,brname,dist);
        ";
        $query = $wpdb->query($sql);
}

Error

Output: 'Failed to move uploaded file.'
3
  • why not use LOAD DATA INFILE ? Commented Jun 6, 2016 at 9:00
  • @splash58 can you please show me an example? Commented Jun 6, 2016 at 9:01
  • i've written an example as an answer Commented Jun 6, 2016 at 9:05

1 Answer 1

3
LOAD DATA INFILE "$_FILES['upload_csv']['tmp_name']"
INTO TABLE wp_sonali_data
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
("id", "brname", "brcode", "dist") 
Sign up to request clarification or add additional context in comments.

5 Comments

can you please modify my code with your example. Thanks !
unfortunately, i don't know wp db library. But i think it has method like query($string)
no offence @Firefog but if you don't understand his code, you should be reading up on SQL queries and learning what you need to pass through the values and work out which of your values fit in which parts of the SQL query statement
I have update my question with this method but Failed to move uploaded file. error output
I hope it's not due to the query :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.