Google Answers Logo
View Question
 
Q: Output file if file is there from Perl cgi template, no error if not there. ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Output file if file is there from Perl cgi template, no error if not there.
Category: Computers > Programming
Asked by: horseradish-ga
List Price: $4.00
Posted: 26 Sep 2003 02:45 PDT
Expires: 26 Oct 2003 01:45 PST
Question ID: 260360
I'm printing an image from within a perl cgi template, using the line:
print "<img src=\"../images/".$content.".gif\">";

However, because the template pulls in various other elements to
create dynamic pages, it's possible that sometimes there won't be an
image to output in this area, which would currently give a broken
image link.

Is there some little perl script I could insert instead which would
say:

print image '$content' 
if image $content is there, 
otherwise don't print anything (and don't cause an error!)

Thanks :-)
Answer  
Subject: Re: Output file if file is there from Perl cgi template, no error if not there.
Answered By: joseleon-ga on 26 Sep 2003 04:28 PDT
Rated:5 out of 5 stars
 
Hello, horseradish:

The easiest way to do that it's to check if the image file exists in
your hard disk, let's look this code:

#!/usr/bin/perl

$physical_path="/home/ttm/perl/images/";

$content="myimage.gif";

if (-e $physical_path.$content)
{
	print "<img src=\"../images/".$content.".gif\">";
}

But as you say, you are using a Perl template, if the template doesn't
allow you to perform that simple check, you can do it in your perl
code and if the file doesn't exists, change $content to contain
"pixel" and put a transparent 1x1 pixel image into your images dir to
substitute those non existent images.

In any case, please, tell me which Perl template system are you using,
so if you have any problem with this solution, I will keep on working
on it until you are happy with the results.

Regards.

Request for Answer Clarification by horseradish-ga on 26 Sep 2003 05:03 PDT
Thanks for your help :-)
I didn't mean I was using a Perl template that someone else had
created, I simply refer to my script as a template as it's the
'skeleton' of the pages it creates dynamically. So I'll try that -
assuming the
$physical_path="/home/ttm/perl/images/";

should be replaced with the path to my images, eg:
$physical_path="/new/images/";

is thast correct? Or does it need the home/ttm/perl but in there?

Thanks!

Clarification of Answer by joseleon-ga on 26 Sep 2003 05:15 PDT
Hello, horseradish:

Yes, that is correct, you must change the line:

$physical_path="/home/ttm/perl/images/"; 

to:

$physical_path="/complete/path/where/your/images/are/stored"; 

That is, the complete path where your images are stored on your
server, that it should be slightly different than the path you use to
publish your images. It could be /new/images but probably will have
the form of /home/youruser/yourpublishdir/images etc, etc.

If you have problems to get the physical dir where your images are
stored, just tell me and I will give you additional instructions on
how to get it.

Regards.

Request for Answer Clarification by horseradish-ga on 26 Sep 2003 05:16 PDT
Taking what you've suggested, do you think it could be simplified
further in this way:

$physical_path="/new/images/".$content.".gif"; 
 if (-e $physical_path) 
{ 
 print "<img src=\"$physical_path">"; 
}

Thanks :-)

Clarification of Answer by joseleon-ga on 26 Sep 2003 05:27 PDT
Hello, horseradish:

No, I'm sorry, because there are two different ways to access the
images stored on a webserver:

1º Webserver paths
2º Physical paths

The first one is the way you are doing it now and it works ok, you set
that path on <img tags and the webserver is responsible to resolve
these paths.

The second one it works on the same way you work on your computer,
that is "physical" paths, where the file resides actually.

As I said, it cannot be simplified that way, but you can create a
function to return the image tag if the image exists and a blank if it
doesn't.

Regards.

Request for Answer Clarification by horseradish-ga on 26 Sep 2003 05:40 PDT
OK one last thing then. I already have the $content variable set up,
let's say it's holding the word 'about'. I usually add the .gif
afterwards, as 'about' is a key word which brings in other elements
too. Also I'm adding bg_ to the front as it's an image. How do I get
this in the correct form so
if (-e $physical_path.$image) was really saying
if (-e /new/images/bg_about.gif)

I assume it's something like this, but not sure if I've got the
correct syntax etc!

$physical_path="/new/images/bg_"; 
$image=$content.".gif"; 
if (-e $physical_path.$image) 
{ 
 print "<img src=\"/new/images/".$content.".gif\">"; 
}

Thanks very much, hope that's it now!

Clarification of Answer by joseleon-ga on 26 Sep 2003 08:49 PDT
Hello, horseradish:

You are almost there, but I think it should be this way:

$physical_path="/new/images/bg_";  
$image=$content.".gif";  
if (-e $physical_path.$image)  
{  
 print "<img src=\"../images/bg_".$content.".gif\">";  
} 

Is there a way you can post your script here so I can fix it and guess
what is the physical path where you store your images?

I remark again that you can have your images stored in a directory of
your server, for example /home/yourhomedir/yourpublishdir/images, but
your webserver "sees" that directory as just "images", so to check out
on your server whether the file exists or not, you need to use the
"physical path" and to let the browser "find" your image, you need to
use the "web server path". Also, if you still are stuck with this, I
can access your server to get what the physical path is.

Regards.

Request for Answer Clarification by horseradish-ga on 26 Sep 2003 12:42 PDT
Thanks for the offer, my script is below. I'm sure it's almost there
but it's not bringing th image in with the line if (-e
$physical_path.$image). I have tested it to make sure the ight data is
getting through - as you'll see by the extra print lines I've added
in. These definietly show the correct path to the image, and the
second one definitely prints the image find. It's just not printing
the image when the 'if' line come before it.

