Back with another problem...profile image delete.

by man5
11 replies
I have a setup where I can upload an image to a directory(creates original and thumb_ original) and store it's thumb_filepath in the mysql database. Works great.

Now the problem is that every time I upload a new image, the old images(orginal and thumb_original) are being kept in the folder directory. I would like to know if there is a way to delete them and keep only the current uploaded image?
#back #delete #image #problemprofile
  • Profile picture of the author David Beroff
    Um, sure, why not?

    I'd probably keep the old file path(s) before the new upload, and not delete it until the upload is complete, just in case.
    Signature
    Put MY voice on YOUR video: AwesomeAmericanAudio.com
    {{ DiscussionBoard.errors[8957211].message }}
    • Profile picture of the author man5
      Originally Posted by David Beroff View Post

      Um, sure, why not?

      I'd probably keep the old file path(s) before the new upload, and not delete it until the upload is complete, just in case.

      I should make it clear that deleting the file path(s) isn't an issue. It's deleting the previous image(s) in the upload folder.

      Let me give you an example.

      1. A user is registered and logged in.

      2. The user has the option of uploading his profile image.

      3. Once he uploads his profile image, the file path is stored in his users table and the image(orginal image and thumb_ image) is uploaded in an images folder directory.

      4. If the user uploads a new profile image, it'll update the database with new file path and also create new instances of those new images in the images folder directory. All good so far.

      5. Now here is where the problem lies. The very first profile image he uploaded is still in the images folder directory. What I would like to know is how to remove that first profile image so that no matter how many times i updat my profile image, the older images in the folder should be overwritten with the new image.
      Signature
      {{ DiscussionBoard.errors[8957287].message }}
      • Profile picture of the author David Beroff
        Right; what I'm saying is that you need to store the old info before you overwrite it with the new info, so you can delete the file(s) once the upload is complete.
        Signature
        Put MY voice on YOUR video: AwesomeAmericanAudio.com
        {{ DiscussionBoard.errors[8957340].message }}
  • Profile picture of the author man5
    Can you possible give me a quick demonstration with your code? Thanks.
    Signature
    {{ DiscussionBoard.errors[8957471].message }}
    • Profile picture of the author David Beroff
      Huh? I've already told you how to do it. {scratches head}
      Signature
      Put MY voice on YOUR video: AwesomeAmericanAudio.com
      {{ DiscussionBoard.errors[8957748].message }}
      • Profile picture of the author man5
        Originally Posted by David Beroff View Post

        Huh? I've already told you how to do it. {scratches head}
        I think we are misunderstanding each other.

        From my research, what i am looking for is something called "unlink", which unlinks the images in the folder directory; thus deleting them.
        Signature
        {{ DiscussionBoard.errors[8957991].message }}
        • Profile picture of the author David Beroff
          OK, so use it, then.
          Signature
          Put MY voice on YOUR video: AwesomeAmericanAudio.com
          {{ DiscussionBoard.errors[8958017].message }}
          • Profile picture of the author man5
            I've tried the unlink but it's not working. Here look at the code and tell me how you would apply it.

            delete.php
            PHP Code:

            <?php
            require_once 'core/init.php';

            try {
                                        
                 = new 
            User();
                 = 
            DB::getInstance()->delete('guestbook', array('name''=',   ()->username));
                
                        =   
            'images/record/' .  . ;
                       =   
            'images/record/thumb_' .  . ;
                            

                
            Session::flash('home''your record has been deleted');
                
            Redirect::to('index.php');
                                        
            }     catch(
            Exception ) {
                die(());
            }

            ?>
            For some reason these some variables aren't showing up. So here is the raw paste as well.

            <?php
            require_once 'core/init.php';

            try {

            $user = new User();
            $delete = DB::getInstance()->delete('guestbook', array('name', '=', $user->data()->username));

            $path = 'images/record/' . $upload . $ext;
            $thumb_path = 'images/record/thumb_' . $upload . $ext;


            Session::flash('home', 'your record has been deleted');
            Redirect::to('index.php');

            } catch(Exception $e) {
            die($e->getMessage());
            }
            Signature
            {{ DiscussionBoard.errors[8958053].message }}
  • Profile picture of the author KirkMcD
    Try:

    $path = 'images/record/' . $upload . '.' . $ext;
    $thumb_path = 'images/record/thumb_' . $upload . '.' . $ext;
    unlink($path);
    unlink($thumb_path);
    {{ DiscussionBoard.errors[8959074].message }}
  • Profile picture of the author man5
    Here's a new test i started. I kept it basic for now. So now perhaps you might be able to tell me what I am doing wrong.

    upload.php

    if (isset($_FILES['image'])) {
    if (empty($_FILES['image']['name'])) {
    echo 'Please choose a file!';
    } else {

    //Preparing the variables
    $name = $_FILES['image']['name'];
    $temp = $_FILES['image']['tmp_name'];
    $path = 'images/';

    $location = $path.$name;

    try {

    $user = new User();
    $data = DB::getInstance()->insert('guestbook', array(
    'name' => $user->data()->username,
    'path' => $location,
    'message' => Input::get('message'),
    'posted' => date('Y-m-d H:i:s')
    ));

    // Move the file.
    move_uploaded_file($temp, $location);



    Session::flash('home', 'You have created a new record!');
    Redirect::to('upload.php');

    } catch(Exception $e) {
    die($e->getMessage());
    }



    }
    }





    Here is the delete.php


    require_once 'core/init.php';


    $user = new User();

    // upload variables
    $name = $_FILES['image']['name'];
    $temp = $_FILES['image']['tmp_name'];
    $path = 'images/';
    $location = $path.$name;


    try {

    if (file_exists($location)) {
    if(is_writable($location)) {

    if(unlink($location)) {
    echo "Successfully deleted " . $location;
    }
    else {
    echo "Problem deleting " . $location;
    }
    }
    }


    $delete = DB::getInstance()->delete('guestbook', array('path', '=', $user->data()->username));


    Session::flash('home', 'your record has been deleted');
    Redirect::to('test.php');

    } catch(Exception $e) {
    die($e->getMessage());
    }
    Signature
    {{ DiscussionBoard.errors[8959331].message }}

Trending Topics