krash11554 Posted September 22, 2012 Share Posted September 22, 2012 I have had this same problem when i started to make to directorys with mdir function. now i am having problems when i use the unlink function. heres the code. unlink('uploads/cars/' . $user_id . '/car' . $car_number . '/'); unlink('uploads/cars/' . $user_id . '/thumbs/car' . $car_number . '/'); I am getting back Warning: rmdir(uploads/cars/15/car1/) [function.rmdir]: Directory not empty inC:\inetpub\wwwroot\garage\phpfunctions\functions.php on line 315 Warning: unlink(uploads/cars/15/thumbs/car1/) [function.unlink]: Permission denied inC:\inetpub\wwwroot\garage\phpfunctions\functions.php on line 317 I have already set all permissions to the root directory idk what else to do. By the way im on IIS. Thanks Quote Link to comment Share on other sites More sharing options...
coded4u Posted September 22, 2012 Share Posted September 22, 2012 <?php // Removing a directory & all its content // glob('DIR/PATH/{,.}*', GLOB_BRACE); -- GLOB_BRACE will remove any hidden files in the directory. $files = glob('DIR_NAME/{,.}*', GLOB_BRACE); foreach($files as $file){ // Loop through each file in the DIR if(is_file($file)) unlink($file); // Removing the file from the DIR } //If you don't want to remove the folder then remove the line below this comment. rmdir('DIR_NAME'); // Remove the DIR ?> I hope this is helpful. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 22, 2012 Share Posted September 22, 2012 Actually, in this case you'll want something that recursively deletes files and folders, in case the folders contain other folders. That's why I recommend using the following function: <?php /** * Will remove a file, or a folder and all of its contents. * * @param string $target Target folder/file to delete. * @return bool */ function unlink_recursive ($target) { // If target is a file, just unlink it straight away. if (is_file ($target)) { return unlink ($target); } // Target is a folder, so get all of its elements. $contents = scandir ($target); // Strip the meta-folder records. array_shift ($contents); array_shift ($contents); // Loop through the individual records. foreach ($contents as $file) { // Build the correct path to the file record. $file = $target.'/'.$file; // If record is a folder, recursively delete it and its contents. if (is_dir ($file)) { // In case of error, stop executing deletions. if (!unlink_recursive ($file)) { return false; } // Skip to the next record. continue; } // Record is a file, delete it. if (!unlink ($file)) { // In case of error, stop executing deletions. return false; } } // Finally, remove the folder itself. if (!rmdir ($target)) { // In case of error, stop executing deletions. return false; } // Return successfully. return true; } Quote Link to comment Share on other sites More sharing options...
coded4u Posted September 22, 2012 Share Posted September 22, 2012 I wasn't too sure what the folder would be containing as the description didn't give much details, i think i'll take a copy of that myself though as i might need it. Quote Link to comment Share on other sites More sharing options...
krash11554 Posted September 22, 2012 Author Share Posted September 22, 2012 So all iahve to do is put the directory in the function? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 22, 2012 Share Posted September 22, 2012 Call the function with the directory path as the parameter, just like you'd do with any other function. PHP, like most other languages, does not differ between a built-in or a user made function. Quote Link to comment Share on other sites More sharing options...
krash11554 Posted September 22, 2012 Author Share Posted September 22, 2012 Ok thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.