Search code examples
phpinputdirectorymkdir

Creating a new folder in another folder using PHP with name from input


I am trying to create a new folder in another folder, using the name for it from the input.

<form>
   Album name <input type="text" name="album_name">
    <input type="submit">
</form>

<?php

if(isset($_POST['album_name'])) {
    $albumName = $_POST['album_name'];
    $path = 'albums/' . $albumName;


    if (!file_exists($albumName)) {
        mkdir($path, 0777, true);
    }
}else{
    echo 'it is so saad';
}

?>

But the folder hasn't created. What is the problem? :(


Solution

  • Your script checks $_POST variable but you send data with GET method. Add method="POST" to your form tag, it will work.

    <form method="POST">
       Album name <input type="text" name="album_name">
        <input type="submit">
    </form>
    
    <?php
    
    if(isset($_POST['album_name'])) {
        $albumName = $_POST['album_name'];
        $path = 'albums/' . $albumName;
    
    
        if (!file_exists($albumName)) {
            mkdir($path, 0777, true);
        }
    }else{
        echo 'it is so saad';
    }
    
    ?>