Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Delete Image

  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Posts
    153
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Delete Image

    Hey Guys,

    I have made a gallery but there is image in the gallery folder that are not in the sql database. I would like to delete these.

    This is what i have done so far.

    PHP Code:
    <?php
    $dir 
    "upload/";
    $dh opendir($dir);
    while ((
    $file readdir($dh)) !== false) {

    $sql9 "SELECT * FROM gallery";
    $result9 mysql_query($sql9$conn) or die(mysql_error());
    while (
    $newArray mysql_fetch_array($result9)) {

    $gallery_image $newArray['gallery_image'];
    }
    if  (
    $file != $gallery_image)
    {
    $myFile "../upload/$gallery_image";
    unlink($myFile);
    }
    else
    {
    echo 
    "no images";
    }
    }
    closedir($dh);
    ?>
    I have never done this before so i havent got a clue. lol

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    PHP Code:
    <?php
    $dir 
    "upload/";
    $dh opendir($dir);

    while ((
    $file readdir($dh)) !== false) {

        
    $sql9 sprintf("SELECT * FROM gallery where gallery_image='%s'"mysql_real_escape_string($file));
        
    $result9 mysql_query($sql9$conn) or die(mysql_error());
        if ( 
    $result9 ) {
            
    // valid results
            
    $newArray mysql_fetch_array($result9)
            
    $myFile "../upload/" $newArray['gallery_image'];
            if ( !
    unlink($myFile) ) {
                echo 
    "failed to remove file : $myFile";
            }
        }
    }
    closedir($dh);
    ?>
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Regular Coder
    Join Date
    Jul 2005
    Posts
    153
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I get a server 500 error with that

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by simjay View Post
    I get a server 500 error with that
    syham code look ok from my point of view.

    - you can use opendir or you are in safe mode?
    - unlink don't work? check suid of the image files

    EDIT: sorry, shyam, correct me please, if I'm wrong:
    simjay say "not in database", so:
    PHP Code:
    <?php
    $dir 
    "upload/";
    $dh opendir($dir);

    while ((
    $file readdir($dh)) !== false) {

        
    $sql9 sprintf("SELECT * FROM gallery where gallery_image='%s'"mysql_real_escape_string($file));
        
    $result9 mysql_query($sql9$conn) or die(mysql_error());
        if ( 
    $result9 && mysql_num_rows($result9) == 0) {
            
    // valid results
    //        $newArray = mysql_fetch_array($result9)
    //        $myFile = "../upload/" . $newArray['gallery_image'];
            
    $myFile "../upload/" $file;
            if ( !
    unlink($myFile) ) {
                echo 
    "failed to remove file : $myFile";
            }
        }
    }
    closedir($dh);
    ?>
    best regards
    Last edited by oesxyl; 01-14-2008 at 02:47 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •