I just read from this question that the OP was

using Mapscript and C# to generate and display a Mapfile

I was wondering if it is really possible to use MapScript to generate a Mapfile? And if so, is there an example of doing so with PHP MapScript?

P.S. I am new to MapServer, and I feel that manually writing every mapfile's is a pain even if I learn all its grammar. It would be nice if a bunch of mapfiles can be generated from a program by varying certain parameters.

If MapScript isn't the solution, is there any other way to generate mapfiles programmatically?

P.S.2. This post asks a similar question about doing it in python, but there seems to be no solution in the answers.

Thanks in advance,

--- EDIT ---

Just to clarify, I am interested in writing/serialization of Mapscript objects into a mapfile (in PHP), not writing a mapfile verbatim via a programming languages such as PHP or C++. From the SO question quoted, this seems possible in C#. I was asking whether there are specific examples of actually doing that (preferably in PHP).

  • Do you want to write out Mapfile using PHP? Or do you want to dynamically manipulate existing Mapfiles with PHP? Since Mapfile are text files (not binary) it is easily possible to write out such files as batch process. But if you need to manipulate Mapfile look at the PHP MapScript API: mapserver.org/mapscript/php/by_example.html
    – webrian
    Commented Aug 14, 2014 at 7:41
  • @webrian I am really interested in writing out Mapfiles using PHP. I know that MapScript could be used to manipulate existing mapfiles (probably as designed to), but I just want the basics. I imagine that having a programming language like PHP generate an mapfile can save me from losing time in making and correcting errors in writing the mapfiles.
    – tinlyx
    Commented Aug 14, 2014 at 9:42

1 Answer 1


Ok, this is not really a GIS related but rather a programming related question. Anyway, for your intended purpose you can use almost any language you like. Here is a very, very simple PHP script based on the example Mapfile from the documentation at http://www.mapserver.org/mapscript/php/by_example.html. In this script only the variable DATA is altered:


foreach(['map1', 'map2', 'map3'] as $mapname) {

    $tpl = "NAME \"Europe in purple\"
SIZE 400 400
SYMBOLSET \"/var/www/html/maps/symbols/symbols.sym\"
EXTENT -5696501 1923039 5696501 11022882
SHAPEPATH \"/var/www/html/maps/data\"

    IMAGEPATH \"/var/www/html/maps/tmp/\"
    IMAGEURL \"/tmp/\"

    NAME \"Europe\"
    DATA \"$mapname\"
            COLOR 110 50 100
            OUTLINECOLOR 200 200 200
            SYMBOL 0

    // Write it to a file
    $handle = fopen("mapfile_$mapname", 'a');
    fwrite($handle, $tpl);



Run the script with php script.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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