Reputation: 541
my current folder path is $path = car_images;
Its working for me. i can upload my images into this folder directly.
But i want to create separate folders for each user. so now images should be upload into $path = car_images/$emp_code;
means i want to first create directory with name $emp_code
into car_images
folder and then into $emp_code
directory images will upload.
My current code that's not creating directory into car_images folder.
$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.",";
}
Upvotes: 4
Views: 1908
Reputation: 1581
This has to work, try it.
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.
You Can change the below code according to your need, So below I have answered for the asked question.
Here, $name and $tmp_name required 'Later' for the function move_uploaded_file($parameter1,$parameter2).
if(isset($_FILES) & !empty($_FILES)){
$name = $_FILES['file']['name'];
/* $size = $_FILES['file']['size'];
$type = $_FILES['file']['type']; */
$tmp_name = $_FILES['file']['tmp_name'];
}
To set it, I have assumed it as $_SESSION. But you can modify it according to your code like $_POST or $_GET. file_exists() checks whether file/dir is present or not.
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.'/';
}
Now the directory as $emp_code will be in the folder $car_images, then you move the file to $emp_code directory.
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";
}
}
Later if this works, then you can modify code to limited IF statements. Hope it helped. :)
Upvotes: 5