Search code examples
phpmysqlmysqlimport

Using mysqlimport inside PHP script


I'm trying to import lot's of CSV files into a MySQL DB programmatically.

After some research, I found LOAD DATA, but it is not a possibility, as the server don't allow it.

Then, I found mysqlimport a viable alternative.

Here's what is happening:

I download lot's of CSV files from a FTP server and, one by one, I execute the folowing:

exec("ln " . $path . $csv_file. " " . $path . "CSVs.txt");
exec("mysqlimport -u root -ppass --local --ignore-lines=1 --columns=column1,column2 " .  $path . "CSVs.txt>" . $path . "_.txt");
unlink($path . "CSVs.txt");

First, I create a symlink of the file (as I didn't manage to use the file name different from the table name) Then, I execute the mysqlimport command, sending the output do a txt file. Finally, I remove the symlink.

When I run, the CSV temp file is created and deleted, but nothing apear on my DB, nor in the output txt.

If I echo the command and past in my terminal, it works flawlessly, but, in the exec, nothing happens.

I believe it has something to do with the quotes and backslashes, but couldn't manage to fix it.

Any help will be appreciated :P

EDIT: I already chmod**ed the **mysqlimpot bin… still didn't work!


Solution

  • It seem like PHP wasn't finding mysqlimport command. So, I had to use it's full path to make it work!