简体   繁体   English

PHP用变量名创建路径和文件夹

[英]PHP creating path and folders with variable names

In PHP I am trying to make a new database type folder for different people on sign up and add files. 在PHP中,我试图为注册和添加文件的不同人员创建一个新的数据库类型文件夹。 I could easily create files and write to them but for some reason every time i try to have PHP create folders using the persons username variable as the path, all it does is create a folder named $username. 我可以轻松地创建文件并写入文件,但是由于某种原因,每次我尝试使PHP使用persons username变量作为路径创建文件夹时,它所做的只是创建一个名为$ username的文件夹。

Here is my code cut down to the basics of that part. 这是我的代码,涉及该部分的基础知识。

<?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);
?>

Try This 尝试这个

<?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);
?>

To make this work, you do need to break out the variable (as Ben Griffiths mentioned) from the string and check it isn't empty. 为了使此工作有效,您确实需要从字符串中分解出变量(如Ben Griffiths所述),并检查它是否为空。 Also, make sure you do make the directory first created using mkdir() (aschuler mentioned this as well). 另外,请确保确实使用mkdir()首先创建了目录(aschuler也提到了此目录)。 The code, therefore, may look something like this but without knowing where $username, $structure, $title and $template come from, you may need to alter this a little: 因此,代码可能看起来像这样,但是在不知道$ username,$ structure,$ title和$ template的来源的情况下,您可能需要稍作改动:

<?php
$title = $_POST['title'];
if (trim($username) == '') {
    die("No username passed in!");
} else {
    $userdir = "/users".$username."/";
    mkdir($userdir);
    $fh = fopen($userdir."title.txt", 'w') or die("There was an error in changing your title.  <br />");

    $stringData = $title."\n";
    fwrite($fh, $stringData);
    fclose($fh);
}

$template = $_POST['temp'];
if (trim($template) == '') {
    die("No template passed in!");
} else {
    $structdir = $structure."/";
    mkdir($structdir);
    $fh = fopen($structdir."template.txt", 'w') or die("There was an error in changing your template.  <br />");

    $stringData = $template."\n";
    fwrite($fh, $stringData);
    fclose($fh);
}

?>

Hope this helps. 希望这可以帮助。

You're saying that this /users/$username/title.txt create exactly /users/$username/title.txt 您是说这个/users/$username/title.txt精确地创建了/users/$username/title.txt

so Your problem is you need to catch $username first, I don't know how your code look but maybe this? 所以你的问题是你需要先捕获$ username,我不知道你的代码看起来如何,但是也许是这样吗?

<?php $username=$_SESSION['username']; //retrieve the username 
    //rest of your code 
    $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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM