Results 1 to 3 of 3

Thread: An image upload Situation

  1. #1
    Join Date
    Apr 2007
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default An image upload Situation

    I am using this upload file script:

    Code:
    <? 
    // Where the file is going to be placed 
    
    $target_path = "images/"; 
    
    /* Add the original filename to our target path. Result is "uploads/filename.extension" */ 
    
    $target_path = $target_path . basename( $_FILES['Image1']['name']); 
    $_FILES['Image1']['tmp_name'];  
    
    $target_path = "images/"; 
    
    $target_path = $target_path . basename( $_FILES['Image1']['name']); 
    
    if(move_uploaded_file($_FILES['Image1']['tmp_name'], $target_path)) { 
    
        echo "The file ".  basename( $_FILES['Image1']['name'])." has been uploaded"; 
    
    } else{ 
    
        echo "There was an error uploading the file, please try again!"; 
    } 
    
    ?>
    But everytime someone uploads the same image name, the image is over-written.

    So this is a little problem because users might upload same image names

    How can i prevent the script from overwritting old images

  2. #2
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Code:
    $target_path = "images/"; 
    if (file_exists($targetpath)) {
    $target_path = $target_path . "a" . basename( $_FILES['Image1']['name']); 
    } else {
    $target_path = $target_path . basename( $_FILES['Image1']['name']);
    }
    if(move_uploaded_file($_FILES['Image1']['tmp_name'], $target_path)) { 
    
        echo "The file ".  basename( $_FILES['Image1']['name'])." has been uploaded"; 
    rand()
    } else{ 
    
        echo "There was an error uploading the file, please try again!"; 
    } 
    
    ?>
    Changes in red.

    Adds an "a" infront of the filename, so if you want to upload file.jpg and it already exists, you have afile.jpg
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  3. #3
    Join Date
    Apr 2006
    Posts
    429
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    or go to this site
    Please don't mind me. I am just posting a lot of nonsense.

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
  •