Page 1 of 1

rmdir not working on empty folder

Posted: Thu Mar 16, 2017 5:08 pm
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

Re: rmdir not working on empty folder

Posted: Fri Mar 17, 2017 7:05 am
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 '/'?

Re: rmdir not working on empty folder

Posted: Fri Mar 17, 2017 7:16 am
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.

Re: rmdir not working on empty folder

Posted: Fri Mar 17, 2017 7:33 am
by diseman
.... and no errors.

Re: rmdir not working on empty folder

Posted: Fri Mar 17, 2017 7:40 am
by Celauran
Checked for hidden files, too? Checked that the directory can be deleted (is_writeable)?

Re: rmdir not working on empty folder

Posted: Fri Mar 17, 2017 7:44 am
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?

Re: rmdir not working on empty folder

Posted: Fri Mar 17, 2017 7:50 am
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.

Re: rmdir not working on empty folder

Posted: Fri Mar 17, 2017 7:59 am
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.

Re: rmdir not working on empty folder

Posted: Fri Mar 17, 2017 8:05 am
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.