doujia1163 2015-11-24 09:14
浏览 20
已采纳

用php创建向上计数的html文件?

I would like to know if I could create a new php file everytime someone clicks a button. The button looks like this:

<button onClick="generate.php"> Generate </button>

PHP:

What do I do here?

I would like the page generated to be a number that goes up for every button click. So, 50 pages have been generated, so the next click makes a file named "51.html", and so on.

I know that for the log, you have to do something like this:

<?php
$file = fopen('countlog.txt', 'r');
$dat = fread($fil, filesize('./class/countlog.txt')); 
$write = fwrite($file, $data+1);
echo $data;
fclose($file);
?>
  • 写回答

1条回答 默认 最新

  • dpppic5186 2015-11-24 09:35
    关注

    Try This

    <?php 
    
    define('START_FILENAME', __DIR__ . "/starting_point");
    
    if (file_exists(START_FILENAME))
    {
        # There is that File - read its content
        $start = (int) file_get_contents(START_FILENAME);
    }
    else
    {
        # No File Found - Create new File
        $start =  0;
        @file_put_contents(START_FILENAME, "$start");
    }
    
    # When Form Submitted
    if (isset($_POST) and isset($_POST['Click']) and $_POST['Click'] == "Generate")
    {
        $file_name = "{$start}.html";
        @file_put_contents($file_name, "Hello World!");
        # Update Counter too
        $start = $start + 1;
        @file_put_contents(START_FILENAME, "$start", 0);
        echo "Generated Filename - $file_name";
    }
    
    
    ?>
    <form action="demo.php" method="post" accept-charset="utf-8">
        <input type="submit" name="Click" value="Generate">
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题