php - 使用数据库中的 user_id 作为文件名创建动态 php 页面

标签 php mysql fopen createfile

简单的用户帖子表单,一切都很好,但希望它生成一个使用数据库中的 user_id 的 php 文件。这样我就可以生成该页面的链接来查看单个帖子。我正在使用 fopen 创建也可以工作的页面。

如何让它生成“user_id”.php 作为新文件名?您可以看到它当前将文件命名为“newfile.php”。

// Create Post
session_start();
if(isset($_POST['createPostBtn'])){
  $myusername = $_SESSION['username'];
  $postTitle = $_POST['postTitle'];
  $postDesc = $_POST['postDesc'];
  $id = $row['user_id'];

  $myfile = fopen('newfile.php', 'w') or die("can't open file");

  $txt = "<?php include('init.inc.php'); ?>";
  $txt2 = "<?php include('header.php'); ?>";
  fwrite($myfile, $txt);
  fwrite($myfile, $txt2);
  fwrite($myfile, $myusername);
  fwrite($myfile, $id);
  fwrite($myfile, $postTitle);
  fwrite($myfile, $postDesc);
  fclose($myfile);

  mysql_query("INSERT INTO `users` (`post_title`, `post_description`, `username`) VALUES ('{$postTitle}', '{$postDesc}', '{$myusername}' )");   
}

最佳答案

尝试以下代码:

$id = '$row['user_id'].'.php'';
$myfile = fopen($id, 'w') or die("can't open file")

关于php - 使用数据库中的 user_id 作为文件名创建动态 php 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30273122/

相关文章:

php mysql 按多列选择顺序

mysql - 谁知道这个sql注入(inject)?

如果第二个表中不存在行,MySql 从两个表中选择

从打印更改为写入文件 c

c - 当其他文件正在尝试访问时,从文本文件中删除行

php - 在 Linux 上使用命令行 PHP 检查互联网连接

javascript - 使用 jquery 提交表单时获取返回值(不刷新)

security - 如何在 CSV 文件上安全地使用 fopen?

php - 使用关联类时如何删除实体?

php - 检查字符串是否以图像扩展名结尾