Jump to content

mkdir


Append

Recommended Posts

Hello,

I was wondering if it was possible to do mkdir() on a windows server, I know it is so I'm going to continue asking my question. I've to make a form, with the action as 'something.php' for example. The 'something.php' file has this in it  '$name = $_POST['filename'];''. I was wondering if it was possible to get what was imputted in the form that I did in HTML, to make a file in 'C:\thirdir\$name'.

That probably doesn't make any sence, if you get it you're a legend.

Link to comment
Share on other sites

Okay, just you spoke about creating both a file and directory.

 

The mkdir() function should work fine for that, assuming the directory has the correct permissions. I would also validate the file name, so that the path cannot be altered (by "../" for example), though I'm not sure how applicable that is in this situation..

 

if (!empty($_POST['dirname']))
{
    $name = $_POST['dirname'];

    // Check the dir name doesn't contain "..", "/" or "\" - you'll
    // want to build on this for more comprehensive validation
    if (!preg_match('/[(\.\.)\/\\\]/', $name))
    {
        // Note the double backslash
        $path = 'C:\path\to\\';

        // Create the dir
        if (mkdir($path . $dir))
        {
            echo 'Success';
        }
        else
        {
            echo 'Failure';
        }
    }
    else
    {
        echo 'Invalid directory name';
    }
}

 

As I said before though, this will only work if the user PHP is running as, has permission to write to that directory. You'll probably get an error if it can't.

Link to comment
Share on other sites

Sorry, there's a typo:

 

        if (mkdir($path . $dir))

 

Should be:

        if (mkdir($path . $name))

 

Although you're going to want to change the $path value to the actual path where you want the directories creating. Be sure to end it with a double backslash, otherwise the single backslash will effectively escape the single quote, and you'll have a syntax error.

Link to comment
Share on other sites

That worked, thanks!

Just another quesiton, prevents me from making a new thread, is there a way with PHP that I can have a 'copy to' ?

So, If I have a file in '../files/file.zip' can I copy it to 'C:\placetocopy\$string from folder just made\file.zip'?

Link to comment
Share on other sites

$file = 'directory/zipfile.zip';

$newfile = 'C:\directory2\zipfile.zip';

 

if (!copy($file, $newfile)) {

    echo "Failed $file...\n";

}

 

Right now i have this

But I want to be able to make the $newfile location to go to the file we just made (the one that's imputted into the textbox)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.