homepage Welcome to WebmasterWorld Guest from 54.242.140.11
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
What does this function do?
I don't know perl
majjk

10+ Year Member



 
Msg#: 3959184 posted 8:08 pm on Jul 24, 2009 (gmt 0)

Hi,
I'm a php guy, and don't know much about perl... so could maybe someone help me out by explaining what this function does, step by step. I want to write a function in php that does the same thing... i.e. rename files that have been uploaded when the filename is not unique i the upload directory.


sub rename_filename{
my $file_name = shift;
my $count = shift;
my $upload_dir = shift;
my $path_to_file = $upload_dir . $file_name;

if(-e $path_to_file && -f $path_to_file){
if($file_name =~ /(.*)_(\d*)\.(.*)/){
# Already renamed so count on
$count = $2 + 1;
$file_name =~ s/(.*)_(\d*)\.(.*)/$1_$count\.$3/;
}
else{
# Not renamed so start counting
$file_name =~ s/(.*)\.(.*)/$1_$count\.$2/;
}
&rename_filename($file_name, $count, $upload_dir);
}
else{ return $file_name; }
}

Many thanks in advance!

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3959184 posted 9:06 pm on Jul 24, 2009 (gmt 0)


sub rename_filename{
# Shift filename from (default) array '@argv' to locally-scoped variable $file_name
my $file_name = shift;
# Shift count from array '@argv' to local variable $count
my $count = shift;
# Shift upload directory path from array '@argv' to local variable $upload_dir
my $upload_dir = shift;
# Build full path to file
my $path_to_file = $upload_dir . $file_name;

# If a file exists at that path and is an ordinary file
if(-e $path_to_file && -f $path_to_file){
# then if filename is in the form "<anything-or-nothing>_<any-number-(including zero)-of-digits>.<anything-or-nothing>"
if($file_name =~ /(.*)_(\d*)\.(.*)/){
# Filename was already used in renaming so increment the count
$count = $2 + 1;
# Replace previously-used filaname with initial name part, underscore, incremented count, dot, and filetype.
$file_name =~ s/(.*)_(\d*)\.(.*)/$1_$count\.$3/;
}
else{
# else filename not already used, so just stick an underscore and the count in the middle
$file_name =~ s/(.*)\.(.*)/$1_$count\.$2/;
}
# Rename the file
&rename_filename($file_name, $count, $upload_dir);
}
# else file does not exist or is not an ordinary file
else{ return $file_name; }
}

I would strongly recommend replacing the regex pattern with something more specific, both for security and for performance reasons: Something like /^((\w+)+)_(\d+)\.([a-z0-9]+)$/ would be safer and faster to process. Note that the extra 'layer' of parentheses used around the first sub-pattern will require that back-references $2 and $3 above be changed to $3 and $4.

Jim

majjk

10+ Year Member



 
Msg#: 3959184 posted 8:38 am on Jul 25, 2009 (gmt 0)

thanks a lot!

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3959184 posted 2:52 pm on Jul 25, 2009 (gmt 0)

Also, this is redundant:

if(-e $path_to_file && -f $path_to_file){

-e is "exists" and -f is "is file." So if it's a file, it exists. :-) With just -e, the resource may be a directory, alias, etc.

if(-f $path_to_file){

perl_diver

5+ Year Member



 
Msg#: 3959184 posted 2:31 am on Jul 26, 2009 (gmt 0)

# Shift filename from (default) array '@argv' to locally-scoped variable $file_name

inside a subroutine the array is @_ not @ARGV.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved