http://www.perlmonks.org?node_id=572210


in reply to CGI-Perl:: Location for saving the GD output

The web server's root and your OS's root are different. The web server won't look outside it's tree. If you write your image file to a location outside the server's DocumentRoot, your web server can't find it.

You can

  1. use the Alias directive to have a location point to some path outside the DocumentRoot
  2. allow the webserver process to write to some images directory inside the tree rooted at DocumentRoot
  3. use a CGI script to serve the image. Provide e.g. the tag
    <img src="/cgi-bin/image.pl?img=$tag.jpg">
    in the page referencing the image, and have it served thusly:
    #!/usr/bin/perl -T use CGI; use strict; my $q = new CGI; if($q->param('img') =~ /^([\w\.\-]+)$/) { # untaint my $file = $1; if(-f "/tmp/$file") { if(open(I,'<',"/tmp/$file")) { print $q->header(-content_type => "image/jpeg"); print while <I>; close I; exit; } } } print $q->header(-status => '404 Not found'); print "<h1>Not found.</h1>\n";
As for 1. and 2. - if you allow your web server to write to some path, you should limit the methods POST, PUT and DELETE for that directory (or location).

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: CGI-Perl:: Location for saving the GD output
by cool (Scribe) on Sep 10, 2006 at 17:40 UTC
    Dear Shmem,

    I tried the third one.. and is giving output but not as image.. its all Greek and Latin..

    Below are the lines of code, where I am generating the image..
    my $png_data = $im->png; #open (DISPLAY,"| display -") || die; print $query->p("$tag : Free energy curve of forward strand and shu +ffle sequence"); #open (DISPLAY,">/var/www/html/upload/$tag.jpg") ||error ($query, "C +ant open GRAPH:") ; open (DISPLAY,">/tmp/upload/$tag.jpg") ||error ($query, "Cant open +GRAPH:") ; binmode DISPLAY; print DISPLAY $png_data; close DISPLAY;
    and just to try with your piece of code, I am trying with this.
    open (I, '<', "/tmp/upload/$tag.jpg"); #print $query->header(-content_type => "image/png"); print while <I>; close I; exit;
    What u say ?? pl help!!
      open (DISPLAY,">/tmp/upload/$tag.jpg") ||error ($query, "Cant open +GRAPH:") ;

      Why are you appending .jpg as extension, if the image is PNG?

      binmode DISPLAY;

      Try using binmode I; for reading and binmode STDOUT for writing the file.

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        hi Shmem,

        I tried as following...

        #!/usr/bin/perl -w use CGI; my $query = new CGI; $query->header(); $query->start_html(); print "Content-type:image/x-png\n\n"; open (I, '<', "/tmp/upload/EMBOSS_001.png"); binmode I; print while <I>; close I; $query->endform(); $query->end_html();
        But it prints only some arbitory characters, not an image!!