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
https://forums.phpfreaks.com/topic/230693-mkdir/
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
https://forums.phpfreaks.com/topic/230693-mkdir/#findComment-1187736
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
https://forums.phpfreaks.com/topic/230693-mkdir/#findComment-1187749
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
https://forums.phpfreaks.com/topic/230693-mkdir/#findComment-1187788
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.