Storing pictures per user on website (php) - php

Users of the website need to able to store images in their "area" , should I store these in the database directly or create a directory for each user.
Or should I just have a single directory for all images and in the database store a list of images that each user owns?
I'm looking for the most effecient way, which I think is a folder for each user?

If you have many pictures per user like avatar and gallery of photo created by the user then use separate folders named with the user id or a hash of the user id.
If you only store avatars have one big folder where the name of the image is the user id or hash of the user id.

It depends on how many pictures there are.
If there is only one picture per user, then perhaps all in the same directory.
However if you have albums and such, there can be millions of photos with all the users photos in the same folder. This would be very slow to search in. Then I'd go with one folder per user.
Also, never give direct access to the folders. Use your database to link to the files.

Better use folder or a cache folder. You can also use resizing functions for the image, so if user upload a large scale picture. Your script will manage to create different sizes, like avatar pics. And cache those images. So thumbnail or avatar viewers only viewing the cache image of the original image. And the large image is usable for future work, like a bigger preview of the avatar.

Related

Store images or folder path in database: how?

My plan is to write a few news articles each day to display on my website.
Each news article will contain a certain amount of pictures. There can be up to 50 images in an article.
At this moment my images get stored like this:
/images/2014/theme1/01.jpg
/images/2014/theme1/02.jpg
/images/2014/theme2/01.jpg
/images/2014/theme2/02.jpg
...
/images/2014/theme2/50.jpg
I'd like to know how I can store these images into my database. I already read a lot about this subject and heard that storing the folder path into the database is better than the images itself.
But how should I do this? The maximum amount of pictures per article is 50, so do I really have to create 50 columns (one for each possible image) or is there another way to do is?
#Luca Prodan Flow this Link. .Net core Add folder in static folder (www) and save image file and store the: "PathWithFolderName + "/ImageName.png" in database.

Access folder and select filename using PHP

I have written a code, where the user selects a profile picture and then the picture is stored in localhost/user/$username/photos/photo1.gif.
After that, I assigned the filename (photo1.gif) into a session variable so I can display it from all my php scripts. This is working just fine. I can display the picture in every php script by accessing this session variable.
The only problem I have is when I am trying to login from the login page: In the login page I connect to the database, retrieve email and password, check them and if they are OK I redirect the user to home.php. The problem is that the user's photo is not linked to the email so i cannot know the filename of the photo. The only thing I know for sure is the directory (because I can retrieve username from database as well).
Lets say that a user has uploaded 4 photos (photo1, photo2, photo3, photo4 - photo4 was uploaded last). It makes sense that he is currently using photo4 as my profile picture.
Is there a way for me to access that folder and retrieve the filename of the picture uploaded last?
Also, as a general question, what is better, store the photos(or files) in a database or server?
A few options:
It would be 'better' to create a photo table and store the user_id and the photo location in that table. Storing the actual photo in the table as a blob is not generally recommended.
Alternatively, to avoid more tables, you could rename the photos as
username_photo1.jpg
username_photo2.jpg
username_photo3.jpg
And then you can retrieve the largest of them.
Finally, another option is to get the file creation date of the photos in the directory and take the most recent photo.
see Getting the filenames of all files in a folder

How to set directory for images in my website?

My aim is to make a multi-user website which may have between 30 to 3000 users. once one uploads an avatar or an image, How should it handle directory naming?
1- all files in one folder?
2- all files in folders separated by day?
/img/upload/2013/05/14/brothers32_5464562.jpg
3- all files in folders separated by user name?
/img/upload/users/brothers32/5464562.jpg
I don't like change my way in mid-way.so, according to your experience which way do you suggest to choose?
I agreed with StarCub until I read each user could upload multiple images. Then I'd do this:
Profile picture. All in 1 folder (as StarCub suggested):
/img/upload/users/USERID.jpg
Uploaded pictures. All in another folder:
/img/upload/userimg/2013/PICID.jpg
EXCEPT if you expect users to upload many pictures. I'd say 1000-5000 pictures per folder is good, so maybe order them by month depending on what kind of site the page is.
If you only have to up 3000 users and each user only has one avatar, I would just put them all in one folder (OPTION 1) and name them use user ID or some sort of unique identifier for that user.
If each user can upload multiple images, then I would use OPTION 3.

imagemagick for thumbnail

I would like user to upload most of their photo albums online, would it feasible to create and save one in thumbnails folder and one in full size folder or generate thumbnails on the fly when the user access their album? It will be something like eBay does. Webspace is not a concern since I have an unlimited plan.
Store the thumbnails when they're uploaded, or better yet, create a thumbnail (and store it) when the image is first accessed.
Storing them means that the server won't have to do unnecessary processing on every page load.

How to store multile images full path into a database so that I can display them in a user home page like Orkut

I am making an application in which every user has to sign in first and then he can access his home page. Now on the home page, I have given an option of uploading an image. Now if the user is uploading one image I am storing the full path of the uploaded image into a database and from there I can display the image easily by an img tag...
But what should I do when the user want to upload many images? Then how should I store their full paths in a database for the same user. Give me an idea just like Orkut or Facebook. Should I make a different table with named images and should I store images in different rows with the same username. What should I do?
I don't know the logic. What should I do? How can I upload many images and how can I store their path and what will be the wisest method and how do I display many images on one page (I can display one)?
You can make a folder, named after user name and id and put all their images there.
To display many images on one page, just add more <img> tags to the page.
This seems to be more of a design question than a PHP question. I would create a separate table to store all paths, this is more normalized.
You still need to handle the UI, but if you are doing a sort of gallery then that is fairly simple with some jQuery sideshows or something like that.
Yes, you want to use a separate table to store the image paths. You'll most likely want a record ID, the User ID, and the path to the image. You could also add a field to contain the sorting order for the images.
Having the sorting order field will allow you to page through the photos if there is more than one page of photos.
Your thinking is correct where you suggest creating a separate table with rows containing the image path and the username. The concept that you are dealing with is called cardinality. I'd recommend that you take a few minutes to read about this concept, since it is so important to database design.
In this case, you're talking about a one-to-many relationship between the user and the images.

Categories