dongposhi8677 2012-03-29 08:25
浏览 20

在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.

  • 写回答

2条回答 默认 最新

  • dongsuoxi1790 2012-03-29 08:34
    关注

    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);
         }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制