diff --git a/core/Db/BatchInsert.php b/core/Db/BatchInsert.php index 254e03d2d46..cf3918580cc 100644 --- a/core/Db/BatchInsert.php +++ b/core/Db/BatchInsert.php @@ -56,12 +56,14 @@ public static function tableInsertBatchIterate($tableName, $fields, $values, $ig */ public static function tableInsertBatch($tableName, $fields, $values, $throwException = false) { - $filePath = StaticContainer::get('path.tmp') . '/assets/' . $tableName . '-' . Common::generateUniqId() . '.csv'; - $loadDataInfileEnabled = Config::getInstance()->General['enable_load_data_infile']; if ($loadDataInfileEnabled && Db::get()->hasBulkLoader()) { + + $path = self::getBestPathForLoadData(); + $filePath = $path . $tableName . '-' . Common::generateUniqId() . '.csv'; + try { $fileSpec = array( 'delim' => "\t", @@ -94,17 +96,36 @@ public static function tableInsertBatch($tableName, $fields, $values, $throwExce throw $e; } } - } - // if all else fails, fallback to a series of INSERTs - if(file_exists($filePath)){ - @unlink($filePath); + // if all else fails, fallback to a series of INSERTs + if (file_exists($filePath)) { + @unlink($filePath); + } } - + self::tableInsertBatchIterate($tableName, $fields, $values); + return false; } + private static function getBestPathForLoadData() + { + try { + $path = Db::fetchOne('SELECT @@secure_file_priv'); // was introduced in 5.0.38 + } catch (Exception $e) { + // we do not rethrow exception as an error is expected if MySQL is < 5.0.38 + // in this case tableInsertBatch might still work + } + + if (empty($path) || !is_dir($path) || !is_writable($path)) { + $path = StaticContainer::get('path.tmp') . '/assets/'; + } elseif (!Common::stringEndsWith($path, '/')) { + $path .= '/'; + } + + return $path; + } + /** * Batch insert into table from CSV (or other delimited) file. *