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.

JavaScript

below is the table for users:

JavaScript

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

JavaScript

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

JavaScript

Update 1:

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

JavaScript

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.

JavaScript
JavaScript

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

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement