rmdir not working on empty folder

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

rmdir not working on empty folder

Post by diseman »

Hi Celauran,

Spent a few hours trying to research this to no avail. Thought I would run it by you to see if you catch the problem.

Trying to delete an empty folder that is:

1. Known to be completely empty
2. Permission'd with 0777

Code: Select all

if(isset($_GET['delete_photo'])) {

		$document_folder = $_SESSION['student_first_name'] . "-" . $_SESSION['student_last_name'];
		$target_dir = "../_photos/".$document_folder."/";

			$folder_check = $target_dir;

			if (file_exists($folder_check)) {

				array_map('unlink', glob("$target_dir/*.*")); //THIS WORKS FINE

					$handle = opendir($target_dir); // DIDN'T MAKE ANY DIFFERENCE
					closedir($handle); // DIDN'T MAKE ANY DIFFERENCE

						rmdir("../_photos/Tom-Jones"); // NO-GO ON LOCALHOST OR SERVER
			}

	}
The file in the folder does delete properly on both localhost and server, but folder doesn't delete on localhost or server.

I tried manually creating a folder on the server and gave it 0777 permissions. After trying to delete it with the script above, I noticed the permisssions were changed to 0777.

I hard-coded the folder "Tom-Jones" just as a test. I also used getcwd() to ensure I was calling script from proper location.

Localhost is using UniServer (localhost) on Windows 10, but I don't think it's a path issue anymore.

Thanks for any help you can provide.

Michael
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: rmdir not working on empty folder

Post by Celauran »

Are you seeing any errors? mkdir/rmdir works as expected for me. Might be different on account of being on Windows? Have you tried using DIRECTORY_SEPARATOR instead of '/'?
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: rmdir not working on empty folder

Post by diseman »

I thought it might be a windows thing too, which is why I then uploaded to the server. But, it didn't work there either.
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: rmdir not working on empty folder

Post by diseman »

.... and no errors.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: rmdir not working on empty folder

Post by Celauran »

Checked for hidden files, too? Checked that the directory can be deleted (is_writeable)?
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: rmdir not working on empty folder

Post by diseman »

I did check for hidden files. cPanel tells me no files ("directory empty"). I checked to make sure the setting to allow me to see hidden files was turned on and it was.

Folder showed permissions of 0777, so that's all permissions, right?
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: rmdir not working on empty folder

Post by diseman »

are you saying rmdir, in general, works on your system ... or ... you used my code to test it and my code worked on your system?

By the way, I also checked my phpini setting to ensure rmdir wasn't being prevented as a precautionary measure.
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: rmdir not working on empty folder

Post by diseman »

Ha! Disregard...

Not sure why, but now it seems to be working local server.

I've been making a few additions, which required some minor changes.

Seems it must have been a programming error that is now correct.

Thank you for your help.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: rmdir not working on empty folder

Post by Celauran »

Tried your exact code locally just now and it's working fine. Is _photos/ writeable?

EDIT: Ha! Too late! Glad you got it working.
Post Reply