QuePID Posted May 30, 2011 Share Posted May 30, 2011 Hi, I am wanting to check to see if a folder exists, if not create it. PHP on a windows machine so I would like to know how to handle the folder separators. I am wanting to use a path which includes the drive letter, such as... $foldername ="C:\folder1\images\$checkfolder" I was thinking about using something like this: if(!is_dir($foldername)) mkdir ("$foldername",777); What would be the best way of tackling this using windows paths? Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted May 30, 2011 Share Posted May 30, 2011 what is wrong with what you posted? What is happening when you try to run that code? Quote Link to comment Share on other sites More sharing options...
QuePID Posted June 2, 2011 Author Share Posted June 2, 2011 what is wrong with what you posted? What is happening when you try to run that code? Here is the code: <?php //Define variables for a test drive of folder detection/creation. $family="Family"; $genus="Genus"; $species="Species"; //Create variable to hold family name along with path and make the folder should it not exist. $familyname="C:\herbariumdb.eku.edu\images\$family"; if(!is_dir($familyname)) mkdir ('$familyname',777); echo "$familyname<br>"; //Create variable to hold genus name along with path and make the folder should it not exist. $genusname='C:\herbariumdb.eku.edu\images\$family\$genus'; if(!is_dir($genusname)) mkdir ("$genusname",777); echo "$genusname<br>"; //Create variable to hold genus name along with path and make the folder should it not exist. $speciesname='C:\herbariumdb.eku.edu\images\$family\$genus\$species'; if(!is_dir($species)) mkdir ("$species",777); echo "$speciesname<br>"; ?> Here is the output: C:\herbariumdb.eku.edu\images$family C:\herbariumdb.eku.edu\images\$family\$genus C:\herbariumdb.eku.edu\images\$family\$genus\$species The $vars should be their values not their names, but something is askew. Quote Link to comment Share on other sites More sharing options...
teynon Posted June 2, 2011 Share Posted June 2, 2011 For variables that are in strings, enclose them in {} Like so: "dir/{$variable}" Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 2, 2011 Share Posted June 2, 2011 $var = 1; echo "variable is $var"; // should output: variable is 1 echo 'variable is $var'; // should output: variable is $var * the difference is in the quotes. With double quotes php will check for variables, with single quotes it wont. although your first level ($family) should have worked. I'm not sure how this works on windows though. in your case, either change the quotes to double, or do something like this that should always work: 'C:\herbariumdb.eku.edu\images\'.$family.'\'.$genus.'\'$species; hope this helps. Quote Link to comment Share on other sites More sharing options...
teynon Posted June 2, 2011 Share Posted June 2, 2011 I didn't check the quotes for all of them. WebStyles is right about the ' and ". You should standardize your code and go with one or the other. FYI, \ is an escape character. That's why I like using { in quotes because it ensures your variable is a variable. Quote Link to comment Share on other sites More sharing options...
QuePID Posted June 2, 2011 Author Share Posted June 2, 2011 $var = 1; echo "variable is $var"; // should output: variable is 1 echo 'variable is $var'; // should output: variable is $var * the difference is in the quotes. With double quotes php will check for variables, with single quotes it wont. although your first level ($family) should have worked. I'm not sure how this works on windows though. in your case, either change the quotes to double, or do something like this that should always work: 'C:\herbariumdb.eku.edu\images\'.$family.'\'.$genus.'\'$species; hope this helps. What you suggested worked except for this line (#14): $speciesname='C:\herbariumdb.eku.edu\images\'.$family.'\'.$genus.'\'.$species; which gives error: PHP Parse error: syntax error, unexpected T_STRING in C:\herbariumdb.eku.edu\qpadmin\foldercreationtest.php on line 14 Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 2, 2011 Share Posted June 2, 2011 yeah, my bad. teynon is right, the backslash is an escape character, use the forward slash in everything and you should be fine. (I'm not a windows guy, but I think the only time you need backslashes in win is when you're accessing a network path with something like \\server\share) hope this helps and I'm not saying something really stupid. Quote Link to comment Share on other sites More sharing options...
QuePID Posted June 2, 2011 Author Share Posted June 2, 2011 yeah, my bad. teynon is right, the backslash is an escape character, use the forward slash in everything and you should be fine. (I'm not a windows guy, but I think the only time you need backslashes in win is when you're accessing a network path with something like \\server\share) hope this helps and I'm not saying something really stupid. Switching to / instead of \ as directory separators worked like a charm. Thanks guys! Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 2, 2011 Share Posted June 2, 2011 Food for thought: Although it might be a little more work, PHP has a built in constant for the "directory separator" characters (aptly named DIRECTORY_SEPARATOR). The main advantage, in my opinion, is that it will use the correct directory separator based upon the platform you are using which makes portability much easier. Although, since you say that this is a windows machine, that may not be important to you. But, it does have the added benefit of not having to worry about the backslash being interpreted as an escape character. Also, even though I use that constant frequently in my code, the full name is a little long. So, I typically define a variable using the constant and then use the variable in my code. Example: $DS = DIRECTORY_SEPARATOR; $fullpath = $rootpath . $ds . $foldername; Quote Link to comment Share on other sites More sharing options...
QuePID Posted June 2, 2011 Author Share Posted June 2, 2011 Food for thought: Although it might be a little more work, PHP has a built in constant for the "directory separator" characters (aptly named DIRECTORY_SEPARATOR). The main advantage, in my opinion, is that it will use the correct directory separator based upon the platform you are using which makes portability much easier. Although, since you say that this is a windows machine, that may not be important to you. But, it does have the added benefit of not having to worry about the backslash being interpreted as an escape character. Also, even though I use that constant frequently in my code, the full name is a little long. So, I typically define a variable using the constant and then use the variable in my code. Example: $DS = DIRECTORY_SEPARATOR; $fullpath = $rootpath . $ds . $foldername; This is very helpful as I desire to produce code which works equally well across platforms. 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.