note 42996 added to function.mkdir

From: Date: Sun, 06 Jun 2004 10:20:43 +0000
Subject: note 42996 added to function.mkdir
Groups: php.notes 
Request: Send a blank email to php-notes+get-70788@lists.php.net to get a copy of this message
I have a slightly different version that provides: 1) Simplified code. if "file_exists" then we have nothing else to do other than returning (non-0 in case of directory, 0 otherwise). 2) Multilanguage support, such as /tmp/öffnung. Some words start by non-[a-z] character in languages other than english. 3) "mkdir" error detection. If "mkdir" returns FALSE that could be because there are several subdirectories that don't exist and / or a system error such as "permission denied". 4) Relative path creation. I don't prepend a slash character when it's not present. The function I present here includes some debugging code. Under a production environment: 1) The debugging code may be removed 2) The function could be made boolean by replacing ERR_OK by TRUE and the remaining ERR_* by FALSE I personally prefer to keep the debugging code. <?php define ("DEBUG_THIS",TRUE); if (DEBUG_THIS) { define ("ERR_OK",0); // OK define ("ERR_NOTDIR",1); // Path exists but not as directory define ("ERR_SYNTAX",2); // Syntax error in path define ("ERR_EMKDIR_1",3); // "mkdir" error with no parent define ("ERR_EMKDIR_2",4); // "mkdir" error and parent exists define ("ERR_EMKDIR_3",5); // "mkdir" error after creating parent } else { define ("ERR_OK",0); // OK define ("ERR_NOTDIR",1); // Path exists but not as directory define ("ERR_SYNTAX",1); // Syntax error in path define ("ERR_EMKDIR_1",1); // "mkdir" error with no parent define ("ERR_EMKDIR_2",1); // "mkdir" error and parent exists define ("ERR_EMKDIR_3",1); // "mkdir" error after creating parent } // // VVDirMake -- Creates a directory and all required subdirs // function VVDirMake($strPath) { // If path exists nothing else can be done if (file_exists($strPath)) return is_dir($strPath) ? ERR_OK : ERR_NOTDIR; // Backwards references are not allowed if (ereg("\.\.",$strPath) != 0) return ERR_SYNTAX; // If it can create the directory that's all. If not then either path // contains several dirs or error such as "permission denied" happened if (@mkdir($strPath)) return ERR_OK; // Gets the parent path. If none then there was a severe error $nPos = strrpos($strPath,"/"); if (!($nPos > 0)) return ERR_EMKDIR_1; $strParent = substr($strPath,0,$nPos); // If parent exists then there was a severe error if (file_exists($strParent)) return ERR_EMKDIR_2; // If it can make the parent $nRet = VVDirMake($strParent); if ($nRet == ERR_OK) return mkdir($strPath) ? ERR_OK : ERR_EMKDIR_3; return $nRet; } // // Demo / Test code // foreach (array( "../Syntax/Error", "/tmp/File", "/ppp/one", "locked/one/two", "one/two", "/tmp/A/B/C/D" ) as $strPath) echo "VVDirMake($strPath): ",VVDirMake($strPath),"\n"; die("That's All !!"); ?> In order to run the demo code (Unix / Linux), you must: 1) Create a regular file, to test ERR_NOTDIR: rm -r /tmp/File; touch /tmp/File 2) Create a "locked" directory under your working directory, to test ERR_MKDIR_* ("permission denied"): chmod 777 locked; # if it exists rm -r locked; mkdir locked; chmod 0 locked 3) Remove directories to be created: rm -r one rm -r /tmp/A Please run it once and check that some directories are created. Then run it once more and check that the output is the same because the function doesn't complain about existing paths. This is the output of a run: VVDirMake(../Syntax/Error): 2 VVDirMake(/tmp/File): 1 VVDirMake(/ppp/one): 3 VVDirMake(locked/one/two): 4 VVDirMake(one/two): 0 VVDirMake(/tmp/A/B/C/D): 0 That's All !! Regards, Alberto ---- Manual Page -- http://www.php.net/manual/en/function.mkdir.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+42996 Delete -- http://master.php.net/manage/user-notes.php?action=delete+42996&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+42996&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#70788) next »