Nandini Posted August 26, 2008 Share Posted August 26, 2008 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 More sharing options...
redarrow Posted August 26, 2008 Share Posted August 26, 2008 use unlink function and a while loop from ur database........... Link to comment https://forums.phpfreaks.com/topic/121355-unused-files-in-directory/#findComment-625687 Share on other sites More sharing options...
Nandini Posted August 26, 2008 Author Share Posted August 26, 2008 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. Link to comment https://forums.phpfreaks.com/topic/121355-unused-files-in-directory/#findComment-625696 Share on other sites More sharing options...
redarrow Posted August 26, 2008 Share Posted August 26, 2008 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); } } } ?> Link to comment https://forums.phpfreaks.com/topic/121355-unused-files-in-directory/#findComment-625698 Share on other sites More sharing options...
Nandini Posted August 26, 2008 Author Share Posted August 26, 2008 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 Link to comment https://forums.phpfreaks.com/topic/121355-unused-files-in-directory/#findComment-625712 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.