Wednesday, May 28, 2014

Directory (or) Folder Creation in PHP

mkdir function is used to create folder or directory in php.

Syntax:
mkdir($str, 0777, true)

PHP Code

<?php
        if(isset($_POST['createdirectory']))
        {
                $folder='D:\phptest\\'; // Desired folder path
                $foldername=$_POST['foldername'];
                $foldercreate=$folder . $foldername;
            if (!mkdir($foldercreate, 0777, true))
                {
                    die('Failed to create folder...');
                }
            else
                {
                    echo "Folder has been created successfully!";
                }
                    $foldercreate="";
                }
        ?>

HTML Form

 <form action="#" method="post">
      Enter Name of the folder:
      <input type="text" name="foldername" size="15" required />
      <input type="submit" name="createdirectory" value="Create" />
  </form>

Complete Code

 <form action="#" method="post">
  <h3>Directory (or) Folder Creation in PHP:</h3>
  Enter Name of the folder:
  <input type="text" name="foldername" size="15" required />
  <input type="submit" name="createdirectory" value="Create" />
  <?php
if(isset($_POST['createdirectory']))
{
        $folder='D:\phptest\\'; // Desired folder path
        $foldername=$_POST['foldername'];
        $foldercreate=$folder . $foldername;
     if (!mkdir($foldercreate, 0777, true))
        {
            die('Failed to create folder...');
        }
    else
        {
            echo "Folder has been created successfully!";
        }
            $foldercreate="";
        }
?>
  </div>
</form>

Please share your comments and feebback.Thanks.Please subscribe my updates via email.

No comments:

Post a Comment