• I just upgraded to 2.3.2 and then 2.3.3. With both versions I have this problem. I take a screen caputre with my mac and it’s saved as something like “Picture 1.png”. I write a blog post and use the image upload feature to upload that picture to put it in my post. It makes a thumbnail and everything just fine.

    If I delete the Picture 1.png from my desktop and take another screen capture, the new image is also saved as “Picture 1.png”. If I want to use that in a new blog post, and I repeat the process, it uploads and OVERWRITES THE PREVIOUS IMAGE. Thus, my earlier blog post has its image overwritten with the new one.

    This was not happening with the old version of WordPress I was using. I’ve searched high and low for solutions, and even tried to find a way to adjust this in the code, but I’ve had no luck.

    Can any tell me (1) how to fix this, or (2) where I can go in the code to do something like append a timestamp to the file names to work around this?

    Thanks,
    Jen

Viewing 5 replies - 1 through 5 (of 5 total)
  • Am I missing something? Why don’t you just change the filename of the screenshot before you upload it so that you don’t overwrite the file?

    PS — I haven’t used 2.3 yet, but I’m guessing that it may have to do with way uploads are stored.

    In earlier versions you had the option to organize uploads by date — if you had that option set, you wouldn’t overwrite your file if you uploaded it on a different day, because it would be stored in a separate folder. This setting is under Options->Miscellaneous.

    Thread Starter golbeck

    (@golbeck)

    I’ve looked at the Options->Mixcellaneous, but it only organizes by year and month, not by date.

    I could rename the pictures, but the thing is that I do all my pictures in this screen capture way (it makes a good web-sized big version quickly, so I don’t have to do anything to resize the images off my camera). Thus, pretty much every day or two I’ll have another Picture 1.png, and it would be much more convenient if they would just be renamed when I upload. That seems like the correct thing to do; I wouldn’t think the default behavior when uploading a picture on a new post should be to overwrite an image used in a old post…even if the pictures have the same name.

    i’ve seen the same problem, and it’s frustrating!

    we often scan and upload a picture and repeat, because it needed correcting. it’s when you repeat that the file is overwritten because there is a case where wordpress can’t tell that you have already uploaded the file and overwrites it.

    to repeat the problem you need:
    1. a file system with case sensitive file names – ie ext3, not mac os x’s hfs.
    2. to upload a file with Upper case characters twice.
    3. wordpress 2.3 and above (the trunk version still has the same problem)

    in our case we’re using wordpress 2.3.3 with the plugin yet-another-photo-blog. yet-another-photo-blog delegates the uploading to wordpress and it is wordpress that overwrites the file. yet-another-photo-blog makes it worse afterwards because it attempts to remove the previous image, which due to the overwriting, is actually the new image. you can’t fix that one without putting the file back manually.

    the problem code seems to be in wp-admin/includes/file.php::wp_handle_upload for wordpress 2.3.3, while in the trunk it was moved (but not fixed) to wp-includes/functions.php::wp_unique_filename.

    here is a snippet of the existing code:

    $number = '';
    $filename = str_replace( '#', '_', $file['name'] );
    $filename = str_replace( array( '\\', "'" ), '', $filename );
    if ( empty( $ext) )
    $ext = '';
    else
    $ext = ".$ext";
    while ( file_exists( $uploads['path'] . "/$filename" ) ) {
    if ( '' == "$number$ext" )
    $filename = $filename . ++$number . $ext;
    else
    $filename = str_replace( "$number$ext", ++$number . $ext, $filename );
    }
    $filename = str_replace( $ext, '', $filename );
    $filename = sanitize_title_with_dashes( $filename ) . $ext;

    the issue is that the code searches for a unique file name using the test file_exists and then after finding a unique file name, it goes and changes the name using the method sanitize_title_with_dashes to one, that is not unique. oops. this is tricky because the method does stuff like convert all the letters to lower case, which on some filesystems doesn’t affect the uniqueness of the file. but on unix-like filesystems it does.

    so here’s what happens:
    user uploads PICTURE.jpeg
    wordpress searches for PICTURE.jpeg
    wordpress does not find it
    wordpress renames PICTURE.jpeg to picture.jpeg
    wordpress saves the file picture.jpeg

    and then..
    user uploads PICTURE.jpeg
    wordpress searches for PICTURE.jpeg
    wordpress does not find it (because the letters are different cases)
    wordpress renames PICTURE.jpeg to picture.jpeg
    wordpress overwrites picture.jpeg

    i have implemented a quick fix in our site by moving the last two lines to before the unique tests.


    $number = '';
    $filename = str_replace( '#', '_', $file['name'] );
    $filename = str_replace( array( '\\', "'" ), '', $filename );
    if ( empty( $ext) )
    $ext = '';
    else
    $ext = ".$ext";
    $filename = str_replace( $ext, '', $filename );
    $filename = sanitize_title_with_dashes( $filename ) . $ext;

    while ( file_exists( $uploads['path'] . "/$filename" ) ) {
    if ( '' == "$number$ext" )
    $filename = $filename . ++$number . $ext;
    else
    $filename = str_replace( "$number$ext", ++$number . $ext, $filename );
    }

    so the important thing here is that the file_exists test happens every time after the filename is changed. this will prevent wordpress from overwriting our important files! anyone want to fix this?

    THANKS yeungda. I have been looking all over for a solution to this. It was making me mad. I was about to dive in myself and try and figure it out. Tuns out it was a fairly simple fix, but nonetheless very effective.

    I hope you don’t mind but I made a post on my blog outlining the solution with a link back to here and your website.

    -Nick
    https://www.nicklarson.com

    all the best with your site nick! and thanks for helping get the word out.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Image Upload Overwrites Existing Images’ is closed to new replies.