PHP File and Folder Management

PHP File and Folder Management

Hello everyone, in this article we are going to talk about directory management in PHP. We will create directories and manage our files. We will move and delete files and folders if we are in need. Let's get Started...

Creating a Folder

For creating a folder with PHP first we have to check is the folder name that we are want to create is exist. If that folder is exist the server will not create the folder and return false. To check the folder is exist we use file_exist function.

<?php $path = "path/to/directory"; if (!file_exists($path)) { //Here code block if the folder path do not exist } ?>

If the related folder do not exist we can create the folder with the mkdir function. Below you can see the usage of mkdir function.


mkdir ( string $pathname, int $mode = 0777, bool $recursive = FALSE , resource $context ) : bool
The values of mkdir function:
  • $pathname: This variable is required to specify the absolute path.
  • $mode : This is an optional variable. This variable will set the permissions of created file and directory. Default value is 0777. Below table you can see permission parameters
  • $recursive: Allows the creation of nested directories specified in the pathname
  • $context: If you want to specify the additional context you can add it as resource in here.
Permission mode values.
Value Permission
0cannot read, write or execute
1can only execute
2can only write
3can write and execute
4can only read
5can read and execute
6can read and write
7can read, write and execute
Now we can create our folder with below code block: <?php $path = "path/to/directory"; if (!file_exists($path)) { if(mkdir($path, 0777, true)){ //Here we create our folder with specified parameters and permissions }else{ //Create an E_WARNING level error if the specified permissions prevent to create the directory. } } else{ //Create an E_WARNING level error if the directory already exists. } ?>

Now we copy a file from another directory into this new created folder. Below code block you can see copy function usage.


copy ( string $source , string $target ) : bool

Below code block lets make an example for copying the files. If copied files exist in target location copied file will be overwritten on same file. Be carefull in this.

<?php $source = "images/temp/uploaded.png"; $target = "images/"; if (copy($source , $target )) { echo "$source copied...\n"; } else{ echo "$source could not copied...\n"; } ?> You can also check if the related file is exist and if exist do not copy the file into related directory. Below code block will show how to do that. <?php $source = "images/temp/uploaded.png"; $target = "images/"; $file_name = substr($source , 0, strrpos($source , ".")); $control_path = $target . "/" . $file_name; if (!file_exists($control_path )) { if (copy($source , $target )) { echo "$source copied...\n"; } else{ echo "$source could not copied...\n"; } } ?>

Now we know the copying the files. Lets see how can we move a file from another directory. Below code block will show the usage of the required function to move a file in the server.

The are two options for this. If the file is at the server we will use the rename function. If we want to move an uploaded file we will use the move_uploaded_file function.

First take a look at to the rename function

<?php //The source file absolute path $sourceFilePath = 'images/temp/uploaded.png'; //Target file absolute path $targetFilePath = 'images/content/uploaded.png'; //Move the file with rename function $isMoved= rename($sourceFilePath , $targetFilePath ); if($isMoved){ echo 'File moving is successfully!'; } ?>

And take a look at to the move_uploaded_file function. Below code block will show the usage of the function.


move_uploaded_file ( string $fileSource, string $target) : bool

Below code block will move the file under folder that named with year and month. It will chechk the folder is exist and if do not exist the related directory will be created before moving. After creating the folder the file will be moved.

<?php $target_dir = "img/" . date("Y") . '/' . date("m") . '/'; if (!is_dir($target_dir)) { mkdir($target_dir, 0777, true); } $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]) ; if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "File moving is successfully."; } ?>

That is all in this article.

Thanks for reading.

Have a nice file/folder processing.

Burak Hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment.

Comments

    There is no comment. Be the owner of first comment...