Robinson
Robinson

Reputation: 21

PHP how to skip file upload if file already exist

I have a problem with a file upload form. If I submit the form without uploading any file. It thinks the file was uploaded when it was missing.

if(isset($_FILES["user_image"]))
{
    $extension = explode('.', $_FILES['user_image']['name']);
    $new_name = rand() . '.' . $extension[1];
    $destination = '../upload/' . $new_name;
    move_uploaded_file($_FILES['user_image']['tmp_name'], $destination);
    return $new_name;
}

Upvotes: 1

Views: 209

Answers (1)

shafeeque ahmad
shafeeque ahmad

Reputation: 97

you can do this by various ways here is the simplest way to do this

if(isset($_FILES["user_image"]) && (int)$_FILES["user_image"]['error']===0)
{
    $extension = explode('.', $_FILES['user_image']['name']);
    $new_name = rand() . '.' . $extension[1];
    $destination = '../upload/' . $new_name;
    move_uploaded_file($_FILES['user_image']['tmp_name'], $destination);
    return $new_name;
}

Upvotes: 3

Related Questions