Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Changing A File Name On The Fly

by Milti (Beadle)
on Dec 18, 2013 at 19:01 UTC ( [id://1067677]=perlquestion: print w/replies, xml ) Need Help??

Milti has asked for the wisdom of the Perl Monks concerning the following question:

I have a web site where users can login and post a profile. I want them to be able to upload their picture to be included with their description. I know how to use a file upload program but I want to rename the image file when/as it is being uploaded and have no idea how to do that.

The person uploading the image file will have a unique "Account ID" such as $AccountID = "$FORM{'field1'}"; and I want the resultant image file in the database to be named pix$accountID.

Can someone provide assistance and point me to an example somewhere as to how to accomplish this task? Any assistance will be greatly appreciated.

Milt

Replies are listed 'Best First'.
Re: Changing A File Name On The Fly
by VincentK (Beadle) on Dec 18, 2013 at 19:42 UTC
Re: Changing A File Name On The Fly
by ww (Archbishop) on Dec 18, 2013 at 22:43 UTC

    ... And how do you propose to protect your site and guests from malware uploaded as if it were (or, embedded in) a picture?

    If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.

    Quis custodiet ipsos custodes. Juvenal, Satires

      Good point, and something I hadn't considered. Would opening and then writing the file in Image::Magick or some other image module protect against this?
        Possibly, but not likely. WAG: perhaps one time in a hundred... but I may be off by several orders of magnitude.

        For better ideas, search the Monastery for prior discussion -- IIRC, several threads offer suggestions of ways to sandbox uploads -- especially pix.


        Quis custodiet ipsos custodes. Juvenal, Satires

Re: Changing A File Name On The Fly
by marinersk (Priest) on Dec 19, 2013 at 00:24 UTC

    When I do CGI file uploads, I open the output file in my script. So I would simply open it with the filename I want.

    You don't indicate how you are uploading the file, so I don't know if this advice helps you.

    # Now upload the file $inpfsz = 0; open(RESFIL, '>', $resfnm); binmode(RESFIL); while ($inpcnt=read($mntfnm,$inpbuf,2096)) { $inpfsz += $inpcnt; print RESFIL $inpbuf; } close(RESFIL);

    Edit: Moved binmode outside the write loop. Noob error LOL.

Re: Changing A File Name On The Fly (CGI?)
by Anonymous Monk on Dec 18, 2013 at 22:23 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1067677]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (None)
    As of 2024-09-07 17:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?
      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.