MyBB Community Forums

Full Version: How to check if a directory is NOT writable?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello. I need a little help from you (again).

I want to make sure a directory (NOT a file) is NOT writable.

Here's my attempt:
// Attempt to determine if a directory is NOT writable
$dir = MYBB_ROOT."my_dir/"; // directory path (in MYBB_ROOT)
@fopen($dir.'test.txt', 'w'); // create and open 'test.txt' file in writable mode
if(@file_exists($dir.'test.txt', 'w')) // if 'test.txt' file has been created
{
	@fclose($dir.'test.txt'); // close file
	@unlink($dir.'test.txt'); // delete file
	@my_chmod($dir, '0755'); // attempt to change the chmod for '$dir' to "0755"
	// a second test
	@fopen($dir.'test.txt', 'w'); // create and open 'test.txt' file in writable mode
	if(@file_exists($dir.'test.txt')) // if 'test.txt' file has been created
	{
		@fclose($dir.'test.txt'); // close file
		@unlink($dir.'test.txt'); // delete file
		exit("The directory is writable."); // stop the script and output a message
	}
}
// the directory is NOT writable
echo "The directory is NOT writable.";
exit();

Is there any other better way to do this? Undecided
see if there is a simple method available here => response by Matt
Thank you .m. ! Indeed, a useful link.
So... with a single line of code I'm able to return the chmod of a directory?
return substr(sprintf('%o', fileperms('./mydir')), -4);
Tested... and it works!
But I'm a little unsure: this code will check basically if the directory contain any writable file or] will return the chmod of the directory itself?

// L.E.: Sorry, my question is (probably) meaningless.
I just want to know if this line will return the chmod of the directory itself?
^ it will return CHMOD of the directory. doesn't ensure that existing files are writable
I'm asking because fileperms() returns the file's permissions as a numeric mode. That's why I'm a little confused. However, I tested with a directory without any file inside and it return the correct value of the directory itself, so... I suppose that it works. Anyway, I will look for more information in the PHP manual.

Thank you again and I appreciate your help!

I hope I'm not asking too much... I'm just a novice that wants to learn more.

Back to the above problem: in this case, why developers does not use in the installation routine of MyBB, something similar with:
	// Check upload directory is writable
	if(substr(sprintf('%o', fileperms(MYBB_ROOT.'uploads')), -4) !== '0777')
	{
		$errors[] = $lang->sprintf($lang->req_step_error_box, $lang->req_step_error_uploaddir);
		$uploadsstatus = $lang->sprintf($lang->req_step_span_fail, $lang->not_writable);
		$showerror = 1;
	}
	else
	{
		$uploadsstatus = $lang->sprintf($lang->req_step_span_pass, $lang->writable);
	}
instead of (line 1144):
	// Check upload directory is writable
	$uploadswritable = @fopen(MYBB_ROOT.'uploads/test.write', 'w');
	if(!$uploadswritable)
	{
		$errors[] = $lang->sprintf($lang->req_step_error_box, $lang->req_step_error_uploaddir);
		$uploadsstatus = $lang->sprintf($lang->req_step_span_fail, $lang->not_writable);
		$showerror = 1;
		@fclose($uploadswritable);
	}
	else
	{
		$uploadsstatus = $lang->sprintf($lang->req_step_span_pass, $lang->writable);
		@fclose($uploadswritable);
	  	@my_chmod(MYBB_ROOT.'uploads', '0777');
	  	@my_chmod(MYBB_ROOT.'uploads/test.write', '0777');
		@unlink(MYBB_ROOT.'uploads/test.write');
	}
? There must be a good reason for all those writing tests (even if it's about directories like 'cache' or 'uploads')... it makes me to believe that this code:
return substr(sprintf('%o', fileperms('./dirname')), -4);
is not the best way to determine if a directory is writable or not (although it seems that it works, at least in my tests).
I'm still confused...

OK, sorry for off-topic but... I was reading something... that made me to smile.
(2014-09-20, 03:05 PM)TheGarfield Wrote: [ -> ]...Okay here's the deal, I think the reason why MyBB's moderators have created the "Plugin Development" forum is for people to post threads about plugins they're developing, and gonna release for free, but need like collecting ideas, beta testers, collaborators and stuff like that...

I hate to say it, but I think that... He's right!. Therefore, until my plugin will not be ready at least in a BETA version, I will not ask for more help in this section. I really don't want to become the next person who will be criticized by TheGardfield (a nice guy otherwise). Also, I do not expect an answer anymore at my second question. I will figure out myself what it's the best way to determine if a directory is writable or not.

Thank you again to .m. for his kindness and to all for your time. A good day.
Easiest way:

if (!is_writable($dir)) {
    // Directory is not writable
}

http://php.net/manual/en/function.is-writable.php

It works with both files and folders Big Grin
I've been always convinced that is_writable() function works only for files (due to "is_writable — Tells whether the filename is writable"). But the filename argument may be also a directory name (without to specify a real file). It's so simple! I'm so embarrassed...

Thank you again, Euan! You're... amazing!
No problem Smile The short description can be a little confusing sometimes on php.net.