Closed (fixed)
Project:
ImageField
Version:
5.x-1.x-dev
Component:
Code
Priority:
Critical
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
13 May 2007 at 14:54 UTC
Updated:
26 Oct 2009 at 06:15 UTC
Jump to comment: Most recent file
Comments
Comment #1
buddaCan we not use the same code as Drupal Core does for the user account photos. if file exists, add a numbver to the end +1 ?
Comment #2
bojanz commentedI am guessing this is the same problem that I've had.
The problem is that imagefield checks if the filename exists only when saving the node, so if you upload (via ajax for example) two files of the same name, preview will show the first image for both fields, because the preview address is the same, so Drupal gets confused.
My first try was to add code to _imagefield_widget_prepare_form_values that would check if the filename exists in the filesystem, and session for all fields, and if the filename exists, add +1 to the filename. Repeat until we have a unique filename. So, if we start uploading Playground.jpg to two fields, we would get incrementing filenames, like Playground_1.jpg, Playground_2.jpg etc.
However, my client pointed out that that could get very bad for performance on a big site, users usually don't rename files, so imagine two thousand images called Image.jpg.. That would make the checking code quite slow, since it would execute file_exists two thousand times.
The solution was to add the unix timestamp to the filename. After that, still do the mentioned check and increment the filename if a file with that name exists, just in case two people posted in the same second.
I am attaching the patch for you to consider.
Comment #3
bojanz commentedChanging thread status.
Comment #4
dopry commentedBojanz, can you drop the time thing? And is there a particular reason you're working with filename and not filepath?
Comment #5
bojanz commentedHere's a new patch, removed the time thing, and rerolled against latest HEAD.
I manipulated filename because it seemed more logical, we make the unique filename, and pass it on to filepath...
Hope it's good.
Comment #6
bojanz commentedBy the way, please note the performance concerns:
Users usually don't rename files, so imagine two thousand images called Image.jpg.. That would make the checking code quite slow, since it would execute file_exists two thousand times.
But that's the way Drupal itself does it, so that's it.. Maybe we could provide adding the timestamp as an option?
Comment #7
Robardi56 commentedHi Dopry,
of course you'll decide which patch to apply (the one with the timestamp, and the one without), but I suggest you reconsider the arguments listed by Bojan just above ( http://drupal.org/node/143686#comment-254800 ) on why the timestamp solves many duplicate names and performance problems we encountered while testing the module, and thus is a valuable feature.
Cordially,
Brakkar
Comment #8
sean2000 commentedMy imagefield version is 5.x-1.x-dev (Last updated: June 19, 2007 - 00:15).
I am getting errors when I try to apply the patch unique_filenames.patch.
I am guessing this might be because imagefield.module has changed since. Any help would be greatly appreciated.
Comment #9
bojanz commentedYes, this patch will have to be rerolled against latest HEAD... However, I'm traveling to Spain in a while, so I won't have time till I return (July 15th)..
Comment #10
sean2000 commentedThanks for your prompt response. If anyone else can help me with this earlier than that, that'd be ace. I'll try figuring it out myself if I can. If not, I'll wait till bojanz is free. Thanks.
Comment #11
bojanz commentedJust came back from holiday, reread this thread, and realised that sean2000 is trying to apply the wrong patch, I already posted a patch against the latest HEAD: http://drupal.org/node/143686#comment-260479
So, Sean, please apply unique_filenames_0.patch
Bojan
Comment #12
bojanz commentedUploading new patch to fix bug mentioned at http://drupal.org/node/144475
Comment #13
dopry commentedI can't reproduce this bug... And it does use the same code as core to create unique filenames. Can anyone confirm this is an issue?
Comment #14
ericpughChecking out the patch. This has been a problem on a site of mine, but with special circumstances. Basically, I have imagefield with imagecache, and if a user uploads a bitmap image it is not processed by imagecache (a seperate problem) however, the image is also not renamed, and has caused all kinds of strange behavior. Images with the same name replacing each other, etc.
Point is... I think that unique filenames would be a good idea, just because of these kinds of "special circumstances." Thanks all.
Comment #15
ximo commentedWe're having the same problem as #14. We allow our users to upload photos of themselves for their profiles, and with nearly 4000 users, it's unfortunate that some users get another user's photo on their profile.. If anybody have a solution for this, please let me know.
Comment #16
reed.richards commentedChanging this to critical.
Comment #17
reed.richards commentedI've tried this in version 5.x-2.1 and it doesn't seem to be a problem. If someone could confirm that this has been fixed for the 5.x-2.1 version, then maybe we finally could close this issue?
Comment #18
quicksketchThe 1.x version is no longer supported and this has been fixed in 2.x. Let's close this out.
Comment #19
goodghost commentedthanx for patch, works for me :)