lovesmith Posted January 21, 2008 Share Posted January 21, 2008 Well i want to delete all the files in a certain folder with the php code. Folder have write permission, and there might me so many files. can unlink function can be used? if yes...what we pass for the filename parameter to delete? Link to comment https://forums.phpfreaks.com/topic/87018-solved-how-to-delete-all-the-files-using-php/ Share on other sites More sharing options...
vbnullchar Posted January 21, 2008 Share Posted January 21, 2008 <?php $dir = "/home/user/"; if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if(filetype($dir . $file) == 'file') { exec("rm $file"); } } closedir($dh); } } ?> Link to comment https://forums.phpfreaks.com/topic/87018-solved-how-to-delete-all-the-files-using-php/#findComment-444969 Share on other sites More sharing options...
lovesmith Posted January 21, 2008 Author Share Posted January 21, 2008 hey thanks a lot, will try now! Link to comment https://forums.phpfreaks.com/topic/87018-solved-how-to-delete-all-the-files-using-php/#findComment-444973 Share on other sites More sharing options...
lovesmith Posted January 21, 2008 Author Share Posted January 21, 2008 <?php $dir = "/home/user/"; if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if(filetype($dir . $file) == 'file') { exec("rm $file"); } } closedir($dh); } } ?> Does this code deletes all the file, as we dont know the file name that users upload to my server. ?? Link to comment https://forums.phpfreaks.com/topic/87018-solved-how-to-delete-all-the-files-using-php/#findComment-444986 Share on other sites More sharing options...
vbnullchar Posted January 21, 2008 Share Posted January 21, 2008 it deletes all the files inside the specified directory.. change : <?php exec("rm $file"); ?> to <?php exec("rm $dir$file"); ?> or in windows <?php exec("del $dir$file"); ?> Link to comment https://forums.phpfreaks.com/topic/87018-solved-how-to-delete-all-the-files-using-php/#findComment-445006 Share on other sites More sharing options...
lovesmith Posted January 21, 2008 Author Share Posted January 21, 2008 Below is my code and my server is linux. The code didnt worked. And the folder got read, write,execute permission as well. Can you say whats going on? $dir=$_SERVER['DOCUMENT_ROOT']."/users/".$userid."/"; if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if(filetype($dir . $file) == 'file') { //$path=$dir.$file; exec("rm $dir$file"); $deletefiles=delete_data(TABLE_UPLOAD, " user_id='$userid'",""); $deletesubs=delete_data(TABLE_USERSPACE, " user_id='$userid' AND space_id='$space'",""); header("Location: storage_list.php"); } } closedir($dh); } } Link to comment https://forums.phpfreaks.com/topic/87018-solved-how-to-delete-all-the-files-using-php/#findComment-445007 Share on other sites More sharing options...
lovesmith Posted January 21, 2008 Author Share Posted January 21, 2008 in above code $userid is the variables that holds the subfolder in which all the files reside that i want to delete Link to comment https://forums.phpfreaks.com/topic/87018-solved-how-to-delete-all-the-files-using-php/#findComment-445016 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.