============================================
$basedir = "http://www.mydomain.co.uk/";
@files = qw(1.jpg 2.jpg 3.jpg 4.jpg);
srand(time ^ $$);
$num = rand(@files);
print "Location: $basedir$files[$num]\n\n";
============================================
The script works perfect, but i wanted to customize it to use alternative settings if first setting not found. In other words when the script looks at $basedir and @files for a file, if the file cannot be found or has been renamed etc, the script should use an alternative setting i.e. $basedir2 and @files2 so basically $basedir2 and @files2 will be exactly same as the other but the only thing that will be different is the value of $basedir2. (if you know what i mean?) just like the way Primary and Secondary DNS works.
Note: it should only resort to the alternative setting if the image is not found not any other reason.
I will be very greatful if someone can help me out on this
I appreciate your help
Thanks
Linda
$basedir = "http://www.mydomain.co.uk/";
$basedir2 = "http://mirror.mydomain.co.uk/";
#
@files = %qw%(1.jpg 2.jpg 3.jpg 4.jpg);
#
srand [perldoc.com](time [perldoc.com] ^ $$);
$num = rand [perldoc.com](@files);
#
$img = join [perldoc.com] '',
(-f "$ENV{DOCUMENT_ROOT}/$files[$num]"? $basedir : $basedir2),
$files[$num];
#
print [perldoc.com] "Location: $img\n\n";
The -f operator returns true if the argument is a file.
Andreas
please help me out.
Thanks
Linda
Bareword found where operator expected at d:\htdocs\image.cgi line 4, near "1.jpg"
(Missing operator before jpg?)
syntax error at d:\htdocs\image.cgi line 4, near "1.jpg "
Number found where operator expected at d:\htdocs\image.cgi line 4, near "jpg 2."
(Do you need to predeclare jpg?)
Bareword found where operator expected at d:\htdocs\image.cgi line 4, near "2.jpg"
(Missing operator before jpg?)
Number found where operator expected at d:\htdocs\image.cgi line 4, near "jpg 3."
(Do you need to predeclare jpg?)
Bareword found where operator expected at d:\htdocs\image.cgi line 4, near "3.jpg"
(Missing operat
@files = qw(1.jpg 2.jpg 3.jpg 4.jpg);
The qw enclosed in percent signs was just me and my auto quoting system trying to turn qw into a link to the Perl [perl.com] manual entry for qw.
Andreas
The Perl [perl.com] cannot find the images. Are they stored in the DocumentRoot [httpd.apache.org] folder? If not you need to specify their path.
Andreas
The only problem i am having is that, the actual script is hosted on $basedir2 server and its WindowsNT and $basedir is on a total different web URL and a Unix Server. And since the script is located in $basedir2 it tries to located $basedir on that same server for somereason, and it cant find $basedir and therefore automatically goes to $basedir2.
Can anyone help me customize it so that it checks $basedir on a different server first and only (ONLY) if it is not found then the last resort should be to switch to $basedir2
$basedir = "http://www.mydomain.co.uk/";
$basedir2 = "http://mirror.mydomain.co.uk/";
#
@files = qw(1.jpg 2.jpg 3.jpg 4.jpg);
#
srand (time ^ $$);
$num = rand (@files);
#
$img = join '',
(-f "$ENV{DOCUMENT_ROOT}/$files[$num]"? $basedir : $basedir2),
$files[$num];
#
print "Location: $img\n\n";
I appreciate anyones help
Cheers
Linda
For a Perl script to determine if a file exits on a different server you are going to need to make a HTTP request to that server and see if you get a valid response.
You can do this by using the LWP::UserAgent.pm [search.cpan.org] module along with HTTP::Status.
The snippet of code shows how a script running on servera checks for a file on serverb and depending on the result will print the correct location.
Use this example as a starting point to adapt your existing script.
use LWP::UserAgent;
use HTTP::Status;my $ua = new LWP::UserAgent;
# define a User Agent and Timeout #
$ua->agent('My Image Cheker');
$ua->timeout(20);# make the request to server b
my $req = new HTTP::Request( GET => 'http://www.serverb.tld/1.jpg');# check if server b responds
my $response = $ua->request($req);if ($response->is_success) {
# image file exists on serverb
print "Location: [serverb.tld...]} else {
# image file does not exist on server b
# so print the copy from server a
print "Location: [servera.tld...]
}
On a more basic note, can't your @files array have the full URL listed :-
@files = qw(
[servera.tld...]
[serverb.tld...]
[serverb.tld...]
[servera.tld...]
);
This would save the overhead of making the request to another server.
- dip
use strict or die();