我当前的文件夹路径是 $path = car_images;
它为我工作。我可以直接将我的图片上传到这个文件夹中。
但我想为每个用户创建单独的文件夹。所以现在图像应该上传到 $path = car_images/$emp_code;
意思是我想先创建名为 $emp_code
的目录到 car_images
文件夹,然后到 $emp_code
目录,图像将上传。
我当前的代码没有在 car_images 文件夹中创建目录。
$path = "car_images/".$emp_code;
if (!is_dir($path.'/'.$emp_code))
{
mkdir($path.'/'.$emp_code, 0777, true);
}
$name = $_FILES['car_images']['name'][$i];
$size = $_FILES['car_images']['size'][$i];
list($txt, $ext) = explode(".", $name);
$file= time().substr(str_replace(" ", "_", $txt), 0);
$info = pathinfo($file);
$filename = $file.".".$ext;
if(move_uploaded_file($_FILES['car_images']['tmp_name'][$i], $path.$filename))
{
$file_name_all.=$filename.",";
}
最佳答案
这一定行得通,试试吧。
This is what you want :
Folder A
|
|--Folder_001---All files uploaded/has to be sent here by 001(user 1)
|--Folder_002---All files uploaded/has to be sent here by 002(user 2)
|--Folder_003---All files uploaded/has to be sent here by 003(user 3)
|--Folder_004---All files uploaded/has to be sent here by 004(user 4)
|--
|--
|--so on....
Inside directory: folder_A(which you already be having), New folders will be generated according to your need.(example: user1_folder, user2_folder..)
For example : when user uploads files, folders can be created according to user_id, Later when user uploads files, files go into:
folder_A --> User_id_folder --> User_id uploaded files will/can be present/sent here.
您可以根据需要更改以下代码,所以下面我已经回答了所提出的问题。
此处,函数 move_uploaded_file($parameter1,$parameter2) 的 $name 和 $tmp_name 需要“稍后”。
if(isset($_FILES) & !empty($_FILES)){
$name = $_FILES['file']['name'];
/* $size = $_FILES['file']['size'];
$type = $_FILES['file']['type']; */
$tmp_name = $_FILES['file']['tmp_name'];
}
为了设置它,我假设它是 $_SESSION。但是你可以根据你的代码修改它,比如 $_POST 或 $_GET。 file_exists() 检查文件/目录是否存在。
if (isset($_SESSION['emp_code'])){
$emp_code= $_SESSION['emp_code'];
if (!file_exists('car_images/'.$emp_code)) {
mkdir('car_images/'.$emp_code, 0777, true);
}
$location = 'car_images/'.$emp_code.'/';
}
现在 $emp_code 目录将在文件夹 $car_images 中,然后将文件移动到 $emp_code 目录。
if(isset($name) & !empty($name))
{
if(move_uploaded_file($tmp_name, $location.$name)){
// do the code here.
// In the sql query use it as '$location$name'
/* FOR EXAMPLE:
$sql = "UPDATE `table_name` SET `image`='$location$name' WHERE `email`='$email'";
*/
}
else {
echo "failed to upload";
}
}
- 现在您有了 car_images 文件夹:
- 在该文件夹中,您拥有所有 emp_codes 生成的文件夹,
- 在该文件夹中,您有他们上传的文件 (emp_codes)。
如果这可行,那么您可以将代码修改为有限的 IF 语句。 希望它有所帮助。 :)
关于php - 如何在php中创建目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50201067/