使用postgresql中的复制功能和ftp服务器

时间:2017-03-13 14:14:49

标签: php sql postgresql ftp ftp-server

我需要使用postgresql的复制查询,我的复制查询在下面(我用php运行)

create temp table tmp_food (like food); -- create temporary table like food

copy tmp_food from 'd:\testingfood.csv' delimiter '|' csv;  -- copy to temp table

insert into food (food_id, food_name)   -- insert into food from temp table
select food_id, food_name
from tmp_food
on conflict (food_id) do                -- update instead of error
update set food_name = excluded.food_name;

drop table tmp_food;                    -- drop temp table

但问题是,我需要从ftp服务器复制它,所以我需要更改此行

  

从'ftp \ somefolder'分隔符'|'复制tmp_food CSV; - 复制到临时表

顺便说一下这是我读取csv的代码,但我需要的是获取路径并将其与我的php结合

$ftp_server = "127.0.0.1";
$ftp_username = "alex";
$ftp_userpass = "alex";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
echo "success connect to FTP";
$contents = fread($handle, filesize($filename));
echo $contents;

// close connection
ftp_close($ftp_conn); 

1 个答案:

答案 0 :(得分:2)

我可以看到以下选项:

  • 将文件读入PHP数组并使用pg_copy_from使用PostgreSQL COPY FROM STDIN将其加载到临时表中。

  • 使用强大的语言编写函数,例如PL/PerlU,在执行COPY之前为您执行文件传输。