I want to create a table from a shapefile in PostgreSQL. To do that, I can run this command in cmd:
shp2pgsql -s 4326 -W LATIN1 E:\Any\THA_adm public.test123 | psql -h localhost -d DATA -U postgres -w 123;
This works, but I want to run it from PHP. How can I do that, I tried PHP functions:
- shell_exec()
- exec()
- system()
but did not work. Is there any other way to create a table from shape in PHP or run this command?
-
Why did exec() not work? What was the error? Did you do something similar than in here stackoverflow.com/questions/18259089/…?user30184– user301842014年06月01日 10:42:44 +00:00Commented Jun 1, 2014 at 10:42
-
Finally its works, Don't know what was the problemmint– mint2014年06月01日 11:25:37 +00:00Commented Jun 1, 2014 at 11:25
-
Nice. I copy my comment as an answer then.user30184– user301842014年06月01日 11:25:59 +00:00Commented Jun 1, 2014 at 11:25
-
exec() works if used like in this answer stackoverflow.com/questions/18259089/…user30184– user301842014年06月01日 11:29:05 +00:00Commented Jun 1, 2014 at 11:29
-
actually exec() worked, I don't know what was the problem :)mint– mint2014年06月01日 11:40:20 +00:00Commented Jun 1, 2014 at 11:40
3 Answers 3
I experienced exactly the same problem (command works in cmd but nothing when called via PHP). What helped me a lot was to retrieve the error message (by adding: 2>&1'& $output). MY command looked something like:
?php
exec("\"C:\Program Files\PostgreSQL9円.6\bin\shp2pgsql.exe\" -s 28992 -I -W
latin1 D:\somefolder\someshape.shp someschema.example_shape | psql -U postgres -h localhost -d somedbname 2>&1", $output);
foreach ($output as $key => $value)
{echo $value;}
?>
It returned "'psql' is not recognized as an internal or external command,operable program or batch file." My fix was to define the full path of the psql executable in the command like this:
<?php
exec("\"C:\Program Files\PostgreSQL9円.6\bin\shp2pgsql.exe\" -s 28992 -I -W
latin1 D:\somefolder\someshape.shp someschema.example_shape | \"C:\Program
Files\PostgreSQL9円.6\bin\psql.exe\" -U postgres -h localhost -d somedbname");
?>
So the answer was "by using exec() as in an example showed in the answer to question https://stackoverflow.com/questions/18259089/execute-gdal-translate-in-php-using-exec".
-
This link doesn't work, what's the answer please ?Geo-x– Geo-x2017年01月10日 10:57:19 +00:00Commented Jan 10, 2017 at 10:57
-
Don't remember. Check the links on that deleted page.user30184– user301842017年01月10日 11:03:35 +00:00Commented Jan 10, 2017 at 11:03
-
How can I do that ? GSE say : "This question was removed from Stack Overflow for reasons of moderation."Geo-x– Geo-x2017年01月10日 11:12:22 +00:00Commented Jan 10, 2017 at 11:12
-
I see there "Here are some similar questions that might be relevant" and a bunch of links.user30184– user301842017年01月10日 11:14:27 +00:00Commented Jan 10, 2017 at 11:14
-
2Perfect, I used this answer : stackoverflow.com/questions/17914402/…Geo-x– Geo-x2017年01月10日 13:52:55 +00:00Commented Jan 10, 2017 at 13:52
As I wrote in this answer, a native PHP alternative to using evil eval()
and shp2pgsql utility would be my PHP Shapefile library, that can read and write any ESRI Shapefile natively in PHP.
It is a free, open source, native PHP library, does not require any third party dependency and is actively maintained.
Explore related questions
See similar questions with these tags.