Skip to content
Advertisement

Share files between users [closed]

I have created an application which is based on PHP MySQL and I’m using XAMP server. Currently, I have created registration and login functionality for users and users can upload and download their own files. All the files are stored in the uploads/ folder. I have stored these files using the user’s id in another table. so one table is for storing users details and another table for storing user’s files according to their id. Now, I want to create functionality which allows a user to share the file to another user.

When I fetch all the files a user has uploaded, I tried to show a button next to the file that says “share”, that button sends the users filename but I am not sure how will I pass the other user’s id with whom I want to share the file and with that being passed, how can I share the file to the selected user. below is the code for displaying the logged in users file and the link through which share can be implemented.

while ($row = mysqli_fetch_array($result)){ 
        $filename = $row['file'];
        $filetype = $row['type'];
        $filesize = $row['size'];
        echo "
        <tr>
        <td>$filename</td>
        <td>$filetype</td>
        <td>$filesize KB</td>
        <td><a href='uploads/$filename' target='_blank'> Download file </br></td>
        <td><a href='delete.php?del=$filename' class='link'>Delete</td> 
        <td> <a href='shareFile.php?share=$filename' class='shareFile'>Share File </td>
        </tr>";



    }

}
else {
    echo "<p>NO FILES UPLOADED YET</p>";
}

below is the table for users:

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `username` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Files that a user uplaods is stored in the user_files according the user’s id. Below is the table for user_files table:

CREATE TABLE `user_files` (
  `id` int(11) NOT NULL,
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `file` longtext NOT NULL,
  `uploaded_on` datetime NOT NULL,
  `type` varchar(30) NOT NULL,
  `size` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Advertisement

Answer

In this case I may suggest that you add another field (column) to the user files table. This field may be called ‘shared_users_id’ and should hold a comma separated lists of ids of the shared users. For Ex.

Table: user_files

---------------------------------------
id | user_id | file | shared_users_id |
---------------------------------------
1  |1        |sample.jpg|2,3,4        |
---------------------------------------

Update 1:

When the share button is clicked, you can first make an AJAX request that list names of users available to be shared

<?php
   // Select all from users
   if(mysqli_num_rows($query) {
      echo '<ul>';
      while($users_lists = mysqli_fetch_assoc($query)) {
         echo '<li data-id="'.$users_lists['id'].'">'.$users_lists['name'].'</li>';
      }
      echo '</ul>';
   }

each list should carry the ids of the shares, so when the sharer clicks on any user, make another POST request (maybe with AJAX) that carries the ID of the shared user, then retrieve this ID from the $_POST handle block and execute the following action. For Ex we first assume that you have retrieved the lists of users when the sharer clicks the Share button.

// jQuery
$("#ajaxUserLists li").click(function(e) {
let user_id = $(this).data('id');
$.post( "handle-share.php", {user_id: user_id});
});
<ul id="ajaxUserLists">
<li data-id="1">Vishal Limbachiya</li>
<li data-id="2">Erisan Olasheni</li>
<li data-id="3"></li>
</ul>

And so to get the list of users of which a file is shared, you can call on the PHP explode function to turn lists to array, to add a new user, you can do array-push, to delete a shared user, you can do unset, then convert back to comma separated string with the implode function and update the database.

In the code: handle-share.php

<?php
    // Check if the AJAX request has been made
    if (isset($_POST['user_id']) {
       // request made
    // assuming that you retrieved the shared users list string into 
    // $shared_users_id variable
    $shared_users_id = $row['shared_users_id'];

    // Firstly turn if to array
    $shared_users = explode(",",trim($shared_users_id));

    // To add shared users
    $new_id = $_POST['user_id'];
    $shared_users[] = $new_id;

    // To remove a shared_user
    $user_id_to_remove = $_POST['user_id'];
    unset($shared_users[$user_id_to_remove]);

    //Convert back to comma separated string and
    // save to database

    $shared_users_id = implode(",",$shared_users);

    // Update the database table
    ...
}

?>

Update 2:

Find shared items for the current User

To inform a user that a file has been shared with him, it would have been so better to create a new table which has relationship with user_files table, the created tab stores details whenever a file is shared to a user, it records the name, time_shared, sharer_id, shared_user_id, is_seen the is_seen field tells whether the shared user has already seen the shared file or not (in order to create a file share notification system). But for the purpose of our table structure, You can run a PHP query like:

...
$current_user_id = 4;
$query = mysqli_query($conn, "select user_id, file from user_files where find_in_set($current_user_id,shared_users_id) <> 0");

// number of shared files
$number_of_shared_files = mysqli_num_rows($query);
// list all shared items and sharer_id
while($results = mysqli_fecth_assoc($query) {
      echo $results['file'].' '.$results['user_id'];
}
?>
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement