SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $unlink'ing photos

    Hi Guys,

    what i'm trying to do here is unlink(delete) the previously uploaded photo by the user, before the new one is uploaded, but i'm having trouble with the unlink bit

    here the code:

    PHP Code:
    <?php    
      $photo1 
    $_FILES["image"]["name"];
      
    $username mysql_escape_string($_POST["username"]);
      
    $max_image_size 512000
      
    $allowed = array("image/gif","image/jpg","image/png","image/jpeg");
      
    $max_image_width 500;
      
    $max_image_height 450;
      
    $random_number rand(0000,9999);
      
      
    // The renamed photo...////////////////////////////////////////////////////////////////
      
    $renamed_photo $random_number.$_FILES["image"]["name"];   
      
      
    // Check there wasn't a blank submission...////////////////////////////////////////////
      
    if(empty($_FILES["image"]["name"])) {
      
           echo 
    "<p>Sorry, But No File Was Selected, Please Go Back And Try Again.</p>";
           exit;  
      }
      
      
    // Validation for Image...
      
    if ($_FILES["image"]["size"] > $max_image_size) {
      
       echo 
    "<p>Sorry <b>$username</b> That Image Is Bigger Than The Allowed Size Of 3mb Please <a href=\"java script: history.go(-1)\">Go Back</a></p>";
       exit;
       
      }
      
      
    #####################################################################
      // Validate image dimensions...////////////////////////////////////////////////////////
      
    $dim getimagesize($_FILES["image"]["tmp_name"]);

      if(
    $dim[0] >= $max_image_width || $dim[1] >= $max_image_height) {
      
        echo 
    "<p>Sorry, That Image Isn't Within The Current Upload Dimensions Please Go Back And Upload Another!</p>";
        exit;
        
      }  
      
    // Validate image types.../////////////////////////////////////////////////////////////
      
    if (!in_array($_FILES["image"]["type"], $allowed)) {
      
        echo 
    "<p>Sorry, That File Isn't One Of The Allowed File Types Only <b>.gif .jpg & .png</b> Are Allowed.</p>";
        exit;
        
      }  
      
    #####################################################################
      
      
    $uploadpath "uploads/"// <- Upload folder...
      
    $uploadpath $uploadpath.$renamed_photo;

      
    #####################################################################  
      
    if (!move_uploaded_file($_FILES["image"]["tmp_name"], $uploadpath)) 
      
         die(
    "<p>Sorry, There Was An Error Uploading Your Image!");
         echo(
    "<p><br />The Image (<b><font color=\"red\">" .$_FILES["image"]["name"]. "</b></font>) Has Been Uploaded Successfully! <a href=\"my_account.php\">Back To My Account</a><br />");
         
        
      
    // Create our query.../////////////////////////////////////////////////////////////////
      
    $sql "UPDATE membership SET photo1='$renamed_photo' WHERE username='$username'";

      
    // Run our query...////////////////////////////////////////////////////////////////////
      
    $rs mysql_query($sql$conn) or die(mysql_error());
    ?>
    any help would be appreciated.

    cheers

    Graham

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Since you're assigning random names, you'll want to query the database for the existing filename if one exists, then unlink() it. What part do you need help with?

  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Dan,

    sorry to be a total noob but this bit: "query the database for the existing filename if one exists, then unlink() it"

    even a tutorial would be great, normally if i see something done once i take notes on how it was done.

    thanks mate

    Graham

  4. #4
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What Dan is saying, since you are using random numbers to assign names, you must query your database to get the previous image name. Something like this:
    PHP Code:
    $result mysql_query("SELECT photo1 FROM membership WHERE username='".$username."' LIMIT 1");
    $row mysql_fetch_object($result);
    $previous $row->photo1;
    unlink("./upload/".$previous);
    // Now upload the new image and update the database. 
    - the lid is off the maple syrup again!

  5. #5
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah i see, because i added the random numbers, i implemented your code notepad it works very well so i need to query the database find the previous photo THEN unlink.

    thanks guys

    Graham


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
  •