I'm working on a user management system that uses PHP and mysql.(XAMPP)
it has around 1000+ user records in A table called "users" with "id" as primary key.
one of the columns is "avatar"
I have a folder with 1000+ jpg images. all user images are named by their ids. for example if a user named Jon with id=222 his photo will be 222.jpg
what I want to do is upload all photos to their users using the image name to be put to the correct user id in the table
what's the best approach I can take?
hope its clear and thanks in advance
I have some steps that you might do :
Upload file as usual, but in this step you must include user id as parameter
Query user by id
Rename file before save image, example :
$user->id . time() . '.' . $file->getClientExtension();
Upload it to folder
Save to your table
Related
I have a form for users to sign up. This form has a file input which will upload images in a directory in server by ajax instantly as user selects a file. There's also a button which will submit the form and saves user info in user table in database.
Each user can have multiple images. Ajax also save address of images in photos table when file uploads. this table has user_id column. but since the user_id will be generated when submit button is hit and user is not registered yet, I can't fill user_id column in photos table. The id of user is generated when whole form is submitted.
so should I update photos table when form submits by adding id of user to user_id column?
or I must save photos with ajax in temporary_photos table and then when submit is hit save them in main photos table and delete them from temporary table?
or is there another approach?
what if user uploads files, but closes the window without hitting submit button? how I find out the window is close and registration is canceled so i will delete images?
You are trying to solve a problem that you created. If user_id is a dependency for the photo to be saved, let the user be saved first.If you do it the other way , it you will over complicate the workflow.
So, i suggest you register the user first, save the user id in session (or log the user in) and then upload the photo.
If you must do it together, then let the ajax request register the user and then save the photo and save the user_id in a session variable. Next time you upload a photo you could get the user from session variable and save the photo.
function savePhoto(Request $input){
if(Session::has('user_id'){
$user = User::find(Session::get('user_id'));
}
else{
$user = User::create($input->all());
}
$user->photo->save($input->all());
}
You can try this way-
Since an user can have multiple images that means the relation is one to many between. So better if you use a images table.
Table: Images
Column(s): id, user_id, image, uploaded_at
Make user_id field is nullable and image column will save the name of uploaded images. Upon successful uploading and adding iamges record to database save last inserted ids in a session array. Once the user submit the form, create a record to user table, grab the last insert it, check if there are any data in session array, if found, loop through the array and select those images record by id and update user_id field. This will solve your problem.
You also asked for a way to delete images for which users didn't submit the form. To do this, you can run a cronjob/laravel task schedular. A script that will remove images and delete records for those images which were uploaded 5 mins (for example) ago and user_id is still null.
I have a table called user_photos which has the following structure:
id
img_url
uploaded_on
username
I also have a table called users, which has a column called profile_pic.
I have a PHP script which is called change_dp.php. The basic idea is that a user can upload photos and then on hover, they have a button called Make display Picture which when clicked, calls change_dp.php.
However, photos which users upload are stored in the user_photos table and the actual photo location is stored in this format (img_url):
user_data/user_photos/ronda2.jpg
user_data folder > user_photos > and then the file name.
However, in my users table, which is where profile_pic data is held, images are stored in this format in the profile_pic column:
ronda_rousey.jpg
Issue: The issue is that I need to execute an UPDATE query in order to allow a user to change their profile picture (What I need is to get the file name, and just the file name from user_photos and SET that to the profile_pic in the users table). But since the images are stored in different folders, I also need a way to copy the image from user_photos to profile_pics when the Make Display Picture button is clicked.
How I think I should tackle this issue:
My thinking was to get the data from user_photos, i.e. get the whole img_url - user_data/user_photos/ronda2.jpg - Obtain just the file name from this column (ronda2.jpg) and somehow find a way to copy that image to the profile_pics folder and store the file name (which would be assigned to a PHP var) in the in the profile_pic column in table users.
But I don't know how to remove the user_data/user_photos/ from before a file name, and if this is even the best way to go about this issue.
Summary:
profile pictures are found in user_data folder > profile_pics > file name.
user uploaded photos are found in user_data > user_photos > and then the whole path to the image followed by image name.
If you just want the filename from a fullpath then something like this will work
$file = "user_data/user_photos/ronda2.jpg";
$arrPathInfo = pathinfo($file);
echo $arrPathInfo['basename'];
I don't really understand properly the concept behind uploading multiple files with preview. I checked DropzoneJS and it looks nice and I feel I can do a lot of stuff with it.
I have this example: I have a user (id: 801) that creates a new post (id: TBD since the post is not yet created). I have a table image_post which holds the images of the specific post, and an image table which holds the image details. Now, I have the Dropzone form which is supposed to upload images to a folder that I specify in the .php file that is implemented to deal with the ajax request coming from Dropzone. Let's say the user uploads 2 images and I store them into an temp folder. The user submits the creating of the new post, it receives an ID: 10001. The temporary files are on the disk (but should they be stored in the DB as well?) but don't see how to link the post to the images.
What are the exact steps that are required to be able to (after the files were uploaded with Dropzone ajax request) link those images to the actual post id?
Your system will need to create a database record for the post prior to them uploading images. Consider the following table structure, where an i prefix indicates an INT column, s a VARCHAR/TEXT column, and dt a DATETIME.
Table posts: iPostId | iUserId | iStatus | sComments | dtCreated
Table photos: iPhotoId | iPostId | dtUploaded
When a user begins to start a new post, create a record in the posts table with their user id, an empty comments, a datetime such as '0000-00-00 00:00:00', and a status of 0, and then add a hidden form input to the photo upload form so that the assigned post id is known to your photo upload script, e.g.:
<input type="hidden" name="iPostId" value="22" />
The photo upload script could create a new record in the photos table using the post id and then save the photo in a particular folder using the post id in the filename, for example if the photo_id for the record created was 2821 you would save the file as 2821.jpg in your chosen folder.
When the user finishes their post and clicks to save/submit, you could use an UPDATE query to modify the original record on the posts table, assigning their entered text to the sComments column, the current date and time to the dtCreated column, and changing the iStatus to 1.
On your pages where you display a list of posts simply modify your query to something like this to exclude those currently in a draft status (0): SELECT * FROM posts WHERE status=1;, and then when viewing the post you can run a query such as SELECT * FROM photos WHERE post_id=22; to get an array of the photos to be displayed.
Obviously this can be expanded upon but hopefully gives you a good starting point.
I have a site that allows users to vote on images. Each image has a unique id number. For voting restriction purposes, I'd like to keep track of what each user has liked. How would I go about setting up a row in the USERS table that holds all the different id numbers of the images liked by this particular user?
You should not have all this information in your users table. I persume that you have one table with all your images and one with all your users. Create a table called "user_image_like" for example. And store "user_id" and "image_id". When the user presses like on the image your script will add "user_id" and "image_id" to the table.
When you wan't to see all images liked by a user you can easly get this by a simple sql query.
If you want to store such information in one table and in one field, I have the answer for you.
You can use either ways:
Store the IDs in a CSV format. When you fetch these values you can deal with them as you deal with a CSV file.
Store the IDs in an XML format. Then, you deal with it as any other XML file.
Regards,
This is a Many to Many relation. i.e. one user can like multiple Images and one image can be liked by multiple users.
According to normalization standards you should use these relation in separate table with user_id and image_id. whenever user like an image add a row to this table.
If you still want to store it in single table (users) then store ids in CSV format. be sure to store only unique image ids for each row.
Check this article
http://www.tonymarston.net/php-mysql/many-to-many.html
I'm building an app where each user can have multiple profiles. Users can upload multiple photos for each profile, etc.
The folder structure will be something like
public_html/
upload/
user-123/
profile-199/
profile-321/
images/
123423.png
I'm going to have a one to many relationship between each profile and each photo, but I'm trying to see how I should flag a photo as special and the users "profile photo".
My proposed table structure is something like this
id
profile_id
filename (or maybe extension only, since filename will most likely just be the primary id for that photo)
profile (boolean field of whether this a profile pic or not)
date_added
How should I treat special images, like profile pictures?
Edit: Sorry if I wasnt clear. I want each user to be able to have multiple profiles, each profile to have many photos. Just like for Facebook, you can have many photos, but only one profile pic at any time.
That would work but it would have a problem that more than one photo could be marked as a profile photo unless you take special care to prevent this.
An alternative design is to have a table with columns user_id and special_photo_id where the photo_id is a foreign key into the photos table. This has a slightly different issue: now it is possible to have a special photo that doesn't belong to the user.
You could have a field in your users table(where its username and other info is stored) with a profile_id field init pointing to the profiles table. That way you get one possibility for the profile and away to identify it.
Thinking about it that will only work if you do have a users table and a separated profiles table.
How about the image table looks like this:
id
profile_id
filename
date_added
and add the column 'profile_image_id' in the profile table? This will be your special image's id.
I imagine you are going to have a lot more non-special images than special. So, most of the time the field 'profile(boolean)' that you proposed will be 0.
Edit: I believe #Iznogood is saying the same thing.