Make Destination Directory on File upload with PHP

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

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

To avoid this you can create separate directories for the store 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. Conclusion

 


 

1. Table structure

Create users table.

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 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 action='multipart/form-data' > following with the username which contains the file element and a submit button.

Create directory and upload

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 create a directory if it does not exist in the upload directory.

Completed Code

<?php
include "config.php";

if(isset($_POST['submit'])){

  # Username
  $username = $_POST['username'];

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

  # Getting 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
  move_uploaded_file($_FILES['file']['tmp_name'],$location);

}

?>

<!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. Conclusion

Use the above script if you want to organize the users files on their specific destination directory instead of storing all users file in one place.

In this way, a new directory dynamically created for a new user. You can either use the username or id for the name of the directory or any other name which is unique.

Related Post

Spread the love

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *