2 Ways To Find Current Directory in PHP Without Regular Expressions

There comes a time when you need to find the current directory in PHP, test to see if it is the directory that you expect it is, and take an action based on the test results. Obviously, the easiest way to get the current working directory in PHP is getcwd(). However, parsing the output of this function can provide some interesting challenges.

While it is trivial to do this sort of search with a Regular Expression, I tend to look for a solution that is easier to understand its functionality without reaching for the reference books. As a result, I would typically use one of the two methods below. One thing to note is that in each scenarion, any directory name in the path will match, not just the current directory. This can be easily updated to only match the single current directory the script is executed from.

  • Tokenize the path – This involves splitting the result of getcwd() based on the directory separator, and in *nix operating systems, that is the forward slash /. It works out to something like is shown below. One note about this method is that it only works when you have a complete match between the $desired_path and the actual directory name. You can easily change this to have the $desired_path do a partial match against the $token.
function find_folder_token() {	
    $path = getcwd();
	$tokens = explode('/', $path);
	$desired_path = 'some_folder';

	foreach ($tokens as $token) {
		if ($token == $desired_path) {
    		return true;
    	}
	}
	return false;
}
  • String Search – This is a much simpler method to finding whether you are in the desired directory or not, but it does provide its challenges. One such challenge is that by default, it allows you to find partial directory names, which may or may not be what you desire. The way it is displayed below only finds complete directory names. Please note the added forward slashes, /, in the $path variable as well as the $desired_path variable. These are required to make sure to match the full directory name.
function find_folder_search() {
	$path = getcwd() . '/';
    $desired_path = '/some_folder/';
    
    if (strpos($path, $desired_path) !== false) {
    	return true;
    }
    return false;
}

Related Posts

Jul 16, 2014
2 minutes

Write Software for People, Not Computers

Throughout a normal day, I end up reading a lot of information about current issues in technology, and today is no different. There was a debate raging about whether or not high-level math was required for programmers that was sparked by this article by Sarah Mei Programming is not math. While it is an interesting topic, and, surprisingly, I mostly agree with Sarah on this issue, that is not the most important portion of her post. The important part is instead a quote from Structure and Interpretation of Computer Programs from MIT Press, and is as follows:

Jul 15, 2014
2 minutes

The Easiest Way to Create A Solution That Works

The easiest way to create a solution that works…is to do it right the first time. Yes, this is a bit of a cop-out, but it turns out to be an important factor to keep in mind when you are tempted to come up with a quick and dirty solution for a problem that does not follow established best practices and is likely to have code quality issues later.

I have run across many sections of code that I or other developers have written in the past that we thought were “good enough” at the time it was written, yet, I was revisiting the code because we discovered a bug in it. Many times, this code had an issue that would have been trivial to fix at the time it was written, if it were only found. It seems that as a developer, we tend to find the least sufficient solution that will solve the immediate problem we are experiencing instead of finding an optimal solution that will be easily maintained months and years after it was written.

Mar 13, 2015
3 minutes

Never Explicitly Trust Software Because It Is Open-Source

One of the major ideas behind open source projects is that allowing anyone that wants to view the source code of a project to be able to do so should make bugs and security weaknesses easy to find. While this did not work so well with OpenSSL and its various bugs that have been exposed recently, I do have an example where it worked extremely well.

Magento is an eCommerce platform that has two separate editions. One is a completely open-source and free as in beer Community edition. The other is a somewhat expensive Enterprise Edition. There is a large community of Magento developers that create extentions, or addons, for these two editions of Magento.