Read the full blog." /> Read the full blog."> Create Directory in PHP with User Input | Slunked

Create Directory in PHP with User Input

Jump To:   «  1 2  View All »

by Frost

Recently I had an inquiry about how I would go about creating a directory in a PHP script using a FORM with POST / GET data. Creating a Directory from a PHP script using PHP is not all that easy and should be done with caution, as you want to make sure that you validate the input and prevent un-wanted characters from being used to create a directory.

Use this script with caution and I would even go so far as to hiding it behind a User Login script and require a valid user.

The Form
For simplicity we will use a very basic input form to submit POST data to the PHP script (I opted to use POST because it is my preference for a php script like this). You can use GET, just be sure to reflect that in the form and on the PHP script. The components of the PHP script will simply be the <form> tags and 2 input tags (one for directory name and one for submit). Here it is:

Make Directory


Make Directory on Server

Enter a Directory Name (Alpha-Numeric only):

The form is really straight forward, we will add more to this form onto our next step which is creating the actual PHP script.

Jump To:   «  1 2  View All »


Leave us a comment

  • K.Dhakal

    Thank you very much. Good Code. I’m happy.

  • Matt

    I have the php file working correctly but want to know how to return the results in a new separate window instead of the current window.

  • Frost

    @Matt: You would need to use javascript to do that. Given I am not a huge Javascript buff, I would suggest you do a google on Javascript and new windows, which should lead you in the right direction.

  • Mohamed

    its working but i need create a label folder like gmail label in php

  • Jimmy

    Warning: mkdir() [function.mkdir]: No such file or directory in C:xampphtdocsrandomcreateDir.php on line 20

    How can I fix this; I’m also getting the error: “Unable to create dir” in red letters… Any idea’s?

  • Johnny

    I got the same error. why?

  • yaswanth

    how to fix
    Warning: mkdir() [function.mkdir]: No such file or directory in /opt/lampp/htdocs/createDir.php on line 20

    • Frost

      The error means that you do not have the proper permissions for creating a directory. You need to have write access to the folder where you will be creating the directory out (chmod 0755) and the owner / group needs to coordinate with whatever PHP is being ran as, in most cases it will be something like www-data:www-data.

  • angela

    Hello, first of all thanks for posting this, its exactly what i was looking for.
    I was wondering if you or anyone around here may know a way to not only create a directory from a user form, but also auto-generate an index file that would just be a copy of the index file of that parent directory or all other subdirectories? (for instance, i am building a directory browser/file uploader for clients to share files with our company, so each folder has an index that has a styled directory browser with file previews and an upload form instead of seeing the raw directory file list.. i’d like them to be able to create a directory but for the directory to have an index in it that matches the theme of the other directories they are able to browse, if that makes sense)

Leave a Comment