Back with another problem...profile image delete.

by 11 replies
13
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?
#programming #back #delete #image #problemprofile
  • 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.
    • [1] reply

    • 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.
      • [1] reply
  • Can you possible give me a quick demonstration with your code? Thanks.
    • [1] reply
    • Huh? I've already told you how to do it. {scratches head}
      • [1] reply
  • Try:

    $path = 'images/record/' . $upload . '.' . $ext;
    $thumb_path = 'images/record/thumb_' . $upload . '.' . $ext;
    unlink($path);
    unlink($thumb_path);
  • 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());
    }

Next Topics on Trending Feed

  • 13

    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?