SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.
    OR.
    Is there a better way..


    <?php
    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>";
    $file_upload="false";}

    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>";
    $file_upload="false";}

    $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";}

    ?>

  2. #2
    SitePoint Enthusiast chovy's Avatar
    Join Date
    Sep 2006
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if exists , then remove it? unlink i think or something similar.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    move_uploaded_file() will overwrite any existing file.

    you should see the documentation for how to do a file upload.
    http://www.php.net/manual/en/features.file-upload.php


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
  •