Jump to content

Archived

This topic is now archived and is closed to further replies.

redarrow

unset picture in folder via database to delete pic.

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.