If I have multiple folders with files in each, ie: folder_1 banana.txt apple.txt folder_2 peach.txt strawberry.txt folder_3 plum.txt coconut.txt Now lets say I am adding banana.txt to folder_3. I don't want it in folder 1 anymore. So what I want to do is create a file called banana.txt in folder_3 and then have some kind of loop that will go through all the folders and delete banana.txt from all folders (except, of course, folder_3) Here's what I have so far: $filename = "banana.txt"; if (file_exists($filename)) { unlink($myFile); fclose($fh); } else { // } Code (markup): That's what I want to do, but I realise I have to put that code inside some sort of loop which opens all the folders - but I I don't know how.
This is the code I am going to use if all else fails. I will have to make 23 instances of it. Here's an example of 2 of them: //==see if file is in another category and delete it $file_1 = $_SERVER['DOCUMENT_ROOT'] . "/categories/1/".$youtube_username.".txt"; if ((file_exists($file_1)) && ($file_1 != $file_new) ) { unlink($myFile); } $file_2 = $_SERVER['DOCUMENT_ROOT'] . "/categories/2/".$youtube_username.".txt"; if ((file_exists($file_1)) && ($file_1 != $file_new) ) { unlink($myFile); } //==end see if file is in another category and delete it Code (markup):
There are dozen ways to do this in PHP. I suggest that you look at the glob function or the dir class (you can find the references on php manual website).
Quickie: If folders are always have numeric pattern in $total_folders = 3; $file_to_delete = 'banana.txt'; for ($i=1; $i<=$total_folders; $i++) unlink( "$_SERVER[DOCUMENT_ROOT]/categories/$i/$file_to_delete" ); Code (markup): I hope it helps.
// List of all the folders $folder_list = array('folder_1', 'new_folder_2',' folder_2'); // List of files (key - filename, value - foldername) $file_add = array('banana.txt' => 'folder_1', 'apples.txt' => 'folder_2'); // Loop through all files foreach($file_add as $file_name => $folder_name) { // Loop through all folders foreach($folder_list as $this_folder) { // If file exists in this folder and this folder is not the copy to folder if(is_file($this_folder . '/' . $file_name) && $this_folder != $folder_name) { // If file hasn't been copied over to the copy to folder, move it if(!is_file($folder_name . '/' . $file_name)) { rename($this_folder . '/' . $file_name, $folder_name . '/'. $file_name); } else { // Otherwise remove it unlink($this_folder . '/' . $file_name); } } } } PHP: Hope it helps..
In my previous answer I suggested using glob or dir. Here is an example of how you would use the glob function: <?php // -- wild card ----------------------------------------------------------------------------------v $search = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "categories" . DIRECTORY_SEPARATOR . "*" . DIRECTORY_SEPARATOR . $youtube_username . ".txt"; $ignore = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "categories" . DIRECTORY_SEPARATOR . "1" . DIRECTORY_SEPARATOR . $youtube_username . ".txt"; // -- the folder to ignore -----------------------------------------------------------------------^ $files = glob($search); foreach($files as $file) { // we perform realpath() on paths so that the character-case and slashes of both paths become consistent if (realpath($file) != realpath($ignore)) { // unlink($file); echo "unlink($file)<br>"; } } ?> PHP:
use an if statment to check if the file exists in the folder if so delete it if not check the next with an else if statement following it