【问题标题】:PHP creating path and folders with variable namesPHP使用变量名创建路径和文件夹
【发布时间】: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


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案2】:

    要完成这项工作,您确实需要从字符串中分离出变量(如 Ben Griffiths 所述)并检查它是否为空。另外,请确保您首先使用 mkdir() 创建了目录(aschuler 也提到了这一点)。因此,代码可能看起来像这样,但不知道 $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);
    }
    
    ?>
    

    希望这会有所帮助。

    【讨论】:

    • 这应该可以,我可以看到它应该可以,但由于某种原因,即使我这样做,它也会创建名为 $username 的文件夹,或者我之前使用的代码根本没有创建它。你认为这是我的文件夹权限吗?
    • 不太可能。我已经在我的服务器上运行了脚本,虽然是的,但我确实必须将运行脚本的文件夹中的权限更改为 0755,当它运行时,它正确地创建了带有 $username 变量的目录。跨度>
    【解决方案3】:

    您是说这个/users/$username/title.txt 完全创建/users/$username/title.txt

    所以你的问题是你需要先捕获 $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);
    ?>
    

    【讨论】:

      猜你喜欢
      • 2015-07-15
      • 1970-01-01
      • 2023-02-15
      • 2020-03-31
      • 2022-10-15
      • 2011-04-14
      • 2019-04-11
      • 2012-03-16
      • 1970-01-01
      相关资源
      最近更新 更多