Jump to content

Folder/Subfolder Creation


QuePID

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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

 

Link to comment
Share on other sites

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.  :wtf:

Link to comment
Share on other sites

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.  :wtf:

 

Switching to / instead of \ as directory separators worked like a charm. 

Thanks guys!

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.