Jump to content

unused files in directory


Nandini

Recommended Posts

Hi

I have a directory named as "gui". In that so many files inserted for each transaction. And i have a database table like "users". In that username and filename where in directory, is same. Now i want to compare db table username and file name. If any file not matching with db username field that file will be deleted. This is the reuirement.

 

Can anyone help me out pls?

Link to comment
https://forums.phpfreaks.com/topic/121355-unused-files-in-directory/
Share on other sites

thanx redarrow for ur quick replay. I used that script but its not working.

 

here is my script.

 

<?

$path="/root/myfiles/gui/";

$handle=opendir($path);

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

$dir_file[]=$file;

}

closedir($handle);

 

$sql=mysql_query("select * from users");

while($row=mysql_fetch_array($sql))

{

$user=$row['username'];

$file_name=$user.".conf";

foreach($dir_file as $v) {

if($v<>$file_name)

{

unlink($v);

}

}

}

?>

 

nothing happend by using this script. please help me out.

not tested example only................

 


<?php

$db_host = ''; 
$db_user = ''; 
$db_pwd = '';
$database = '';
$table = '';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

$sql="SELECT * FROM USERS WHERE USERNAME='$USERNAME' AND PASSWORD='$PASSWORD'";
$x=mysql_query($sql)or die(mysql_error());	

while($file_name=mysql_fetch_assoc($x)){

if ($handle = opendir('/path/to/files')) {

while (false !== ($file = readdir($handle))) {
        
    	if(!$file_name==$file){
    	
    		unlink($file);
    	}
  closedir($handle);
}
  }
   }
?>

Hi i used your code. But the following errors occured. the path is also correct.

 

Warning: readdir(): 13 is not a valid Directory resource in /var/www/admin/trunks/un_files.php on line 53

 

Warning: readdir(): 14 is not a valid Directory resource in /var/www/admin/trunks/un_files.php on line 53

 

Warning: readdir(): 15 is not a valid Directory resource in /var/www/admin/trunks/un_files.php on line 53

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.