Thank you for your continued help!


-------------------------

#!/usr/local/bin/perl 

require "leftside.lib";

if ($ENV{'REQUEST_METHOD'} eq 'GET')
	{
	@pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
	read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	@pairs = split(/&/, $buffer);
} else {
	print "Content-type: text/html\n\n";
	print "Use Post or Get";
}

foreach $pair (@pairs) {
	($key, $value) = split (/=/, $pair);
	$key =~ tr/+/ /;
	$key =~s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
	
	$value =~ tr/+/ /;
	$value =~s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
	
	$value =~s/<!--(.|\n)*-->//g;
	
	if ($formdata{$key}) {
		$formdata{$key} .= ", $value";
	} else {
		$formdata{$key} = $value;
	}
}

$content = $formdata{'content'};
$number = $formdata{'main'};

print "Content-type: text/html\n\n";
print <<"top";
<html>
<head>
	<title>Fever</title>
	<link HREF="/new/fever.css" TYPE="text/css" REL="stylesheet"
MEDIA="Screen">
</head>
<body bgcolor="#000000" marginwidth=0 marginheight=0 leftmargin=0
topmargin=0>
<table border="1" width="740" cellspacing="0" cellpadding="0"
class=body>
	<tr valign=top>
		<td width=125 height=375>
top

&Nav;

$physical_path="/new/images/bg_";   
$image=$content.".gif";
print $physical_path.$image;
print "<img src=\"/new/images/bg_".$content.".gif\">";   
if (-e $physical_path.$image)   
{
print "<img src=\"/new/images/bg_".$content.".gif\">";
}

print "</td>\n<td width=20><img src=\"/new/images/ip.gif\"
width=20></td>\n<td width=415 rowspan=2>";

$center = "/web/new/".$content."/".$content.$number.".html"; 
print $center;
open(INFILE, $center) or die "Can't open file: $!"; 
while (<INFILE>) { 
print $_; 
}

print "</td><td width=20><img src=\"/new/images/ip.gif\"
width=20></td>\n<td width=160 rowspan=2>";

$submenu = "/web/new/".$content."/".$content."_sub.html"; 
open(INFILE, $submenu) or die "Can't open file: $!"; 
while (<INFILE>) { 
print $_; 
}

print "</td></tr>\n</table></body></html>";

Clarification of Answer by joseleon-ga on 29 Sep 2003 02:16 PDT
Hello, horseradish:

 Unfortunately I cannot guess your physical path from that script, put
this line somewhere in your code:

print `pwd`;

Copy and paste it because I'm using backquotes (`), this line will
execute a pwd command and will dump the results, this way I could
guess your physical path and change the portion:

$physical_path="/new/images/bg_";    
$image=$content.".gif"; 
print $physical_path.$image; 
print "<img src=\"/new/images/bg_".$content.".gif\">";    
if (-e $physical_path.$image)    
{ 
print "<img src=\"/new/images/bg_".$content.".gif\">"; 
} 

I'm 99.9% sure that your physical path it's not /new/images, this is
the webserver path.

Regards.

Request for Answer Clarification by horseradish-ga on 29 Sep 2003 03:53 PDT
Ok the result I get from that is simply:

/cgi

Does that make sense?

Thanks a lot for your help :-)

Clarification of Answer by joseleon-ga on 29 Sep 2003 05:11 PDT
Hello, horseradish:
  Are you executing that script on a remote webserver or in your own machine?

Regards.

Request for Answer Clarification by horseradish-ga on 29 Sep 2003 05:18 PDT
On a remote webserver - it's hosted at a company called xo.com

Clarification of Answer by joseleon-ga on 29 Sep 2003 05:21 PDT
Hello, horseradish:
  Well, that's very strange, in any case, do you have any objection to
send me a user and a password to access your server, so I can gather
the data I need? Don't post it here, I will provide you instructions
to send me such information privately without infringing Goggle
Answers TOS.

Regards.

Request for Answer Clarification by horseradish-ga on 29 Sep 2003 06:19 PDT
Thanks for this kind offer. I'm a little hesitant about giving the
details as it's not my site or domain. But xo.com have pretty good
support - perhaps I could ask them directly. Would you mind telling me
exactly what I need to know - is it the webserver path to the images
folder?

Thanks!

Clarification of Answer by joseleon-ga on 29 Sep 2003 06:28 PDT
Hello, horseradish:

No problem with that, just trying to give you a solution ;-)

Yes, you need to ask them:

-Full "physical" path to your images directory

In most shared hosting servers it's in the form of:

/home/youruser/www/images

That is, they setup an specific directory for your website.

When you have this full path, you just need to add it to the script
and check if the image it's *physically* there and just print out the
image tag if it's there.

Looking forward to see if your problem with physical paths is finally
solved ;-)

Regards.

Request for Answer Clarification by horseradish-ga on 03 Oct 2003 06:41 PDT
Thank you I finally got the solution! It was /web/new/images..

Thanks so much for your in-depth help!

Clarification of Answer by joseleon-ga on 03 Oct 2003 11:15 PDT
Hello, horseradish:

  Great your problem it's solved now and thanks for the tip and the nice comments!

Regards.
horseradish-ga rated this answer:5 out of 5 stars and gave an additional tip of: $4.00
This researcher went out of their way to ensure I had the solution to
the problem, and never made me feel as though I was asking too much of
them. Brilliant service!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy