Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

CGI Image on localhost

by eoin (Monk)
on Dec 21, 2003 at 20:32 UTC ( [id://316237]=perlquestion: print w/replies, xml ) Need Help??

eoin has asked for the wisdom of the Perl Monks concerning the following question:

Merry Christmas everybody,

I'm not really sure that this is the right place to ask this question but you've never steered so far. So here goes :)
I have just setup Apache2 on my "family machine" and am in the process of setting up a cgi based family website.
The script that I use
#!F:\perl\bin\perl.exe -wT use strict; use CGI; my $q = CGI->new(); my $node = $q->param('node') || ""; my $html_dir_path = "..\\data\\html\\"; my $html_file_path = "$html_dir_path$node.txt"; open(HTML, "$html_file_path") or die "Error! Couldn't access HTML file +\n"; print "Content-type: text/html\n\n"; while(<HTML>) { print $_; } exit;
Works grand its just that I can't get my images to load.
My scripts are in the cgi-bin dirctory no doubt and and my images are in htdocs\images\. I'm running a win2k machine and apache2.0.47.
I know, I know, its more of a htmll question but I thought I'd chace my luck, it being christmas and all. I've already tried the ovious ones like
"..\htdocs\images\image.jpg"
But I can't figure it out.

Thanks in advance for any help.
div class="pmsig">

Merry Christmas, Eoin...

If everything seems to be going well, you obviously don't know what the hell is going on.

Replies are listed 'Best First'.
Re: CGI Image on localhost
by exussum0 (Vicar) on Dec 21, 2003 at 22:42 UTC
    Typically with apache, you have your cgi's in a sperate directory than you static, non-program, content. You have that straight. What you would typically do is have your images refered as http://machinename/images/image.jpg

    You can use .., but you'd probably use ../images/image.jpg. That's a relative url. Relative url's are nice usually, but it's a matter of opinion what people like more :)

    Remember, for browsers, its typically nicer to use /'s instead of \'s.


    Play that funky music white boy..
      Cheers thats what I was looking for, however will <quote>relative urls</quote> work under windows eviroments e.g. the windows servers??

      Merry Christmas, Eoin...

      If everything seems to be going well, you obviously don't know what the hell is going on.

        All in all, apache should do the right thing with slashes.

        Here's a long winded explanation on why it's fine...

        Let's put it this way. When you click on a link in a browser, it's supposed to issue an http request to the server based on the href. If the href has ../../../ etc, it will strip a few directories in the request. LIkewise if you do ../tmp/../tmp/../tmp. That's basic directory resolving in any os (replace /'s with \'s).

        So if you are at http://www/somedir/somedir2 and clicked a link http="../", you'll ask for http://www/somedir , if you clicked http="../somedir2/../somedir2" it should request http://www/somedir2/../somedir2. That is a guess. It may recompress it back to http://somedir/somedir2, but that's somethign to test and play with. not very useful :)

        Anyway, the browser should try and compress it somewhat, turning initial ../'s back into something shorter. The request is sent to the server and it can interpret it ANY WAY it wants. Doing something like /cgi-bin/this.cgi/that will pass that as an argument to your this.cgi script. This is a feature of the server 'cause it parses it that way. Well, at least apache did last i checked :) Anyway, last check, apache on windows should translate http://blah/this/../that/ int c:\whereyouinstalledapache\htdocs\this\..\that, which should turn into c:\whereyouinstalledapache\htdocs\that.


        Play that funky music white boy..
Re: CGI Image on localhost
by Roger (Parson) on Dec 21, 2003 at 23:09 UTC
    The following is a image slideshow demo I wrote not long ago. Save the script as 'p109.pl' under /cgi-bin.

    #!C:/Perl/bin/perl.exe -w use strict; use CGI; my $q = new CGI; my $q_image = $q->param('image'); my $image_url = "/images"; my @images = map { /([^\/]+)$/ } <../htdocs$image_url/*.jpg>; $q_image = $images[0] if ! defined $q_image; my $html = do { local $/; <DATA> }; $html =~ s/%%([^%]*)%%/ if ($1 eq "IMAGE") { image_link() } elsif ($1 eq "PREV") { prev_link() } else { next_link() } /ge; print $q->header, $html; sub image_link { return qq{<img src="$image_url/$q_image" alt="$q_image">}; } sub prev_link { return qq{<a href='/cgi-bin/p109.pl?image=}.get_prev_image().qq{'> +PREV</a>}; } sub next_link { return qq{<a href='/cgi-bin/p109.pl?image=}.get_next_image().qq{'> +NEXT</a>}; } sub get_prev_image { my %hash = map { $images[$_] => $_ } 0..$#images; return $images[$hash{$q_image}-1]; } sub get_next_image { my %hash = map { $images[$_] => $_ } 0..$#images; my $next = $hash{$q_image}+1; $next = 0 if $next > $#images; return $images[$next]; } __DATA__ <HTML> <TITLE>Simple Image Slideshow CGI in Perl</TITLE> <BODY> <TABLE align=center> <tr><td align=center>%%PREV%%</td><td align=center>%%NEXT%%</td></tr> <tr><td colspan=2>%%IMAGE%%</td></tr> </TABLE> </BODY> </HTML>
Re: CGI Image on localhost
by rchiav (Deacon) on Dec 21, 2003 at 20:36 UTC
    Did you try to view the source of the HTML from your browser?

    Did you see if you can type in the path to the image in your browser?

    Did you see if the path that works in the browser is the same as the path in the HTML?

      Do you mean just "F:\foo\bar\apache\htdocs\images\image.jpg"?
      I thought of that but Is there any other way??

      All the Best, Eoin...

      If everything seems to be going well, you obviously don't know what the hell is going on.

        I mean, did you do any of that? It seems like you're not trying to solve your own problem and want us to guess what's wrong for you.

        The image is not displaying. WHY isn't it displaying?? Have you looked at the source of the HTML and see how it's refrenceing the picture? We can't guess what the IMG tag is pointing to. if you

        r images are located at http://localhost/images/myimage.jpg and in the generated HTML it's http://localhost/somethingelse/myimage.jpg then that's your problem. Your quesion is like me saying "My Car won't start. Why?"

Re: CGI Image on localhost
by CountZero (Bishop) on Dec 21, 2003 at 22:06 UTC
    Check the content of the <img> tag. It should be something like "<img scr='images/image.jpg'/>".

    If it starts with a / it is an absolute path and you should avoid that.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://316237]
Approved by Roger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2025-03-20 02:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (60 votes). Check out past polls.