简体   繁体   中英

create a folder and in there a subfolder with php

I've tried soooo many times now and I couldn't find a solution!

My problem:

I want users to be able to upload images in the folder "user_images" (this folder exists already on the server). The php script shall create a specific user subfolder in "user_images". Like this -> "user_images/user_65/(imagesGoHere)"

I tried it like that, but it doesn't work ->

$userid = $_SESSION['user_id'];
$folder = "user_".$userid;
$upload_dir = "user_images/.$folder";           
$upload_path = $upload_dir; 

if(!is_dir($upload_dir)){
mkdir($upload_dir, 0777);
chmod($upload_dir, 0777);
}

Would be nice if someone could help me! Thx

您需要在此处指定第三个参数,即布尔值:

mkdir($upload_dir, 0777, true);

PHP can only create directories if it has the privileges in the parent directory to do so. As a word of warning, creating a folder per user is not a particularly scalable solution.

It shouldn't need to be recursive and chmodding after creation is pointless because mkdir does it already.

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