【发布时间】:2012-03-13 21:15:45
【问题描述】:
在 PHP 中,我正在尝试为注册和添加文件的不同人创建一个新的数据库类型文件夹。我可以轻松地创建文件并写入它们,但由于某种原因,每次我尝试让 PHP 使用个人用户名变量作为路径创建文件夹时,它所做的只是创建一个名为 $username 的文件夹。
这是我的代码,简化为该部分的基础知识。
<?php
$title = $_POST["title"];
$myFile = "/users/$username/title.txt";
$fh = fopen($myFile, 'w') or die("There was an error in changing your title. <br />");
$stringData = "$title\n";
fwrite($fh, $stringData);
fclose($fh);
$template = $_POST["temp"];
$myFile = "$structure/template.txt";
$fh = fopen($myFile, 'w') or die("There was an error in changing your template. <br />");
$stringData = "$template\n";
fwrite($fh, $stringData);
fclose($fh);
?>
【问题讨论】:
-
制作目录的代码在哪里?
-
您是否尝试过从字符串中拆分出 $username:
$myFile = "/users/".$username."/title.txt"; -
之前应该使用
mkdir()函数。 fr2.php.net/manual/en/function.mkdir.php -
能否提供$username 和$structure 的值?
-
我在上面说过,但它只是创建路径 /users/$username/title.txt 它没有放入文件路径的实际用户名。我曾经尝试过 mkdir,但这只是创建了一个名为 $username 的目录。
标签: php html variables directory