Jump to content

Recommended Posts

I am trying to delete any picture in a folder that is not in the database via the unset statement

The idear is to plonk this in the folder with the members pictures to delete any picture not in the database.
Please help.




[code]
<?

//connect to the database

$db=mysql_connect("localhost");
mysql_select_db("members_pictures",$db);

// select the field name

$query="SELECT * FROM picture ";
$result=mysql_query($query);

// if statement where picture = 1 to the users id

if(mysql_num_rows($result)==1){

}else{

// delete any other picture within that folder thats there.

unset($picture);
}

?>

[/code]
unset deletes variables, are you sure you aren't looking for unlink?

If so, you could readdir() in a loop, and check the database for the filename.

[code]        $dir = realpath("."); //current folder.
        if($dh=opendir($dir)) {
            while(($file=readdir($dh))!=false) {
                $result=mysql_query("SELECT `picture_file_name` FROM `picture` WHERE `picture_file_name` = '$file'");
                if(mysql_num_rows($result)<1) unlink($file); // no result DELETE FILE
            }
            closedir($dh);
        } else echo "directory error"; [/code]

note, this is an example script and might not work just right for you. Oh, and this WILL delete files, so be careful. Test it someplace before you load it on your production site.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.