cgi script to print all .png files on a single page


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cgi script to print all .png files on a single page
# 1  
Old 06-04-2010
cgi script to print all .png files on a single page

Hi guys

I'm relativley new to Perl, and have not touched html before, im trying to write a cgi script that prints all images on a single html page from a given directory. Im using perl to gather stats, rrdtool to update and create graphs now i just need to print these graphs all onto 1 index.cgi page.

The page appears but no images

I need to do it dynamically as new graphs will appear in the future, otherwise i could just add them 1 by 1.

Heres the code i have so far, can anyone suggest if and what im doing wrong here?

Code:
#!/usr/bin/perl -w

use strict;
use CGI;

my $cgi = new CGI;
my $image = $cgi->param('image');

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

print "<html><head><title>$image</title></head>\n";
print "<body>\n";
print "<center>\n";
print "<img src=/var/www/html/$image><p>\n";
print "</center>\n";
print "</body>\n";
print "</html>\n";

Thanks very much
Jeffers
# 2  
Old 06-04-2010
The reason the images aren't showing is that the img-tag requires the path is based on the servers document root. Eg, if your document root is /var/www/html and the images full path is /var/www/html/images/stats1.png, it would have to be included in one of these 2 ways:
HTML Code:
<img src="/images/stats1.png"> <!-- Absolute -->
<img src="images/stats1.png"> <!-- Relative, assuming your script resides in the document root -->
However, if you want to do more than just a bit of dynamic web sites, read up (at least) on HTML/XHTML, maybe CSS, and get a copy of CGI Programming with Perl, just to cover the basics.
# 3  
Old 06-04-2010
Thanks Pludi for your reply

I did try changing the path but it didn't have the desired affect. I first had issues with enabling cgi, but i think i've solved that now, i've done the following in order to get the images appearing...

DirectoryIndex includes the index.cgi extension
AddHandler cgi-script .cgi is uncommented

httpd.conf is as follows
Code:
ScriptAlias /cgi-bin/ "/var/www/html" 
<Directory "/var/www/html/">
    AllowOverride None
    Options None FollowSymLinks +ExecCGI
    Order allow,deny
    Allow from all
</Directory>

DocumentRoot
<Directory "/var/www/html">

I changed the directory as you suggested

Code:
print "<img src=rrdtool/$image><p>\n";

Source code from the site
Code:
<html><head><title></title></head>
<body><center>
<img src=rrdtool/><p>
</center></body></html>

isn't their any easier way to include each image in the directory than using a bunch of print commands, would it be easier to use some kind of for each loop in a perl script??

thanks for your help

---------- Post updated 06-05-10 at 00:55 ---------- Previous update was 06-04-10 at 22:58 ----------

OK now im completely confused,

i just tried looking in the directory where all my images are stored and i get an error saying Forbidden, i changed the following and it worked again, could it be that i have the cgi config setting all screwed up ??

was
Code:
ScriptAlias /cgi-bin/ "/var/www/html" 
<Directory "/var/www/html/">

now
Code:
ScriptAlias /cgi-bin/ "/var/www/cgi-bin" 
<Directory "/var/www/cgi-bin/">

now that ive changed it i get the forbidden error when trying to access the url
http://mywebserver/pass.cgi

when i used to get the following from the apache log file

[Fri Jun 04 23:03:01 2010] [error] [client 10.20.20.20] Use of uninitialized value in concatenation (.) or string at /var/www/html/pass.cgi line 9.
[Fri Jun 04 23:03:01 2010] [error] [client 10.20.20.20] Use of uninitialized value in concatenation (.) or string at /var/www/html/pass.cgi line 11.
[Fri Jun 04 23:03:01 2010] [error] [client 10.20.20.20] Directory index forbidden by Options directive: /var/www/html/rrdtool/, referer: http://mywebserver/pass.cgi

I guess the last line is stopping the cgi script from working, i dont understand this.
# 4  
Old 06-07-2010
Randomly changing settings without knowing what they're doing seldom solves a problem (even if it's a nice learning experience).

