1

I'm trying to open a file and IMPORT it to PSQL

$sql ="COPY r_vehicle_submodel FROM '".$filePath."' DELIMITER ',' CSV";

However it display me error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[58P01]: Undefined file: 7 ERROR: could not open file "C:\xampp...\uploads\SearchTreeCZ.csv" for reading: No such file or directory' in

I try to copy this path to clipboard and open file and i open it correctly so file is there for sure.

After that I was thinking if just PHP don't have permission to this file/ folder so I write something really simple:

$file = fopen($filePath,'r');
echo fread($file,filesize($filePath));  

Which open me my file correctly, I also try to scandir this path successfully.

echo $filepath display: C:\xampp\htdocs\xampp\utilities\searchTreeImport\uploads\SearchTreeCZ.csv

Guys do you have any idea where can be a problem and WHY I can't open this file?

Thanks

EDIT: This QUERY works (MySQL)

$sql = "LOAD DATA INFILE '".$filePath."' 
        INTO TABLE r_vehicle_submodel 
        CHARACTER SET UTF8
        FIELDS TERMINATED BY ',' 
        ENCLOSED BY '\"' 
        IGNORE 1 LINES ";   

This QUERY doesn't work (PSQL)

$sql ="COPY r_vehicle_submodel FROM '".$filePath."' DELIMITER ',' CSV";

So i expect problem can be in my $sql query

Andurit
  • 5,612
  • 14
  • 69
  • 121
  • 1
    What value is inside $filePath ? Please tell. – Nana Partykar Nov 12 '15 at 11:57
  • hey @NanaPartykar added to my question :) – Andurit Nov 12 '15 at 12:30
  • So, when you type this `C:\xampp\htdocs\xampp\utilities\searchTreeImport\uploads\SearchTreeCZ.csv`. File is opening ?? – Nana Partykar Nov 12 '15 at 12:35
  • Correclt,y if I copy / paste this URL to total comander or some other explorer file is opening – Andurit Nov 12 '15 at 12:35
  • @NanaPartykar I expect problem will be in my query, please check my EDIT :) – Andurit Nov 12 '15 at 12:44
  • 1
    You `COPY` command will presumably be run under whatever user the PostgreSQL server process is running as. Is the PostgreSQL server process on the same machine, and does the user it's running as have access to the file? The PHP process isn't relevant, as PHP is just handing the work over to the PostgreSQL server. – Matt Gibson Nov 12 '15 at 13:08
  • Hi @MattGibson ,PHP server is actualy runing on "localhost" however PostgreSQL server is runing on server on some remote IP. Also Local OS is WINDOWS, however remote is CentOS – Andurit Nov 12 '15 at 13:12
  • 1
    Well, there's your reason. Your COPY command is running (I'd guess as the PostgreSQL server process user) on the remote CentOS server, which won't have access to the file it's trying to copy, which is just sitting on your C: drive. You'll either need to find some way of copying the file to a location your CentOS box can see, or find another way of doing the import. If it's not much data, perhaps read it in PHP and INSERT it row-by-row to the server. – Matt Gibson Nov 12 '15 at 13:15
  • [This earlier question](http://stackoverflow.com/questions/18510954/import-csv-file-into-postgres-via-php-in-web-page) is similar and also mentions likely problems with COPY only being available to PostgreSQL superuser accounts. – Matt Gibson Nov 12 '15 at 13:21
  • Thanks :) this actualy help me solve my problem! I really appriciate your help – Andurit Nov 12 '15 at 15:46

0 Answers0