Replace Image in upload Script

I have two questions related uploading pics from a web page.

  1. I tried installing this script and it works. However If the picture already exists I want it to replace the file.
    Here is my script so far, and it lets me know if the file exists, but then does not replace the original jpg.
    The end result is that I want an administrator to upload a pic, and it will replace the picture on a webpage. So…If they name the pic for the index page always…index.jpg, it will replace the index.jpg picture with the new index.jpg picture on the index page.
  2. Would it be better to save the image to a database, and then pull it from the database…by telling it to display the last file added to the database.
    Is there a better way…

if ($userfile_size >250000)
{$msg=$msg.“Your uploaded file size is more than 250KB so please reduce the file size and then upload. Visit the help page to know how to reduce the file size.<BR>”;

if (!($userfile_type ==“image/pjpeg” OR $userfile_type==“image/gif”)){$msg=$msg.“Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>”;

$add=“upload/$userfile_name”; // the path with the file name where the file will be stored, upload is the directory name.

if (file_exists(“upload/” . $_FILES[“userfile”][“name”]))
echo $_FILES[“userfile”][“name”] . " already exists. ";

if(move_uploaded_file ($userfile, $add)){
// do your coding here to give a thanks message or any other thing.

}else{echo “Failed to upload file Contact Site admin to fix the problem”;}


if exists , then remove it? unlink i think or something similar.

In that case I would need to put it in a database, which is probably what I should do. I was trying to keep it simple, but in the end it would probably be better for admin purposes.

move_uploaded_file() will overwrite any existing file.

you should see the documentation for how to do a file upload.