1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Uploaded file overwrites file with same name, help please

Discussion in 'PHP' started by chrisj, Mar 30, 2017.

  1. #1
    The upload script that I'm using works successfully, but if a file is uploaded that has the same name as a file that is already in the destination folder, the new file will overwrite the existing file.

    I'm looking for a simple solution to remedy this, like modifying the uploaded file name, like something with:

    $randomString = time();
    Code (markup):
    Here's part of the current upload code:

    if (@$_POST['submit'] != "") {
    $allowed_ext = array("gif", "jpeg", "jpg", "png", "pdf", "doc", "docs", "zip", "mov", "MOV", "flv", "mp4", "3gp", "3GP");
    $extension = end(explode(".", $_FILES["file"]["name"]));
    if (($_FILES["file"]["size"] < 10485760000) && in_array($extension, $allowed_ext)) {
    if ($_FILES["file"]["error"] > 0) {
    //$message.="There is some error in upload, see: " . $_FILES["file"]["error"] . "<br>";//Enable this to see actual error
    $message.="There is some error in upload. Please try after some time.";
    } else {
    $uploaddir = '../Upload/';
    $uploadfile = $uploaddir . basename($_FILES['file']['name']);
    $uploaded_file = false;
    if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
    {
    $uploaded_file = $_FILES['file']['name'];
    }
    Code (markup):
    Any help will be appreciated.
     
    chrisj, Mar 30, 2017 IP
  2. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #2
    Use this line:
    $uploadfile = $uploaddir . time().'_'. basename($_FILES['file']['name']);
     
    JEET, Apr 5, 2017 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    Or an alternate, with a file exists check:

    $uploadfile = $uploaddir . basename($_FILES['file']['name']);
    if(file_exists($uploadfile )){
    $uploadfile = $uploaddir time().'_'.. basename($_FILES['file']['name']);
    }
     
    JEET, Apr 5, 2017 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    I usually just check to see if the file exists (open it with errors trapped). If it does, throw the user a message about changing the name of the file s/he's uploading.
     
    Rukbat, Apr 9, 2017 IP
  5. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #5
    Why throw an error to the user? You can rename it yourself something(1).jpg something(2).jpg etc...
    Also, any specific reason to open and trap? I think almost all programming languages today have a fileExists check function available
     
    JEET, Apr 9, 2017 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    You could, but if the user needs to know the name of the file, to look at it later, you'd have to send a message with the new name anyway. It depends on whether anyone is going to need the file in the future.
     
    Rukbat, Apr 16, 2017 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    Normally, if a user needs access to the file, that doesn't happen with a direct link. Normally, you'd have an interface showing all files in the user's account, or in a folder, or something like that, giving the user the ability to get to the file regardless of what it's called. Of course, you would keep the submitted filename with a counter on the end, or something - but that is basically the same thing most OSes does with auto-renaming.
     
    PoPSiCLe, Apr 17, 2017 IP
    JEET likes this.
  8. Salmanahmad

    Salmanahmad Greenhorn

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #8
    Write clean and clear code
    no one understand ...you will help from stackoverflow
     
    Salmanahmad, May 3, 2017 IP