Make a Directory in PHP

Introduction

File Handling is an important topic in any web application. We often need to make a directory, make a file, open a file, read a file, close a file, delete a file, processing into the file for various tasks.

Now I will describe how to create a directory in PHP.

So, let's start with the topic of creation of a directory. The mkdir function is used for make a directory in PHP.

Syntax of mkdir (Make directory)

bool mkdir ( string $pathname [, int $mode =0777 [, bool $recursive = false]])

pathname: pathname is the directory path.

mode: Access permission for a directory, 0777 is the default mode. mode is ignored in Windows.

recursive: Allows us to creae nested directories.  

The return value of the mkdir function is bool, so it will always return either TRUE  on success or FALSE on failure.

<!DOCTYPE html>
<
html>
<
head>
<
title></title>
</
head>
<
body>
<
form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<
div style="width:330px;top:120px; left:220px;position:relative; border:1px solid gray; margin-bottom:5px; padding:5px; padding-top:17px; ">
<div style="position:relative; float:leftpadding-top:8px; left:5px; padding-bottom:15px; "><h3> Directory creatation programme:</h3></div>
<
div style="position:relative; float:left; left:12pxpadding-top:8px; ">Name of the folder:</div>
<
div style="position:relative; float:left; left:45pxpadding-top:8px;"><input type="text" name="folderName" size="15" id="fldr" /></div>
<
div style="position:relative; float:leftpadding-top:8px; left:12px"></div>
<
div style="clear:both; position:relative; margin-bottom:12px;"></div>
<
input type="submit" name="MkDir" value="Ok" style="position:relative; left:160px; " />
<?php
if($_POST['MkDir'])
{
$str='D:\Developer\\';
$str1=$_POST['folderName'];
$str=$str . $str1; //string concatenation logic
 if (!mkdir($str, 0777, true))
{
die('<br/><br/> Failed to create folders...');
 }
else{ echo "<br/><br/>New folder is created succesfully!"; }
 
$str="";
 }
?>
</div>
</
form>
</
body>
</
html>

FolderName

ClickOnOkButton 
finalScreen 

Summary

In this article we learned how to make a new folder/directory in PHP. 


Similar Articles