如何使用webhost中的另一个php文件创建php文件


how to create a php file using another php file inside your webhost

我创建了一个php文件,该文件创建了另一个php文件,代码在这里:

if (isset($_POST["Submit"])) {
$name=$_POST['name'];
$string = '<?php
echo "'. $_POST["message"]. '";
?>
';
$fp = fopen("$name.php", "w");
fwrite($fp, $string);
fclose($fp);
}

它在我的本地机器上使用xampp成功地创建了一个php文件,但是当我把它上传到我的网站时,每次我点击提交,它都不会创建我写的任何文件。我不明白它怎么了,有人说我需要把它连接到我的ftp服务器,但我不明白。提前感谢各位的帮助。

检查要保存文件的目录的权限。它一定是664。

我认为这里有3件事需要解决。1) 文件权限2) 重写文件3) 我认为你的$string行中有一个错误,因为你已经在php代码中了

试试这个

<?php
//If the form is submited
if (isset($_POST["Submit"])) 
{
//we parse the informations
$name=addslashes($_POST['name']);
$string = addslashes($_POST["message"]);
//We will recreate the file even if it was existing 
$fp = fopen("$name.php", "w+");
fwrite($fp, $string);
fclose($fp);
//We give access permission to the file
chmod("$name.php", 0777);
}
?>

此外,如果你在一个目录中创建文件,请使用FileZilla等ftp软件连接到你的ftp,右键单击该目录,然后单击Chmod。在该目录上创建Chmod 0777。一切都应该正常。

提交验证

现在,如果它仍然不起作用,您可以通过使用以下代码创建文件名test.php来逐点搜索错误

<?php
//we parse the informations
$name='my_one_file';
$string = '<h1>It is working<h1>';
//We will recreate the file even if it was existing 
$fp = fopen("$name.php", "w+");
fwrite($fp, $string);
fclose($fp);
//We give access permission to the file
chmod("$name.php", 0777);
echo "<a href='$name.php' target='_blank'> <h1> $name.php</h1> </a>";
?>

然后在浏览器中启动您的网站/test.php。如果它有效,您应该在服务器上有一个文件名my_one_file.php。如果它显示了一个链接,那么问题出在您的If(isset($_POST["Submit"])上。这意味着我们必须交叉检查您的html表单

触摸验证

最后,如果它仍然不起作用,那么我们可以验证是否需要像某些服务器那样在修改之前先在服务器上创建文件。为此,您仍将创建一个test.php文件,其中包含以下代码

<?php
$name='my_one_file';
$string = 'My message';
touch("$name.php"); 

//We will recreate the file even if it was existing 
$fp = fopen("$name.php", "w+");
fwrite($fp, $string);
fclose($fp);
//We give access permission to the file
chmod("$name.php", 0777);
echo "<a href='$name.php' target='_blank'> <h1> $name.php</h1> </a>";
?>

然后打开你的网站/test.php。如果它有效,你应该在服务器上有一个文件名my_one_file.php