Jump to content

[SOLVED] create a folder and create a file in it in the same page.


ted_chou12

Recommended Posts

I seem to find trouble both creating the folder itself and a file to store in it in the same page. The folder can be created, but the file doesnt seem to be stored into the folder... and you need a new page,(second page) for it to store the file in the folder, what may be the problem?
Ted.
When you go to create the folder. Keep the folder name in a variable. Then use that variable when creating the file. Example:
[code]<?php

if(isset($_POST['submit']))
{
    $folder = $_POST['newFolder'];

    if(!file_exists($folder))
    {
        if(!mkdir($folder))
        {
            echo "Unable to create a new folder '$folder'";
        }
    }


    $fileName = $_POST['newFileName'];
    $fileContents = $_POST['newFileContents'];

    $handle = fopen('./' . $folder . '/' . $fileName, 'wb');

    fwrite($handle, $fileContents, strlen($fileContents));

    fclose($handle);

    echo '<a href="./' . $folder . '/' . $fileName . '">' . $fileName . '</a> successfully crrated!';
}

?>
<h1>Create New File and Folder</h1>
<form action="" method="post">
  Folder Name: <input type="text" name="newFolder" /><br />
  File Name: <input type="text" name="newFileName" /><br />
  File contents: <textarea name="newFileContents"></textarea><br />
  <input type="submit" name="submit" value="Create!" />
</form>[/code]
Extremely basic. But it shows what I mean.

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.