这是我想让它能够通过用户名为用户创建多个目录的代码。
现在它只创建一个名为big
<?php
$db = new PDO("..."); // Connection details here
$stmt = $db->prepare("SELECT * from some_table where user_id = :id"); // Finds the information based on an ID. Change this depending on how you want the select to work
$stmt->execute(array(':id' => "1")); // Gives value to :id and executes the statement
$row = $stmt->fetch();
// Then run your code
if (!file_exists(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big')) {
mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big', 0777, true);
}
?>
我想要另外两个这样的目录:
user/upload/'.$row['UserName'].'/avatar/small
user/upload/'.$row['UserName'].'/avatar/original
答案 0 :(得分:2)
你已经有了解决方案。继续做你正在做的事情......
if( !is_dir( (ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big' ) ){
mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big', 0777, true);
}
if( !is_dir ( (ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big' ) ){
mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big', 0777, true);
}
等等
答案 1 :(得分:1)
只需遍历结果并创建目录(is_dir比file_exists更快)
$db = new PDO("..."); // Connection details here
$stmt = $db->prepare("SELECT * from some_table where user_id = :id"); // Finds the information based on an ID. Change this depending on how you want the select to work
$stmt->execute(array(':id' => "1")); // Gives value to :id and executes the statement
// Then run your code
while ($row = $stmt->fetch()) {
array_walk(array('big', 'small', 'original'), function(&$v, $k) {
$dir = ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/' . $v;
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
}
}
答案 2 :(得分:0)
您需要创建一个用户名的单个文件夹,然后根据需要在用户名文件夹下创建其他文件夹,并且访问权限类似。
if (!file_exists(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big')) {
@mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big', 0777, true);
}
if (!file_exists(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/small')) {
@mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/small', 0777, true);
}
if (!file_exists(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/original')) {
@mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/original', 0777, true);
}
答案 3 :(得分:0)
您需要做的就是修改我给您的原始脚本:
<?php
$db = new PDO("..."); // Connection details here
$stmt = $db->prepare("SELECT * from some_table where user_id = :id"); // Finds the information based on an ID. Change this depending on how you want the select to work
$stmt->execute(array(':id' => "1")); // Gives value to :id and executes the statement
$row = $stmt->fetch();
// Then run your code
if (!file_exists(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big')) {
mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big', 0777, true);
}
if (!file_exists(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/small')) {
mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/small', 0777, true);
}
if (!file_exists(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/original')) {
mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/original', 0777, true);
}
?>