ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I am trying (without much luck) to insert an image into a cgi script. I have RedHat 9 with Apache running. I am trying to create a script and I am inserting an image into the script.
I am placing the image in the here document. The page appears but the image does not... I have the image in the /var/www/cgi-bin directory. I have also tried placing the image in the / dir but to no avail.
If anyone has any ideas on what I am doing wrong could they let me know. Any help is appreciated!!
What do you mean exactly by "inserting an image into the script?"
It sounds like you are physically taking the binary data for an image and appending it to the end of a script. If that is the case, it's not going to work.
Generally what you would do to have a CGI script generate an image, would be to send the appropriate Content-Type, then print out the binary data. (Note, I said print out the binary data, not append the data. You don't say what language you are using for the CGI program, so how you do that exactly, I can't tell you.)
Take for example a CGI script written in C... I might do something like so:
// Disclaimer, this code is just off the top of my head and isn't
// guaranteed to work perfectly. I am probably forgetting some
// minor details here and there. :)
FILE* fp = fopen("someimage.jpg");
// Read the file 1 byte at a time and print it out...
// Could probably read bigger chunks, but this code
// is just for demonstration purposes.
fread(&ch, 1, 1, fp);
CGI scripting is done in Perl. In fact, files ending .cgi and most files in cgi-bin on the internet are perl scripts using the CGI module.
If you're using the CGI module then before you print any other data you should be sending the content type headers (as shown in the C example above, only using Perl like a normal person ) or the easy way:
After that you can use normal HTML code just by including it in print statements...including images. Try:
Bear in mind that if you want to use quote marks (") around the filenames like you would when using ONLY HTML then you must escape them so they don't intefere with perl:
Forgot to mention, if you knew all that but meant that the script simply can't find the image, then try putting it in the parent directory from the cgi-bin and reference it by "../image.png" (using the two periods to indicate parent directory).
Forgot also if you're using HTML within the perl script then you still need to send <HTML> and <BODY> (print("<html><body>") as you would with a normal HTML document.
CGI actually doesn't need to be done in specific language. Perl is the most common language, but I'm most familar with C, and figured I'd have less chance of messing something up completely with an example in that.
My example is actually an example that can be called from another html page by embedding it in a page like so:
I was under the impression that you were trying to do some sort of validation before people could see images. If that is the case, you would want to put the image files somewhere that they wouldn't be directly accessible to an HTTP user, but that a CGI script could access and serve similar to my example.
As an example, I once worked on a photo album app that allowed people to log in to see photos that had been taken by a camera installed in their home. In this case, we obviously wouldn't want just anyone to be able to see the photos due to privacy issues... For this we stored the images in a database, but they could just as easily have been stored in a folder that was outside of the scope of what the web server would allow people to see. (e.g. if a user has a ~/public_html folder that can be seen from a web page, you could put the actual images in ~/images, which isn't accessible directly from the web server.)
If my assumption of what you were trying to do was wrong, however, just having your CGI print out an HTML image tag would be sufficient. And thinking about it, my assumption was most likely wrong. My mind was still thinking about how I used CGI scripts to create images. Using something like CGI or php to return an image content-type can also allow you to do other interesting things like create images "on the fly" based on parameters you pass in.
What I meant by inserting a image into the script was that I am writing a perl script with CGI and I am trying to insert an image onto a webpage. I am inserting the image in a here document but I dont know for certain if this will work?? Ive tried placing the image in the root directory, the cgi-bin, parent dir to cgi-bin but it still doesnt display.. do I need to incorporate any module into my script or change permissions of the image..
I have enclosed a snippet of code to show what I am doing..could it be the Content - Type needs to change as I am inserting an image?
Any more help is greatly appreciated.
use DBI; #uses the DBI module
use CGI qw(:standard); #uses CGI module
Thanks for the example..is it possible to insert the image within the here document like I have done above, instead of printing out all the individual print statements?? Do I need to change any of my code??
Ok... I've only done a few CGI apps, but have done a lot of server-side scripting stuff with php/asp. But, I'm not quite sure what you mean by "within the here document." What are you referring to as a "here" document exactly?
Ahhh, ok. I've seen that used, but didn't know the term for it.
Anyway, what you want isn't really feasible. HTML documents are text only, and your CGI script is basically creating an HTML document. In HTML, to display an image, you use an <IMG> tag, as has been shown in examples above already. You cannot have binary image data embedded directly into an HTML document.
If you are trying to create an image "on the fly", or are trying to display images from some sort of protected directory that a web browser doesn't have direct access too, then you will need a separate CGI script that returns only the image data specified by the Content-Type, and then in your HTML, you use that CGI script as the src attribute of the IMG tag.
If you aren't trying to protect the image, and you aren't generating it on the fly, you really just want to use the <IMG> tag. If you are having problems understanding that, you will need to be more specific with what exactly you are trying to do.