How to Access the Parent Directory of a Directory Using PHP 


               


How to Access the Parent Directory of a Directory Using PHP



PHP


In this article, we show how to access the parent directory of a current directory you are in using PHP.

So let's say you are in the Articles directory of your website located in the website's root directory, as this website is.

So suppose you want to write code so that you can access the root directory of the website.

In order to go up one directory from the current directory, which is to reach the parent directory of the current directory, you can use the following PHP code shown below.



Using "..", we go a directory up from the current directory. Since the Articles directory is located on the root directory, this brings us to the root directory.

Using ".." allows you to go the parent of the current directory.

The code outputs all the files in the parent directory of the current directory.

Going Multiple Directories Up From the Current Directory

So the following code above goes up one directory from the current directory.

You can go how many directories up that you want.

Suppose you are in the Articles/Uploads directory of your website and you want to get to the root directory.

You can go up 2 directories by using the following PHP code shown below.



We simply use ".." twice to go up 2 directories from the current directory.

How to Go to Another Directory in the Same Parent Directory

Now we will show how to go to a directory on the same level as the current directory.

Let's say we are in the Articles directory, which is on the root directory. And we want to go to another directory on the root directory, let's say, images.

We first have to go up one directory, getting out of the current directory to the root directory. We then have the specify the directory on the root directory we want to access.

So the code accesses another directory on within the same root directory. Specifically, it goes from the Articles directory on the root directory to the images directory located on the same root directory.



So this code goes to another directory on the root directory, from the Articles directory to the images directory.

And this is all you really need to know about accessing parent directories using PHP.


Related Resources

How to Create a New Directory Using PHP

How to Upload Files to Your Server Using Your Own Website

How to Restrict the Size of a File Upload with PHP

How to Create Your Own File Transfer Protocol (FTP) For Your Website Using PHP

How to Create a Register and Login Page Using PHP

How to List All Files in a Directory using PHP

How to List All the Directories of a Website Using PHP

How to List All Files of the Home Directory and Its Subdirectories of a Website Using PHP

How to Read the Contents of a File Using PHP

How to Write Contents to a File Using PHP

How to Copy a File Using PHP

How to Rename a File Using PHP

How to Delete a File Using PHP



HTML Comment Box is loading comments...