LOAD DATA LOCAL INFILE forbidden in… PHP

后端 未结 12 1090
无人共我
无人共我 2020-12-01 10:38

I am trying to use LOAD DATA INFILE to insert some records into a table. Unfortunately, it\'s not working.

Here are some details

If I use this i

相关标签:
12条回答
  • 2020-12-01 11:04

    had this problem today and solved it by setting the following in php.ini

    mysqli.allow_local_infile = On
    
    0 讨论(0)
  • 2020-12-01 11:04

    I didn't get the exact error you get, but you need no ensure the following:

    Enable by adding to your my.cnf:

    [mysql]
    local-infile=1
    
    [mysqld]
    local-infile=1
    

    Tell the connection in PHP that it may use LOCAL INFILE

    Using mysql:

    mysql_connect(server,user,code,false,128); // 128 enables LOCAL INFILE
    mysql_select_db(database);
    

    Using mysqli:

    $conn = mysqli_init();
    mysqli_options($conn, MYSQLI_OPT_LOCAL_INFILE, true);
    mysqli_real_connect($conn,server,user,code,database);
    

    Give MySQL user FILE permission

    When using LOCAL this shouldn't be necessary, though. LOCAL says that the file is located on the client server (where you have PHP is installed), otherwise it looks at server location (where MySQL is installed).

    GRANT FILE ON *.* TO 'mysql_user'@'localhost' 
    
    0 讨论(0)
  • 2020-12-01 11:10

    In php.ini you insert "mysqli.allow_local_infile = On", and restart

    0 讨论(0)
  • 2020-12-01 11:12

    Easier work around is to use exec()

    exec("mysql -u myuser -pMyPass -e \"USE mydb;TRUNCATE mytable;LOAD DATA INFILE '" . $file . "' IGNORE  INTO TABLE mytable;\"; ");
    
    0 讨论(0)
  • 2020-12-01 11:12

    2019+ relevant answer with a bit more background:

    In PHP >7.2.16 and >7.3.3 the default ini configuration of mysqli.allow_local_infile, which controls this, changed from '1' to '0' (so it is now disabled by default).

    This directive is only configurable via PHP_INI_SYSTEM so ini_set() will not work.

    The only option is to add the following directive to your php.ini file, not forgetting to reload apache.

    [MySQLi]
    mysqli.allow_local_infile = On
    
    0 讨论(0)
  • 2020-12-01 11:12

    LOAD DATA LOCAL INFILE executes regardless of the warnings. it works on mysql client since it allows the execution of queries, ignoring warnings. Though it later prints out the warnings. It refuses in PHP though because a warning will halt the script.

    0 讨论(0)
提交回复
热议问题