Jump to content

Deep file delete


The Little Guy

Recommended Posts

This is taken off of the documentation page for unlink (http://php.net/unlink).

 

james at NOSPAM dot telserco dot com

29-Sep-2007 10:10

Cheap and dirty example for cleaning a directory of files so many seconds old.

 


        function cleantmp() {
                $seconds_old = 3600;
                $directory = "/var/tmp";

                if( !$dirhandle = @opendir($directory) )
                        return;

                while( false !== ($filename = readdir($dirhandle)) ) {
                        if( $filename != "." && $filename != ".." ) {
                                $filename = $directory. "/". $filename;

                                if( @filemtime($filename) < (time()-$seconds_old) )
                                        @unlink($filename);
                        }
                }
        } 

 

Some modding, and that should fit your needs.

Link to comment
Share on other sites

Here is what I came up with:

 

Good or not?

<?php
if(isset($_GET['dir'])){
$dirs = $_GET['dir']."%";
$query = sprintf("SELECT * FROM albums WHERE owner = '%s' AND album LIKE '%s' ORDER by album DESC",
mysql_real_escape_string($_SESSION['id']),
mysql_real_escape_string($dirs));
$sql = mysql_query($query);
while($row = mysql_fetch_array($sql)){
	$sq = mysql_query("SELECT * FROM files WHERE directory = '{$row['album']}' AND owner_id = '{$_SESSION['id']}'");
	while($ro = mysql_fetch_array($sq)){
		if(file_exists('../../tzfiles.com/users/'.$row['album'].'/thumbs/videos/'.$ro['file_name'])){
			unlink('../../tzfiles.com/users/'.$row['album'].'/thumbs/videos/'.$ro['file_name']);
		}
		if(file_exists('../../tzfiles.com/users/'.$row['album'].'/thumbs/'.$ro['file_name'])){
			unlink('../../tzfiles.com/users/'.$row['album'].'/thumbs/'.$ro['file_name']);
		}
		unlink('../../tzfiles.com/users/'.$row['album'].'/'.$ro['file_name']);
	}
	if(file_exists('../../tzfiles.com/users/'.$row['album'].'/thumbs/videos')){
		rmdir('../../tzfiles.com/users/'.$row['album'].'/thumbs/videos');
	}
	if(file_exists('../../tzfiles.com/users/'.$row['album'].'/thumbs')){
		rmdir('../../tzfiles.com/users/'.$row['album'].'/thumbs');
	}
	rmdir('../../tzfiles.com/users/'.$row['album']);
	mysql_query("DELETE FROM files WHERE directory = '{$row['album']}' AND owner_id = '{$_SESSION['id']}'");
	mysql_query("DELETE FROM albums WHERE album = '{$row['album']}' AND owner = '{$_SESSION['id']}'")or die(mysql_error());
}

}
?>

Link to comment
Share on other sites

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.