How to avoid the image replacement while saving images to a folder via PHP

Discussion in 'HTML & Website Design' started by nomanz, Jul 8, 2010.

  1. #1
    i am new to php and i find lots of help on the internet to uplaod a file to a folder.
    i am developing a website which need contineous uploading of images to a folder.i want if the image already exists in the folder the name of the new image that i am going to upload can change autometically.

    i mean if there is a image named car.jpg in the folder and i again upalod the image of car.jpg the 2nd image renamed and save as car(2).jpg what to do to achieve this.. regard
    Noman Zahid
     
    nomanz, Jul 8, 2010 IP
  2. Kirkbride

    Kirkbride Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The easiest way to do this would be to make every file name unique. The easiest way to do that is probably to add a time stamp to each file's name. So instead of car.jpg and car(2).jpg, you'd have car-1234567890.jpg and car-1234567891.jpg.

    Use PHP's time() function to get the time stamp. Something along the lines of

    $timestamp = time();
    $filename = 'car';
    $extension = '.jpg';
    $full_filename = $filename . '-' . $timestamp . $extension;

    Another more complicated option would be to check to see if a file with the same name exists before saving it. Use PHP's file_exists() function for that. You'll have to make sure your script takes into account that there may already be a car(2).jpg as well as a car.jpg in which case you then create a car(3).jpg file. Not sure how you'd handle that but it would probably involve writing a function that runs a loop incrementing the number, then checking for an existing file with that number and only stopping the loop when it doesn't find an existing file. I hope that's understandable the way I wrote it.
     
    Kirkbride, Jul 8, 2010 IP
  3. nomanz

    nomanz Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    plz have a look to my code....

    if ((($_FILES["file1"]["type"] == "image/gif")
    || ($_FILES["file1"]["type"] == "image/jpeg")
    || ($_FILES["file1"]["type"] == "image/pjpeg"))
    && ($_FILES["file1"]["size"] < 2000000))
    {
    if ($_FILES["file1"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file1"]["error"] . "<br />";
    }
    else
    {

    if (file_exists("front/" . $_FILES["file1"]["name"]))
    {
    echo $_FILES["file1"]["name"] . " already exists. ";
    }
    else
    {
    move_uploaded_file($_FILES["file1"]["tmp_name"],
    "front/" . $_FILES["file1"]["name"]);
    }
    }
    }
    else
    {
    echo "Invalid file";
    }



    I am using the file name that i have uploaded to a folder into my database....



    here is my database query


    $insert1 ="INSERT INTO `front_image`( `name`) VALUES ('".$_FILES["file1"]["name"]."');


    now i didnt understand how can i save the image with the current timespam

    plz help me and modify the code as i tries it but it gives me systax error :(
     
    nomanz, Jul 8, 2010 IP
  4. Kirkbride

    Kirkbride Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You should create a new variable called $filename (or something similar) and give it the value of $_FILES["file1"]["name"]. I don't have time to test out this code, but it would be something like this:

    $filename = $_FILES["file1"]["name"];
    $filename = preg_replace("/\.jpg/", '', $filename);
    $timestamp = time();
    $full_filename = 'front/' . $filename . '-' . $timestamp . '.jpg';

    Put that after the first '} else {' and then use $full_filename in place of $_FILES["file1"]["name"] from then on.

    Note, the code I just wrote hasn't been tested and can probably be improved considerably, but I think it'll work. Also note that for simplicity's sake I just assumed the file was a JPEG, but you'll have to take into account that it might be a GIF or some other kind of image file type you want to accept.
     
    Kirkbride, Jul 8, 2010 IP