在PHP中创建新文件并更新这些创建文件的列表

I am trying to make a website that will hold RPG character sheets dynamically. I would like to be able to create new character sheets by submitting a form with the sheet's title, like this (this is part of the index.php page):

<form action = "charCreate.php" method = "post">
    <h1>Character Sheet Name:</h1> 
    <input type = "text" name = "fileName">
    <input type = "submit" value="Submit">
</form>

I am aware of the fopen method, but I'm not sure exactly how to use it in this scenario. I want to be able to create new webpages with this form, and have the index.php display a list of the files that have been created using the above form.

What would be the best way to dynamically update a list of created webpages and to create these webpages, using the value in the form for the file name.

I also want to find out how to alter these newly created pages, but I need to figure this out first.

Thanks.

Do the following:

<?php
    // w will create a file if not exists
    if($loHandle = @fopen('folder_to_add_files/'.$_POST['fileName'], 'w'))
    {
        echo 'Whoops something went wrong..';
    }
    else
    {
        // you can write some default text into the file
        if(!@fwrite($loHandle, 'Hello World'))
        {
            echo 'Could not right to file';
        }

        @fclose($loHandle);
    }
?>

Watch out for spaces and other weird characters in your filename. You can replace spaces with str_replace like so:

// Replace spaces with underscores
$lstrFilename = str_replace(' ', '_', $_POST['fileName']);

To show the files in the index.php you can do the following:

<?php
    if ($loHandle = @opendir('folder_to_add_files')) 
    {
        echo 'Directory handle: '.$handle.'<br />';
        echo 'Entries:<br />';

        // This is the correct way to loop over the directory.
        while (false !== ($lstrFile = @readdir($loHandle))) 
        {
            echo $lstrFile.'<br />';
        }

        @closedir($loHandle);
     }
?>

The first, and most important point here is that you will run into scalability / data corruption issues trying to manage data in files - that's what databases are for.

It is possible to build large, fast systems just using flat files for storing data but this requires a lot of complex code to implement complex file locking queues. But given the alternative of simply using a database, it's rarely worth the effort.

Allowing the user to specify the filename means they will be able to trash any file writeable by the webserver uid on your machine. They'll also be able to deploy their own PHP code. Not a good idea.

For a quick and dirty solution (which will at some point in the future fail in horrible and painful ways...).

 function write_data($key, &$data)
 {
    $path=gen_path($key);
    if (!is_dir(dirname($path)) {
        mkdir(dirname($path), 0777, true);
    }
    return file_put_contents($path, serialize($data));
 }

 function get_data($key)
 {
    $path=gen_path($key);
    return unserialize(file_get_contents($path));
 }

 function gen_path($key)
 {
    $key=md5($key);
    return '/var/data/' . substr($key,0,2) . '/' . substr($key,2) . '.dat';

 }