Php Directory operations
Thanks to directory operations with PHP, we can create new directories, move directories, rename them, delete them and show what's inside. Creating software such as a web-based file manager or file directories Thanks to directory operations, which is one of the subjects we need to learn in order to start uploading work, we have handled all file management issues. As with file functions, we will do most of the work in directory operations with functions. As I explained in the previous lesson, we will need permission and authority for directory events.
Creating a directory with PHP
We realize this event, which can also be considered as creating a new folder, thanks to the mkdir() function. It works with one parameter, but if you want, you can specify the file CHMOD value with the 2nd parameter.
mkdir('hello');
When we run the example above, we create a new folder named hello in the directory we run. If we want to express the CHMOD value of the new directory we will create, we activate the 2nd parameter.2. If we do not use the parameter, its default value is “0777”, meaning all permissions are granted.
mkdir('newdirectory', '0655');
In this example, we created a directory named newdirectory and canceled the write permission by giving it a CHMOD value of 0655. Deleting a directory with PHP We use the rmdir() function to delete existing directories. The directory we will delete with this function must be empty. If we try to delete a full directory in this way, PHP will get angry and show an error. If we want to delete a full directory as is, then we will first need to delete all the content using the unlink() function that we explained in the other lesson.
rmdir('hello');
When this example runs, there will be no directory named hello.
Changing and moving directories or files with PHP
Actually, we can say that renaming directories or files and moving them are the same thing. Although it may seem strange, when you change its name, you actually move it in a way. We will do this with the rename() function. In the first parameter of this function, which works with two parameters, we write the name of the file or directory we will move or change, and in the second parameter we write the path we will move.
rename('old', 'new');
When the above example runs, it changes the old named folder to the new one. Likewise, when we specify a file extension and name instead of a directory, this time the replacement will be made on the file.
rename('picture.jpg', 'landscape.jpg' );
In this study, we will change the name of our file named picture.jpg to landscape.jpg. Similarly, when we specify a different directory in the second parameter, it will move the file to that directory.
rename('image/image.jpg', 'landscapes/image.jpg' );
Here, we moved a.jpg file in the picture folder to the landscapes folder and changed its name to picture.jpg.
Reading the contents of a directory with PHP
To list all the files in a directory, we need two functions and a while loop. First, the opendir() function is the same as fopen() We open the directory with the same logic as opening a file. Then, we connect the variable to which we loaded the directory link with the readdir() function and the while loop and access the content of the entire directory.
$directory = opendir('files');while($file = readdir($dir)) { echo $file ;}
The issue that will draw your attention here may be the condition I used in the while loop. In this condition, where we use a single equal sign, the purpose is different compared to other conditions. Thanks to this special method, the loop will rotate one by one until it counts the files in the entire directory. The file names will be written on the screen in order according to the content of the files directory. For example, picture.jpg, file.rar and music.mp3. The output on the screen in the files directory with the file will be:...image.jpgfile.rarmusic.mp3. First published here. and .. signs indicate a subdirectory and the current directory. If you want, you can eliminate those characters with an if statement and access only the content.