How can I store shapefile in PostgreSQL database as a table using PHP?

by mint   Last Updated May 17, 2018 23:22 PM

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:

  1. shell_exec()
  2. exec()
  3. system()

but did not work. Is there any other way to create a table from shape in PHP or run this command?



Answers 2


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".

user30184
user30184
June 01, 2014 12:35 PM

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\PostgreSQL\9.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\PostgreSQL\9.6\bin\shp2pgsql.exe\" -s 28992 -I -W 
latin1 D:\somefolder\someshape.shp someschema.example_shape | \"C:\Program 
Files\PostgreSQL\9.6\bin\psql.exe\" -U postgres -h localhost -d somedbname");
?>
Philip
Philip
May 17, 2018 22:59 PM

Related Questions


Access is Denied in cmd prompt shp2pgsql

Updated February 21, 2018 16:22 PM

Create blank table from unix shell

Updated May 20, 2015 21:09 PM



ArcGIS 10.2.2 Mxdperfstat

Updated April 12, 2015 20:09 PM