Change back the httpd.conf settings. The Directory directive needs a real path as an argument, not an Alias.

The Forbidden message came up because Apache, by default, does not allow directory indexing as a security measure. That does not mean that you can't access the files inside, it only means you'll have to know the exact name.

And your script does exactly what you told it to (which isn't necessarily what you wanted). It takes the name of an image as an argument, and displays said image. So you'd have to call it as http://mywebserver/pass.cgi?image=test.png it would display the image "test.png" from the directory /var/www/html/rrdtool/
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl CGI : unable to download the excel sheet from perl cgi page

Hi All, I have written an cgi perl script that displays an image(Excel image) and when clicked on that Image I need to download a excel sheet. I made sure that excel sheet exists in the folder with the given name but still I am not able to download the sheet. print "<center><table... (2 Replies)
Discussion started by: scriptscript
2 Replies

2. Windows & DOS: Issues & Discussions

Super Simple Script to remove first characters of any png file

Well I searched the net with varying success, but it seems kinda hard to find a one/max 2 lined command to: strip all *.png files in the folder from their first two characters. Any help is appreciated. In DOS commandline of course... (17 Replies)
Discussion started by: pasc
17 Replies

3. Web Development

CGI not working with httpd server on busybox 1.15.0 on ltib Linux 2.6.34 (404 page not found)

I have some industrial ARM linux board with 2.6.34 Linux on it with Busybox v1.15.0. The https.conf is located in /etc/ and contains: H:/root/web In the www directory I also have 'cgi-bin' folder with chmod 777 and in that folder a file called 'testcgi'. Now I start the server with... (1 Reply)
Discussion started by: Roboserg
1 Replies

4. Shell Programming and Scripting

Print multiple copies page by page using lp command

Hi I have a pdf file that is being generated using the rwrun command in the shell script. I then have the lp command in the shell script to print the same pdf file. Suppose there are 4 pages in the pdf file , I need to print 2 copies of the first page, 2 copies of the second page , then 2... (7 Replies)
Discussion started by: megha2525
7 Replies

5. Shell Programming and Scripting

Delete Files with CGI Bash Script

Hi. I could use some help with my problem. I am creating a website. One option on this website is to delete a specified file, say an image, when the user clicks on it. I want to do this with CGI. I believe bash will be the easiest since I will just type the command "rm file". I also do not know... (4 Replies)
Discussion started by: JMooney5115
4 Replies

6. Shell Programming and Scripting

How to pass data from server (CGI script) to client (html page)

Hi I know how to pass data from client side (html file) to server using CGI script (POST method). I also know how to re-create the html page from server side after receiving the data (using printf). However I want to write static pages on client side (only the structure), and only to pass... (0 Replies)
Discussion started by: naamabm
0 Replies

7. Shell Programming and Scripting

lpr- how to print from page to page

hi all i have file_1 which contains 66 pages and i want to print only page 1 to 3 i try to write like this lp -d name_of_printer file_1 -P 1-7 this command does not work any ideas ? (4 Replies)
Discussion started by: naamas03
4 Replies

8. Shell Programming and Scripting

CGI: refresh page and timeout

Hi, I've been able to put together an HTML page that calls on a cgi program (via ksh) to do some server side processing, then send it back to the user's browser window. The script basically tails a log file and refreshes every # seconds (user chosen) by using the "refresh" meta tag (html). My... (0 Replies)
Discussion started by: man
0 Replies

9. Shell Programming and Scripting

CGI passing arrays/hashes to another CGI script

If I have a Perl CGI script (script01), which fills an array(s) with information and outputs a HTML page with a link to another CGI page (script02); is there anyway to pass the array(s) from "script01" to "script02" when the page visitor clicks the link? Hope that makes sense! :) (2 Replies)
Discussion started by: WIntellect
2 Replies
Login or Register to Ask a Question