Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: rename file

by perlfan (Vicar)
on Aug 24, 2020 at 17:40 UTC ( #11121046=note: print w/replies, xml ) Need Help??


in reply to rename file

rename only file and leave extension alone

The file system doesn't care about extensions. If you wish to retain the extension (or add a different one), then you will have to add that explicitly to the new name.

use CGI; use CGI::Carp qw ( fatalsToBrowser ); use File::Basename; my $upload_dir = "upload/"; my $query = new CGI; my $filename = $query->param("file"); my $upload_filehandle = $query->upload("file"); my $ext = "jpg"; # this can be dynamic based on checking what kind of +file it is my $newname = "edddd.$ext"; my $fileto = rename($filename, $newname); open ( my $UPLOADFILE, ">", $upload_dir/$fileto" ) or die "$!"; binmode $UPLOADFILE; while ( <$upload_filehandle> ) { print $UPLOADFILE; }

Another note is that rename is like mv, which means that you're actually doing a cp and rm (of the original file) if crossing file system boundaries. If this is all happening on the same file system, then it's an atomic rename (fully done or not at all). The "copy" scenario introduces possible file corruption, and what I like to do in that case is an explicit copy, verify the $newname against the $filename, them unlink the $filename.

Update - added my $ to lexical file handle I added to the script.

Replies are listed 'Best First'.
Re^2: rename file
by AnomalousMonk (Bishop) on Aug 24, 2020 at 19:30 UTC
    Update - added $ to lexical file handle I added to the script.

    I'm not sure if the quotation applies to the
        open ( $UPLOADFILE, ">", $upload_dir/$fileto" ) or die "$!";
    statement or not, but if it does, please be aware that a sigil does not a lexical make.

    Strictures are not enabled in the code here, so $UPLOADFILE just autovivifies a package-global scalar filehandle where UPLOADFILE would have been a package-global bareword filehandle. For a true lexical, use my:
        open ( my $UPLOADFILE, ">", $upload_dir/$fileto" ) or die "$!";
    Of course, the script in question is short enough that lexical-versus-global filehandle won't make any practical difference.


    Give a man a fish:  <%-{-{-{-<

      Thanks, fixed. Oversight.

        You can't have run the code you posted:

        perl -c xx.pl [Thu Sep 3 09:15:03 2020] xx.pl: String found where operator expected + at xx.pl line 16, near "$fileto" ) or die "" [Thu Sep 3 09:15:03 2020] xx.pl: (Missing operator before " ) or +die "?) [Thu Sep 3 09:15:03 2020] xx.pl: Scalar found where operator expected + at xx.pl line 16, near "" ) or die "$!" [Thu Sep 3 09:15:03 2020] xx.pl: (Missing operator before $!?) [Thu Sep 3 09:15:03 2020] xx.pl: String found where operator expected + at xx.pl line 16, at end of line [Thu Sep 3 09:15:03 2020] xx.pl: (Missing semicolon on previous l +ine?) Status: 500 Content-type: text/html <h1>Software error:</h1> <pre>syntax error at xx.pl line 16, near &quot;$fileto&quot; ) or die +&quot;&quot; Can't find string terminator '&quot;' anywhere before EOF at xx.pl lin +e 16. </pre> <p> For help, please send mail to this site's webmaster, giving this error + message and the time and date of the error. </p> [Thu Sep 3 09:15:03 2020] xx.pl: syntax error at xx.pl line 16, near +"$fileto" ) or die "" [Thu Sep 3 09:15:03 2020] xx.pl: Can't find string terminator '"' any +where before EOF at xx.pl line 16.
Re^2: rename file
by haukex (Bishop) on Aug 24, 2020 at 18:57 UTC
    If this is all happening on the same file system, then it's an atomic rename (fully done or not at all).

    It's not quite that simple. While rename(2) is supposed to be atomic on POSIXish systems, there are several of corner cases where that isn't completely reliable (e.g. NFS). One really has to know what OS and filesystem one is working on.

      Thank you for clarifying this point, I almost mentioned NFS. OP didn't seem quite ready for a lesson of file systems, but I did want to relate it to what might actually be happening since they were thinking the operations were extension or file type aware. I also assumed they were coming from a more vulgar environment, like Windows (but used unix commands to illustrate the point nonetheless). That and using old skool CGI from 1990 seem to go hand in glove
Re^2: rename file
by AnomalousMonk (Bishop) on Sep 03, 2020 at 07:42 UTC

    I just noticed that the following statement in this post has an unbalanced double-quote:

    open ( my $UPLOADFILE, ">", $upload_dir/$fileto" ) or die "$!"; ^ ^ | | +------------------+
    A bit late, but...


    Give a man a fish:  <%-{-{-{-<

Re^2: rename file
by frank1 (Novice) on Aug 24, 2020 at 17:59 UTC

    thanks

Re^2: rename file
by AnomalousMonk (Bishop) on Sep 03, 2020 at 16:47 UTC

    Maybe just mark this code as untested :)


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11121046]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2020-12-05 03:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (63 votes). Check out past polls.

    Notices?