SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    823
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Unlink 2 files at once in two different folders

    Is there a way to modify this so that not only the called file to be deleted is gone in one folder, but the associated file is deleted as well?

    $image_path = '../../images/gallery/';
    if (isset($_POST['image']) && $_POST['image']) {
    @unlink($image_path.$_POST['image']);
    }

    SCENARIO:I have a file upload that automatically creates a thumbnail from the original and stores it in a 'thumbs' folder and there's a '_thb' added at the end of the name before the '.jpg' extension. The original image is stored in a different folder called 'gallery/' and it has the same name as the '_thb' one, excluding the '_thb'.

    I have no problem deleting the original, but I don't know how to delete the associated '_thb' file. The original file's filename is stored in a db table, the thumb is not.

    Thank you for your time!
    toad78

  2. #2
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Peterborough, Ontario, Canada
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just unlink the second file.
    If it has a prefix that remains the same length use substr()
    Have I helped you? You could help me.
    Like my business on facebook!
    Text message polls with real-time, live results.

  3. #3
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    823
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I attempted to do that by doing this

    $image_path = '../../images/gallery/';
    $image_thumb = '../../images/gallery/thumbs/';
    if (isset($_POST['image']) && $_POST['image']) {
    @unlink($image_path.$_POST['image']) && @unlink($image_path.$_POST['image']);
    }

    But it didn't work because the thumbnail file that has a similar name in the 'thumbs' folder has the added '_thb' at the end of it; so it doesn't get deleted. The only conclusion I've come upon is that the scaled-down copy that is created and moved to the 'thumbs' folder shouldn't be renamed with the '_thb' at the end in order to make the above work.

    Any other advice?

  4. #4
    SitePoint Enthusiast jwh335's Avatar
    Join Date
    Dec 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you do something like:
    Code PHP:
    $image_path = '../../images/gallery/';
    $image_thumb = '../../images/gallery/thumbs/';
    $thumb_arr = explode('.', $_POST['image']);
    $thumb_file = thumb_arr[0] . "._thb." . thumb_arr[1];
     
    if (isset($_POST['image']) && $_POST['image']) {
        @unlink($image_path.$_POST['image']) && @unlink($image_thumb.$thumb_file);
    }


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
  •