Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie question, display image using perl/cgi 4

Status
Not open for further replies.

frankli

Technical User
Apr 6, 2005
44
CA
Hi all,

I have a problem when printing a simple html over cgi

in html i can get the image "test_logo.gif" displayed on the screen,
<html>
<body>
<a href=" src="test_logo.gif" alt="Test Logo"></a>
</body>
</html>

however in perl/cgi, the image can not be displayed,
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print <<EOF;
<html>
<body>
<a href=" src="test_logo.gif" alt="Test Logo"></a>
</body>
</html>
EOF

the path of the image is current path.

please point out what I did wrong. Thanks.

Regards!
 
yes, for testing, I put them all under serverroot/cgi-bin/.
 
run this:-

#!/usr/bin/perl

print "Content-type: text/html\n\n";

print <<EOF;
<html>
<body>
<a href=" src="test_logo.gif" alt="Test Logo"></a>
</body>
</html>
EOF
# end of script [red]add this line - IMPORTANT !!![/red]

i assume you have chmod 755 yourscript.cgi !?


Kind Regards
Duncan
 
yes, the test_logo.gif and this test.cgi are in the same directory ../cgi-bin/

I tried as Duncan suggested, still, I got everything but the image, it appreared as the cross mark and looks like the image is missing, but I m pretty sure that I did't make mistake on the path and image file. How come.

Thanks and regards!
 
add print `ls`; to your script - to reveal the files your cgi can see

(these are backticks - not single quotation marks)


Kind Regards
Duncan
 
Hi, yes I can see all of them, filename in my cgi-bin dir, and test_log.gif and test.cgi are two of them.

Any of you guys can run this small script and see if you can get the image, you can use any image.

Thanks and regards!
 
Thanks for the post.

No I cannot display it directly in the browser using

after I moved all the gif file to htdocs or htdocs/images and change the path in the script, it works, but not under cgi-bin or cgi-bin/images <I remembered I used to do that way though>, checked the httpd log seems it reports the web server was trying to execute the file instead of loading it, not sure if it is the solution or there is any way I can put the image files in cgi-bin/images .


Regards!
 
the server should not allow images to be displayed directly from the cgi-bin, that is the entire problem, which you solved by moving the image out of the cgi-bin.
 
nice one Kevin! I would not have guessed that!!!


Kind Regards
Duncan
 
Its all just html, regardless of if a perl script spits it out or a static html page does. This will not work:

<img src=
so why should this if the image is in the cgi-bin:

<img src=frog.jpg>

They are both esentially the same, even if one is a relative URL and one is an absolute URL.
 
Hi Kevin. That's not quite what i meant - i understand absolute and relative URL's - i just didn't realise you could not have a picture in the cgi-bin


Kind Regards
Duncan
 
You could have a picture in cgi-bin but you wouldn't be able to ask the webserver for it. You would have to call a server-side script of some kind (probably cgi) to provide the http header and data.
I guess it's irrelevant to this discussion but I just thought I'd point it out for general reference.


Trojan.
 
Hi Trojan

Not irrelevant at all - I genuinely have no idea why that would not work! Please can you explain a bit further?


Kind Regards
Duncan
 
Yep,
The reason that you can't link to a picture is (as I guess is obvious) because the webserver will see anything in the cgi-bin directory as a script to execute.
If, however, you have a script called (say) image.pl, it could in turn open "image.jpeg" and echo the contents straight back to the browser (after supplying the appropriate http header).
Since the perl script is reading the file and not the webserver, it is not seen as something to execute.
Does that make any sense?


Trojan.
 
I guess the perl scipt might be something like:
Code:
#!/usr/bin/perl
open FH, "image.jpg" or handle_error();
print "Content-type: image/jpeg\n\n";
while(<>) { print; }
close FH;
You're just writing the http header type to say that it's an image and then just echoing the image data back to the browser.


Trojan.
 
Makes perfect sense! Thanks dude! I guess i kind of assumed that the Perl scripts would execute due to their extension - .pl or .cgi - not just because of there location. I am not one to pretend i know how something works - until i am 99.9% sure i do. (and these days there is a lot of misinformation out there). From looking at, and twiddling with, the Apache httpd.conf i must have got the wrong idea. Thank you again.


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top