Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Me and my collegue are using the shp2pgsql and psql commands in php code (a Drupal module to be precise).

We put these commands in a php variable and use the exec() command to execute them. This works very well but now we have to use these operations on an external server which doesn't support the exec() command.

Is there any workaround or solution for this matter?

Thanks in advance.

share|improve this question
    
the server can not execute anything at all? You'll have to ask the administrator then to add that program to the server – nickves Apr 11 '13 at 8:59

I don't know if this is a Drupal thing or not, BUT I can tell you why under PHP exec would not be allowed.

Many shared hosts run PHP in safe mode. This is done primarily to prevent one account using the OS facilities available on the running server, being used to gather information from other accounts on the same server.

Anything that can be used to run external commands (Particularly OS commands that may run with root privileges) or view files in someone else's folder, is generally disabled.

On a shared hosting server, the web-server (Usually something like Apache) is often run using the same user (often something like www-data).

Beacuse this user is for all intents owned by the web server, then ANYTHING the web server has access to is technically readable by any account running PHP scripts in the web-servers context.

Safe mode PHP was designed to prevent this scenario occurring by allowing system admins to list in their PHP configuration, the names of methods and functions they deem to be unsafe on that particular server configuration.

There's a general school of thought, that ANY PHP function that allows external software to be run and controlled from a PHP script, should always be on the list. After that, directory functions and file functions are often added next.

There is also something called a "Safe mode folder", which means that some commands that are considered dangerous can be enabled, but can be restricted so they ONLY operate in the folder your scripts live in.

The final part of the puzzle, is the location of runnable external programs. Many web server hosts are configured to ONLY allow programs to be run from a certain folder. I'd be very sure that a tool like "shp2pgsql" would not be installed by default in any of these normal program execution folders.

This would lead me to believe that you've likely uploaded the program yourself, into your own server home folder, and by that it's very likely that even if you could enable the exec function, the chances of running your program outside the normal OS folders is still going to fail anyway.

Given that your running "shp2pgsql", my guess is that your trying to insert shape files into a postgres spatial database.

If I'm correct about this, then a better way to do it might be to convert the shape files to a more web friendly format such as geoJSON, then use a simple PHP script that talks directly to the database to upload those files into tables for further processing.

Current versions of Postgres have top class features for handling JSON data, and postgis can use SQL statements such as 'ST_GeomFromGeoJSON'

http://postgis.net/docs/ST_GeomFromGeoJSON.html

GeomFromGeoJson can take a GeoJson file, which you could easily read from your server using php's "get_file_contents" or "file" functions, and insert it directly into your spatial data table.

Beacuse GeoJSON is a text file, you can convert them on your workstation from Shp to GeoJson (GDAL should be able to help you here) , then just FTP them to your server and run your script.

If you must use "shp2pgsql" to do the task, then the only way your likely to get it working is to contact the company managing the web host for you, and ask if there is any way they can enable it for you.

On a final note, if your running on a dedicated server, IE: one that your paying to have exclusive access too, and no one shares with you, then it's highly likely that you'll be able to change the configuration for things like "safe mode PHP" yourself.

share|improve this answer
    
+1 for php directly talking to postgresql/postgis, if you can't edit php.ini – Andreas Müller Feb 7 at 17:06

I'm unsure but is this a possibility to allow the exec shp2pgsql and exec psql commands only, based on who is logged in?

http://www.juniper.net/techpubs/software/junos/junos95/swconfig-access-privilege/id-10521195.html

Any one has experience on this? Thank you.

share|improve this answer
    
Please add this comment to your original question, it's not good practice to use answers to ask further questions. – shawty Feb 8 at 11:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.