Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

uploading a file with the same name

by katgirl (Hermit)
on Jul 24, 2002 at 13:38 UTC ( #184827=perlquestion: print w/replies, xml ) Need Help??
katgirl has asked for the wisdom of the Perl Monks concerning the following question:

How can I make sure that when people upload files, they always have a different name, so that they don't wipe off the old file? Is this a good way:
#$imagename is name of file $num=0; while(-e "$Data/$category/$imagename"){ $num++; $imagename=$imagename$num; }
My brain is starting to fizz. Please help, oh lovely fellow monks!

Replies are listed 'Best First'.
Re: uploading a file with the same name
by Abigail-II (Bishop) on Jul 24, 2002 at 13:51 UTC
    That would be a very, very bad way of dealing with this problem. If two people want to upload a file with the same name at the same time, you will get problems as your program doesn't run atomically.

    Keep a counter in a database, or a properly locked file and use that.


      Or use the process ID as a portion of the name, ie image-$PID-$INCREMENTING_COUNTER, that should do the trick. So long as the operation is atomic within the PID it'll be fine (which it is).
        Not necessarely. For instance if you are using a threaded webserver in combination with mod_perl. Whether each thread inside the same process get its own PID depends on the OS.


Re: uploading a file with the same name
by kodo (Hermit) on Jul 24, 2002 at 13:45 UTC
    Hi katgirl,

    Yes this would work pretty fine. You could use -s instead of -e because most likely you want the file to be overwritten when it's empty. But I usually give the people who want to upload it a message then and ask them to use another name, because in lots of cases people also want to download their files again, and when there are 10 files a la etc. people might start to wonder which one theirs was.
    Another good way is, if you have a username or something to hang the username at the end of the filename before counting numbers up, but that depends on the kind of usage you have for your script...
    Oh yea and you could make this one a bit more obfuscating with $imagename = $imagename.++$num;

      $num=1; $imagename=$imagename. ++$num while -s "$Data/$category/$imagename";
      Perhaps consider using an MD5 Sum to see if the files are the same too, to negate duplicate uploads ;)


      Brother Frankus.


      it's for an image gallery: so if the person needs to find their picture, they can just look for it. I wrote the entire thing myself, and I'm only just realising some of the things I should have put in there when I started...
Re: uploading a file with the same name
by talexb (Canon) on Jul 24, 2002 at 13:53 UTC
    merlyn has written a column that applies: try reading it and see what you can learn.

    --t. alex

    "Mud, mud, glorious mud. Nothing quite like it for cooling the blood!"
    --Michael Flanders and Donald Swann

Re: uploading a file with the same name
by katgirl (Hermit) on Jul 24, 2002 at 14:13 UTC
    Thank you all for your suggestions! I used amphiplex's suggestion, with a small alteration:
    $num=""; while(-e "$Data/$category/$num$imagename"){ $num++; } $imagename = $num.$imagename;
    That works great (although the other suggestions would have worked just as well, but I believe in KISS!) I'll have a look at merlyn's column once my brain has stopped being jelly-like...
      Nice to hear that :), even though my answer was partly wrong (not the code, I hope, but the part about yours checking for foo0)

      ---- amphiplex
      One thing to look out for is that if you are expecting lots of images and usage , most 'NIXs do linear scans of directories, meaning that with a lot of images, it can get very, very slow. If you ever used Cache::FileCache you might notice the way it uses digests of the names (for fairly random distribution) and splits the storage into sub-directories. /dir/a/ab/abcd/abcdefghijk.gif

      If you don't expect that kind of usage, I would just go the simple route. You can always modify it later. (Though if people are linking to this stuff, you might want to think about it now.)


      "To be civilized is to deny one's nature."
Re: uploading a file with the same name
by cybear (Monk) on Jul 24, 2002 at 15:20 UTC

    I like your work ethic, continual improvement, but I have to
    side with Abigail-II here. As Aby (can I call you Aby?) points
    out. Using this script will not keep anyone from putting a file
    with duplicate names on you site. I would just give you a way
    of fixing the problem.

    If your on a unix box try setting the sticky bit on your upload
    directory. That should keep anyone but the owner of a file from
    over-writing a file that already exists.

    Of course, if sounds like you are staging the data before allowing
    it to be posted... which is very wise.

Re: uploading a file with the same name
by amphiplex (Monk) on Jul 24, 2002 at 13:49 UTC
    This will not do what you want.
    suppose you want to upload the file foo and the file already exists. Your code would check for foo0, which propably does not exist, and overwrite foo.

    Besides, you append th number, giving you: foo0,foo01,foo012,....
    And to concatenate strings you should use ".".

    One way to do it:
    $num=""; while(-e "$Data/$category/$imagename$num"){ $num++; } $imagename = $imagename.$num;

    ---- amphiplex

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://184827]
Approved by wil
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2019-01-21 21:42 GMT
Find Nodes?
    Voting Booth?
    After Perl5, I'm mostly interested in:

    Results (370 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!