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.
Related
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.
I am using php and mysql. If I want to do a sharing photos website, whats the best database design for upload and display photos. This is what I have in mind:
domain:
|_> photos
|_> user
Logged in user will upload photo in
[http://www.domain.com/user/upload.php]
The photos are stored in filesystems, and the path-to-photos stored in database. So, in my photos folder would be like: photos/userA/subfolders+photos, photos/userB/subfolders+photos, photos/userC/subfolders+photos etc
Public/others people may view his photo in:
[http://www.domain.com/photos/user/?photoid=123]
where 123 is the photoid, from there, I will query from database to fetch the path and display the image.
My questions:
Whats the best database design for photo-sharing website (like flickr)?
Will there be any problems if I keep creating new folder in "photos" folder. What if hundreds of thousands users registered? Whats the best practices
What size of photos should I keep? Currently I only stored thumbnail (100x100) and (max) 1600x1200 px photos.
What others things I should take note when developing photos-sharing website?
1 - At a minimum your database should consist of
users: user_id, user_name, pw_hash, salt
photos: photo_id, user_id, photo_path
fields would be added for extra functionality you wanted (tags, last_seen, photo_upload_date, photo_views, etc...)
2 - Dont prematurely optimize. i would go with adding a users/username folder for each user and dumping the users photos in there.
3 - i recommend keeping the original size, thumbnail, and a size that is suited for your site. so if your layout holds a photo with max width of 850px resize all photos to a max 850px wide. If youre layout changes you can use the original picture and resize them to the new size.
4 - there is an excellent answer on SO somewhere about image upload security but i cant find it. ill keep looking.
think about using a CDN: https://stackoverflow.com/questions/72369/whats-the-best-cdn-for-image-hosting-on-a-high-volume-web-site
good talk on scalabilty, the section on thumbnails would be good for you to watch: http://www.youtube.com/watch?v=ZW5_eEKEC28. this video talks about a file-per-directory limit in ext3 filesystems you may be interested in.
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.
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.
I have a form that searches a mysql db for whatever the user is after, and returns the results on the same page in a div (using ajax then php to search mysql)...
The results are ads, as cars for example, and I want each ad to have a unique image associated with it so that it displays the images next to the ads...
Storing images in BLOBS arent really a good solution I have heard...
How would I do this the best way?
Thanx
A fast way will be to store images in a folder giving unique filenames in folder or separate folders if you want to put images of different categories in separate places. After searching the available ads, read associated unique file-names and server to client.
Suppose you save images related to car ads in folder /images/cars/. User searches for Audi S6 and the result returns 5 ads.
Now we will proceed based on the naming of the image files. If you give file related to each ad unique name and put that name into record for that ad then simply get that name and create image URL as follows:
/images/cars/ + result_row('image_name')
If you are naming images based on id of ad record then use this scheme:
/images/cars/ + result_row('id')
If you mean that at first you were sending image bytes in response, then you don't need to do that. simply send the path constructed and use it in src property of img tag.
PS:- My PHP skill are not very good now!
Typically, you would want to store image records in the database as details about the image and then a file path to the actual image. Then, store the images in the regular file system.
As a side best-practice, you typically will want to store the path relative to some common root, then you can append the file root so the store of images can be moved around.
ie> store ads/cars/1005.jpg and then append a root of 'C:/myApp/images/'
Each unique ad will have a unique identifier in the database. You could create image subdirectories for a given set of images and save the images based on their unique identifier.
i.e. advertisement 506 could be stored in /img/506.jpg
Ultimately you would query the database for the advertisement and then you would assume you are loading the image with filename identifier + '.jpg'