Previous Thread
Next Thread
Print Thread
Rate Thread
Joined: May 2002
Posts: 48
WC
Offline
Member
Member
Offline
Joined: May 2002
Posts: 48
Anyone know how I would go about checking for a files's extension in perl ?

Sponsored Links
Joined: Jan 2000
Posts: 5,073
Admin Emeritus
Admin Emeritus
Joined: Jan 2000
Posts: 5,073
Code
my $filename = "whatever.txt.ext";
my @components = split(/./, $filename);
my $real_extension = $components[-1];
That should do the trick.


UBB.classic: Love it or hate it, it was mine.
Joined: May 2002
Posts: 48
WC
Offline
Member
Member
Offline
Joined: May 2002
Posts: 48
So to check for the extension in a if statement I would use something like if ($real_extension = "ext") ?

Joined: Mar 2001
Posts: 7,394
LK Offline
Admin / Code Breaker
Admin / Code Breaker
Offline
Joined: Mar 2001
Posts: 7,394
instead of '=' use 'eq'...
Quote
code:
Code
if ($real_extension eq 'ext') {

You can also do, instead of all that:
code:
[qb]
Code
if ($filename =~ m/.ext$/) {
[/qb]

Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
And if you want the check to be case-insensitive:

Code
if ($filename =~ m/.ext$/i) {

Sponsored Links
Joined: May 2002
Posts: 48
WC
Offline
Member
Member
Offline
Joined: May 2002
Posts: 48
Ok Im trying to do this within public_topic_page.pl

Code
my $filename = $user_profile[20];
my @components = split(/./, $filename);
my $real_extension = $components[-1];
if (($filename =~ m/.jpg$/i) || ($filename =~ m/.gif$/i)){


chomp($user_profile[20]);
$user_profile[20] = "" if (($user_profile[20] eq 'http://') &#0124;&#0124; ($user_profile[20] =~ /cp.cgi/ &#0124;&#0124; /ultimatebb.cgi/ &#0124;&#0124; /</));
my $this_img = ($user_profile[20] eq "") ? '' : qq!
<img src="$user_profile[20]" alt="" />
!;
}

if ($filename =~ m/.swf$/i) {

chomp($user_profile[20]);
$user_profile[20] = "" if (($user_profile[20] eq 'http://') &#0124;&#0124; ($user_profile[20] =~ /cp.cgi/ &#0124;&#0124; /ultimatebb.cgi/ &#0124;&#0124; /</));
my $this_img = ($user_profile[20] eq "") ? '' : qq!
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="120" height="120">
<param name=movie value="$user_profile[20]">
<param name=quality value=high>
<embed src="$user_profile[20]" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="120" height="120">
</embed>
</object>
!;
}
Doesn't seem to work though...This is based on the external avatar hack thing

Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
try this one:

Code
my $filename = chomp($user_profile[20]);
my $this_img;

if (($filename =~ m/.jpg$/i) &#0124;&#0124; ($filename =~ m/.gif$/i)){
$this_img = qq!
<img src="$filename" alt="" />
!;
}

if ($filename =~ m/.swf$/i) {
$this_img = qq!

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="120" height="120">
<param name=movie value="$filename">
<param name=quality value=high>
<embed src="$filename" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="120" height="120">
</embed>
</object>
!;
}

Joined: May 2002
Posts: 48
WC
Offline
Member
Member
Offline
Joined: May 2002
Posts: 48
That doesn't work either, neither the img or swf avatars show up so I'm assuming there is something wrong with the if statements

Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
are you sure that $user_profile[20] is not blank and holds a jpg or gif or swf value? Also is $user_profile[20] correct slot? I dont know ubb member fields...

The above code seems right to me...

You can set a default valuve where it says 'my $this_img;'

my $this_img = '';

Also, if it does not hold the full url, add it before $this_image;

$this_img = qq!

!;

etc...

Joined: May 2002
Posts: 48
WC
Offline
Member
Member
Offline
Joined: May 2002
Posts: 48
$user_profile holds the correct URL...Im sure of this because when I use the code seperately without the if statements then the avatar shows up, its just when I add the if statements in nothing appears so Im assuming the if statements are wrong or theres something else wrong with the code ?

Sponsored Links
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
Try printing out $filename before the if's, and $this_img afterwards, to see what's going on.

You could also simplify things a bit:

if ($filename =~ m/.(gif|jpg)$/i) {
...
} elsif ($filename =~ m/.swf$/i) {
...
}

Joined: May 2002
Posts: 48
WC
Offline
Member
Member
Offline
Joined: May 2002
Posts: 48
NM...trying now....was uploading the wrong file

[EDIT]: Tried the simplied code and its still not working. How would I go about displaying the filename ? When I used $filename near where the images, rating is displayed I got a 0 as the output

Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
Do you have access to your server error log? If so, you can output it there using 'warn':

warn "filename='$filename'";

If not, you can output it to a temporary file:

open(my $fh_debug, '>>', '/path/to/debug') or die "$!";
print $fh_debug scalar localtime, ":filename='$filename'n";
close($fh_debug);

Joined: Jan 2000
Posts: 5,073
Admin Emeritus
Admin Emeritus
Joined: Jan 2000
Posts: 5,073
... or you can just print "FN: $filename
"; wink


UBB.classic: Love it or hate it, it was mine.
Joined: May 2002
Posts: 48
WC
Offline
Member
Member
Offline
Joined: May 2002
Posts: 48
Got it to work !!!!!!!

Heres the code that I finally ended up using in case anybody else is interested
my $filename = chomp($user_profile[20]);

my $this_img;

if ($user_profile[20] =~ m/.(gif|jpg)$/i) {
$this_img = qq!

!;

}
elsif ($user_profile[20] =~ m/.swf$/i) {
$this_img = qq!







!;
}

Used User_profile[20] instead of $filename in the if statements and it ended up workiong..go figure... thanks for all the help smile
}

Joined: Sep 2002
Posts: 35
Member
Member
Offline
Joined: Sep 2002
Posts: 35
how is this to be used???

i'm looking to add an icon to indicate if an image is displayed within post??

Basically it will add an icon to a post if someone has posted an image within the topic. much like this forum does: http://forums.vwvortex.com/zeroforum?id=27

could this code somehow work with that??

has anyone created such a hack that i mention??

Joined: May 2002
Posts: 48
WC
Offline
Member
Member
Offline
Joined: May 2002
Posts: 48
Well this code is for getting external avatars to work...however I guess it could be used in the way you describe however it might require extensive modification

Joined: Mar 2001
Posts: 7,394
LK Offline
Admin / Code Breaker
Admin / Code Breaker
Offline
Joined: Mar 2001
Posts: 7,394
WC, I suggest you to use &ImageChecker($user_profile[20]), to make sure people don't do it for bad causes.


Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
AllenAyres
AllenAyres
Texas
Posts: 21,079
Joined: March 2000
Forum Statistics
Forums63
Topics37,573
Posts293,925
Members13,849
Most Online5,166
Sep 15th, 2019
Today's Statistics
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
Top Posters
AllenAyres 21,079
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,833
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Morgan 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2024 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.0.0
(Preview build 20221218)