4

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.",";
            }
3
  • Does the mkdir give an warning, or are there any errors? Also i'd use $info['extension'] rather than the $ext. Commented May 6, 2018 at 15:01
  • no errors nothing. images are uploded into car_images folder Commented May 6, 2018 at 15:02
  • 1
    Is $emp_code set? Commented May 6, 2018 at 15:03

1 Answer 1

5

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";
            }
            }
  • Now you have car_images folder:
  • In that folder, you have all emp_codes generated folders,
  • In that folder, you have the files uploaded by them (emp_codes).

Later if this works, then you can modify code to limited IF statements. Hope it helped. :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.