【发布时间】:2021-04-24 03:20:23
【问题描述】:
当我使用 PHP 编写文件时,
<?php
header('Access-Control-Allow-Headers: *');
$texttowrite = $_POST['articletext'];
$title = $_POST['title'];
$name = $_POST['name'];
// Illegal characters on Unix and Linux or any *nix server
$illegal = array(" ", "?", "/", "\\", "*", "|", "<", ">", '"');
// Legal characters
$legal = array("-", "_", "_", "_", "_", "_", "_", "_", "_");
// Replace it!
$newtitle = str_replace($illegal, $legal, $title);
$newname = str_replace($illegal, $legal, $name);
$filename = "content/" + $newtitle + "-" + $newname;
$myfile = fopen($filename, "w") or die("Unable to open file!");
fwrite($myfile, $texttowrite);
?>
注意:这个程序应该从 POST 表单数据并将其写入服务器。
它只是在服务器文件系统的根目录中创建一个名为“0”的文件。有什么问题吗?
编译器没有产生错误。
【问题讨论】:
-
调试。在
fopen之前打印$filename的值。这是你所期望的吗?一堆打印将显示每个变量的值。由于这不是致命错误(即您的日志中没有错误),因此它是一个逻辑错误。 -
在 1,402,389 个 PHP 问题(可能从 2008 年开始)中,肯定有一个重复的问题,但并不容易找到。有人能找到吗?类似的问题,但对于
+=运算符:How can I use the += operator in PHP for a string? -
重复(但它不是一个规范问题。它已关闭,但关闭目标是不是所有规范问题):Plus operator in PHP
-
为了将来参考,这个问题已经在 Meta 上讨论过:Why is a question about creating a file in PHP not about programming?
-
什么是(规范)重复?
标签: php file server-side file-writing