From the course: PHP Techniques: Working with Files and Directories

Relative paths and magic constants - PHP Tutorial

From the course: PHP Techniques: Working with Files and Directories

Start my 1-month free trial

Relative paths and magic constants

- [Instructor] In the previous movie, we worked with absolute file paths. Now let's take a look at relative paths and something called magic constants. PHP defines a few values for us, and we call them magic constants, because they're automatically set. We're going to look at two of them. The first is __FILE__. It may be hard to see, but before and after the word FILE in all caps are two underscores. It is _, _, FILE, _, _. The underscores are a tip that it's one of the magic constants. There are several others in PHP, but __FILE__ and __DIR__ are the most useful ones. PHP sets the file magic constant to the absolute path of the current PHP script that's being run. PHP sets the __DIR__ magic constant to the absolute path of the directory of the current PHP script being run. You can also determine the same value for yourself by using the dirname function on the __FILE__ magic constant. Dirname returns the parent directory of any file or directory. All of these will return strings which contain absolute paths. Then we can use relative paths to locate other files based on that location. There are a couple of Unix conventions that are useful for working with relative paths. The first is a single period which represents the current directory. Even more useful, a double period represents the parent directory. This is a Unix console and you can see what a typical file list inside a directory looks like. You see the single period here, that's a reference to the current directory, and the .. is a reference to the parent directory. And then I have the files which are inside that directory. Remember, a path is just a string. If we append a string onto the end of a directory path, then we'll have a new string that's a path to a file in that directory. Or, we could use these Unix conventions and define a relative path. This string says to go to the parent directory to find file.txt. You can also use the PHP dirname function to find out the name of that directory, but this can be faster to write. Let's try this in PHP. Let's make a new file in our project, and we're going to save it as file_relative.php. And let's put our PHP tags in there. And then inside there, let's put echo, and let's use those magic constants, __FILE__, and then I'll append just the string with a br tag, just so we get a nice line return in our HTML. And then I'll copy that and paste it, and I'll just change that to use __DIR__ for the directory. Let's take a look at these in Firefox. We'll open up Firefox, and I'll change this from file_basics to file_relative. You can see it returned an absolute path to the current running file and to its directory. We already know how to use file_exists. Let's try it here. Let's add another line here, and we'll use echo file_exists, and let's put in that __FILE__, and question mark, we're going to use the ternary operator again with exists and colon none and then a semi-colon at the end, and we'll echo back a br tag just to make it look nice. Let's also do another one where we set the file path to be equal to the directory string, and then we'll append onto it /file_basics.php, and then we can do the same code just to see if it exists. But of course we're going to use $filepath instead of the file. So now it's a relative path from the current directory to file_basics.php. Notice that I put a forward slash in front of the file name. If you look back at the string over here for the directory, you'll see that it does not end in a slash separator. So I need to be sure to add it. All right, so let's save that file. Let's go to Firefox and let's try it out. You can see that it tells me that both of these files exist. Often, when I'm working in PHP, I define a PHP constant of my own, which is a path to the root of the project. And then I can use relative paths from that anchor. You can define that constant by writing out an absolute path or determine it using the __FILE__ and __DIR__ magic constants that we just learned. Relative paths and magic constants are going to be useful for making your PHP code portable, whether it's to other directories or to other file systems.

Contents