Make Destination Directory on File upload with PHP

You normally store the user-selected files in a specific directory but the same directory is also used by all other users.

If you are allowing to upload multiple types of files like – images, videos, doc, etc then it becomes a mess.

To avoid this you can create separate directories to store files in an organized way.

But for storing it in a more organized way you can dynamically create directories for each user for storing their files. This makes traversing to file easier.

Make Destination Directory on File upload with PHP


Contents

  1. Table structure
  2. Configuration
  3. HTML & PHP
  4. Output
  5. Conclusion

1. Table structure

Create users table and added some records.

CREATE TABLE `users` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `username` varchar(80) NOT NULL,
  `name` varchar(80) NOT NULL,
  `password` varchar(80) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

2. Configuration

Create a config.php for the database connection.

Completed Code

<?php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "tutorial"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
  die("Connection failed: " . mysqli_connect_error());
}

3. HTML & PHP

Create a new upload directory for storing files.

List records from the users table and add a <form method='post' action='' enctype='multipart/form-data' > following with the username which contains the file element and a submit button.

Create a directory and upload a file

I am using the username for the directory name. For this, I created a hidden element with username value in <form >.

On the form submit select the POST username and with mkdir() method creates a directory if it does not exist in the upload directory.

Completed Code

<?php
include "config.php";

if(isset($_POST['submit'])){
  if(isset($_POST['username']) && isset($_FILES['file']['name'])){
     # Username
     $username = $_POST['username'];

     # Get file name
     $filename = $_FILES['file']['name'];

     # Get File size
     $filesize = $_FILES['file']['size'];

     # Location
     $location = "upload/".$username;

     # create directory if not exists in upload/ directory
     if(!is_dir($location)){
       mkdir($location, 0755);
     }

     $location .= "/".$filename;

     # Upload file
     if(move_uploaded_file($_FILES['file']['tmp_name'],$location)){
        echo "File uploaded.";
     }
  }
}

?>

<!doctype html>
<html>
 <body >
 
 <!-- User list -->
 <table border='1'>
   <tr>
    <td>S.no</td>
    <td>Username</td>
    <td>&nbsp;</td>
   </tr>
   <?php

   $fetch_user = mysqli_query($con,"select * from users");
   $count = 1;
   while($row = mysqli_fetch_assoc($fetch_user)){
     $username = $row['username'];
   ?>

   <tr>
    <td><?= $count ?></td>
    <td><?= $username ?></td>
    <td>
     <!-- Form -->
     <form method='post' action='' enctype='multipart/form-data'>
      <input type='hidden' value='<?= $username ?>' name='username' >
      <input type="file" name="file" id="file" >
      <input type='submit' name='submit' value='Upload'>
     </form>
   </td>
  </tr>
  <?php
   $count++;
  }
  ?>
 </table>
 
 </body>
</html>

4. Output

View Output


5. Conclusion

Using the above script you can create a new directory if not exists dynamically using mkdir() and upload a file.

Specify your directory location in mkdir() function for creation.

If you found this tutorial helpful then don't forget to share.