Results 1 to 6 of 6

Thread: append filename

  1. #1
    Join Date
    Feb 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default append filename

    Hey, i have a page where users upload images, everything fine. However if a file with the same name is uploaded i just

    PHP Code:
    echo 'An image with that name already exists, please change the name and upload it again'
    However i would prefer it if the name appends, for example if FILE.gif exists it renames the image that is being uploaded to FILE1.gif and so on.

    Thanks.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Something like:
    Code:
    if(file_exists($imagename)) {
      while(file_exists($imagename . @++$i));
      $imagename .= $i;
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Of course, adapting that would include something like:
    Code:
    fwrite(fopen($file,$handle),$filename . $imagename);
    Depending on how you write the files.
    - Mike

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No it wouldn't... I'm assuming $imagename is the name of the file to write to. It's a cut-and-paste job.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Wouldn't $imagename return the number?
    - Mike

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
      $imagename .= $i;
    Note the dot.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •