Jump to content

Directory function vs Alternative


dalawh

Recommended Posts

What is the difference between using the directory functions to change the directory and opening file as opposed to just opening the file straight? I am not sure if you can open a file in a different directory by using the directory function or not, so correct me if it is not possible.

chdir("info/");$fileName="info.txt";$file=fopen($fileName,"r+");

VS

$directory="info/";$fileName="info.txt";$file=fopen($directory.$fileName,"r+");

If we use the first, how do we change back to the previous directory? If both is possible, which is the better method? Outside of opening files, which is better? Using strings or directory functions?

Link to comment
Share on other sites

You can get getcwd before you change the directory to get the current directory that you can change back to. But I would recommend doing something like that just for opening files. Changing PHP's working directory has several implications (like files being included) that you may not want to occur just for opening some files. It's typical to specify an absolute or relative path when opening a file instead of changing the working directory.

Link to comment
Share on other sites

You can get getcwd before you change the directory to get the current directory that you can change back to. But I would recommend doing something like that just for opening files. Changing PHP's working directory has several implications (like files being included) that you may not want to occur just for opening some files. It's typical to specify an absolute or relative path when opening a file instead of changing the working directory.
Okay, I will stick to the relative path and not chnage the directory. What would be some good times to change directories? Since getcwd gets you the current directory, when you change to a new directory and you want to change back to the original directory, does using chdir() to the original directory work? What I mean is that does chdir() only search the current directory for directories or does it do a global search?
Link to comment
Share on other sites

Frankly, I can't think of a good reason to change the working directory unless the script is running in command-line mode, when PHP starts with a different working directory than it does when you access it over the web.

when you change to a new directory and you want to change back to the original directory, does using chdir() to the original directory work? What I mean is that does chdir() only search the current directory for directories or does it do a global search?
If you give it a single directory name without a path then it looks in the current directory, but getcwd returns an absolute path.
Link to comment
Share on other sites

Frankly, I can't think of a good reason to change the working directory unless the script is running in command-line mode, when PHP starts with a different working directory than it does when you access it over the web. If you give it a single directory name without a path then it looks in the current directory, but getcwd returns an absolute path.
Let's say I am in the root directory and in the root, there are folders called images, temp, and random. If I change directory to temp and in the temp folder, there are folders called files, images, and cookie. If I use chdir("random"), it will not work because it only looks in the current directory? If I use a path meaning chdir("random/randoms/stuff.txt"), it will work? Absolute path is the default path?
Link to comment
Share on other sites

Guest So Called

I too can't think of any reason why you would want to change PHP's current directory context. At bare minimum you'd have to save the context if you changed it, in case you wanted you wanted to go back. I can think of plenty reasons you wouldn't want to change the context, just beginning with the above, that you've just created more things for your code to keep track of. More complicated code = more probable bugs = more debugging time = more maintenance labor if your code needs to be supported. I'd like to see one good reason why you would want to change PHP's current directory context, other than you can do it. That's why a lot of "code stuff" gets implemented -- just because "we" can do it.

Link to comment
Share on other sites

An absolute path is a path that starts at the filesystem root. On a Linux server that would be something like /home/user/public_html/temp, etc. On a Windows server it would start with a drive letter like C: or D:, e.g. C:\Inetpub\wwwroot\temp.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...