7

I'm trying to upload a csv file to the table users in Laravel.

This is my php script:

private function _import_csv($path, $filename)
    {

        $csv = $path . $filename;
        $query = sprintf("LOAD DATA local INFILE '%s' INTO TABLE users FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\\n' IGNORE 0 LINES (`firstname`, `lastname`, `username`, `gender`, `email`, `country`, `ethnicity`, `education`  )", addslashes($csv));
    return DB::connection()->getpdo()->exec($query);

}



public function uploadUsers(){


    if (Input::hasFile('fileInput')){

        $file = Input::file('fileInput');
        $name = time() . '-' . $file->getClientOriginalName();

        $path = 'public/uploads/'.date('Y/m/');

        $file->move($path, $name);
        $this->_import_csv($path, $name);

    }

    return Response::json(["success"=>true]);
}

But I'm getting this error:

{"error":{"type":"ErrorException","message":"PDO::exec(): LOAD DATA LOCAL INFILE forbidden","file":"C:\\wamp\\www\\lc2\\laravel\\app\\controllers\\UsersController.php","line":224}}

I don't know whats wrong. Can anybody please help me?

2 Answers 2

25

Please try it

Set PDO Connection options

Set attribute PDO::MYSQL_ATTR_LOCAL_INFILE in database.php

'mysql' => array(
            ....
            'options'    => [PDO::MYSQL_ATTR_LOCAL_INFILE=>true],
        ),
Sign up to request clarification or add additional context in comments.

Comments

-3

Try to clean the query string before the sprintf function.

Comments

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.