简体   繁体   English

如何在php中重命名子文件夹

[英]How to rename a subfolder in php

I have created a folder using php whenever a new client creates a business name 每当新客户创建公司名称时,我都使用php创建了一个文件夹

$sql = "INSERT INTO kd_aboutus (business, category, subcategory, content, uafk, datein) VALUES ('$bn','$cat','$scat','$descr','$userID',now())";
    $run_sql = mysqli_query($conn, $sql);

    if (!file_exists("user/$bn")) {
        mkdir("user/$bn", 0755);
    }

Now my problem is when the user wants to edit his or her business name, I also need to rename their folder which they had already created. 现在我的问题是,当用户想要编辑他或她的公司名称时,我还需要重命名他们已经创建的文件夹。 How am I supposed to do this. 我应该怎么做。 This is my code in the edit file 这是我在编辑文件中的代码

$sql = "UPDATE kd_aboutus SET business='$bn', category='$cat', subcategory='$scat', content='$descr' WHERE aboutID='$targetID'";
    $run_sql = mysqli_query($conn, $sql);

You will need the rename function. 您将需要重命名功能。 Note that the minimum parameters are string $oldname , string $newname , so you will need to load the current name before changing the record in the database. 请注意,最小参数是string $oldname , string $newname ,因此在更改数据库中的记录之前,您将需要加载当前名称。

Example

<?php

$query = "SELECT business FROM kd_aboutus WHERE aboutID='$targetID'";

if ($result = $mysqli->query($query)) {
    $row = $result->fetch_assoc();
    if (!empty($row['business'])) {
        $oldBn = $row['business'];
        rename("user/$oldBn", "user/$bn");

        $sql = "UPDATE kd_aboutus SET business='$bn', category='$cat', subcategory='$scat', content='$descr' WHERE aboutID='$targetID'";
        $run_sql = mysqli_query($conn, $sql);
    }
}

The above is untested but should illustrate the sort of thing you'll need to achieve. 上面的内容未经测试,但可以说明您需要实现的目标。

You will first have to get current folder name before renaming it. 重命名之前,您首先必须获取当前文件夹名称。

SELECT business FROM kd_aboutus WHERE aboutID='$targetID';

Get the result of above query in a variable. 在变量中获取上述查询的结果。 Let's say $oldbn 比方说$oldbn

Then, rename the folder as: 然后,将该文件夹重命名为:

rename("user/$oldbn", "user/$bn");

$sql = "UPDATE kd_aboutus SET business='$bn', category='$cat', subcategory='$scat', content='$descr' WHERE aboutID='$targetID'";
$run_sql = mysqli_query($conn, $sql);

PHP具有重命名文件夹/文件的功能: http : //php.net/manual/en/function.rename.php

rename(oldname,newname[,context])

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

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