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
https://forums.phpfreaks.com/topic/81738-deep-file-delete/#findComment-415149
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
https://forums.phpfreaks.com/topic/81738-deep-file-delete/#findComment-415162
Share on other sites

Archived

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

×
×
  • 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.