简体   繁体   中英

How to rename a subfolder in php

I have created a folder using php whenever a new client creates a business name

$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.

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

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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