SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    delete images from folder

    I was wondering how to delete images and it's thumbnails from a directory.

    I've seen that unlink() should be used, but I'm not sure how to implement it into this code.

    I have checkboxes for deleting multiple rows in the database. It works fine with the database info. But I'd also like to delete the images from the directory that are associated with those IDs.

    Below are the code from two pages. The first the form for the checkbox
    And then the delete page.

    1.
    <form method="post" action="remove_photo.php">
    <input type="hidden" name="submit" value="1">
    <input type="checkbox" name="variable[]" value="$photo_id" /></TD>

    2.
    PHP Code:

    if(isset($_POST['submit'])){
    $delcount count($_POST['variable']);

    if(
    $delcount 0){
    $idlist implode(',',$_POST['variable']);
    $query "DELETE FROM photographs WHERE photo_id IN($idlist)";

       
    $result mysql_query($query);
        
    //Check whether the query was successful or not
    if($result) {
    header("location: photo.php");
    exit();
    }else {
    die(
    "Query failed");
    }

    }

    Can anyone help me how to use unlink() with the above code


    Here are some piece of code for the uploading of a file. Don't know if that info is needed?
    PHP Code:
    $pPath "../photos/";

    $filename $pPath.$_FILES['thefile']['name'];
    $thumbname "TN_".$_FILES['thefile']['name'];
    $thumbnail $pPath.$thumbname
    Thanks

    Bjorn

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to first query the database using the id, to get the filename. Then, you can delete the file, and then the record.

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you give an example of what that looks like with the code I showed?

    Thanks

    Bjorn

  4. #4
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I fixed the unlink part. I added the following code and it deletes all images selected from the checkboxes

    PHP Code:

    $QUERY1 
    mysql_query("SELECT * FROM photographs WHERE photo_id IN($idlist)");
    $ROWS mysql_num_rows($QUERY1);

    if (
    $ROWS) {
    $I 0;
    while (
    $I $ROWS) {

    $photo_path mysql_result($QUERY1,$I,"photo_path");
    $thumb_path mysql_result($QUERY1,$I,"thumb_path");

    unlink("../$photo_path");
    unlink("../$thumb_path");

    $I++;
    }

    Thanks for your help anyway to decide the order how to do it. That helped.

    Bjorn


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
  •