我将在这个例子中使用PDO connection
假设你在数据库中有一个表,像这样。
表名:设置
+----+---------+---------------------+
| id | name | property |
+----+---------+---------------------+
| 1 | color | red |
+----+---------+---------------------+
创建文件
当您想创建一个新文件时,使用新名称。您只需要在数据库中编辑row[id=1]。
首先我们需要编辑row[id=1] 来创建一个新的文件名。
<?php
require_once 'connection.php';
$NewColor = 'green';
$UpdateDataBase = $conn->prepare("UPDATE settings SET property = ? WHERE id = 1");
$UpdateDataBase->execute([$NewColor]);
我们将property 更改为数据库中带有ID = 1 的行。从red 到green。
现在我们需要使用fopen() 和fclose()
我们现在要从我们的数据库中获取green,以便创建新文件。
<?php
require_once 'connection.php';
// We gonna take a row from database where id = 1. Because that ID has our information.
$NewFileName = $conn->prepare("SELECT * FROM settings WHERE id = 1 LIMIT 1")
$NewFileName->execute();
// Now we will open foreach loop.
foreach($NewFileName->fetchAll() as $row){
$NewFile = 'path/to/the/file/'.$row['property'].'.php';
// now we gonna use fopen()
fopen($NewFile, 'w');
fclose($NewFile);
}
重命名文件
现在我们将使用rename()
<?php
require_once 'connection.php';
$oldfile = 'path/to/the/file/red.php';
$RenameFile= $conn->prepare("SELECT * FROM settings WHERE id = 1 LIMIT 1");
$RenameFile->execute();
foreach ($RenameFile->fetchAll as $row){
$newfile = 'path/to/the/file/'.$row['property'].'.php';
// Now we gonna use rename()
rename($oldfile, $newfile);
}
但在你使用它之前,请查看Hacking using “rename()” of PHP