If I want each user in my web app to be able to upload about 6 pictures with a description, what is the best way to store that info in the db?
How would you do it?
i would save the image files to a location in the file system.
then, keep a database table for images that contains id, filepath, description.
you may also want to keep other info in the image table as well, for instance, which user the image belongs to, and which is the main image, width, height, etc.
Related
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.
i just wanna ask on how to make a simple gallery for my website wherein the images used to display are from database and will automatically display on the gallery? so that when i have to update the images i won't enormously create thumbnails..??
I would recommend against keeping the image data in a database, it's almost always a bad idea.
I would recommend storing metadata about the images in the database and then including a pointer to the file (the image's path on the local filesystem). If you need to make thumbnails create them as the images are added, store them on the filesystem too and store the path to them in the table too.
Another approach is to have the filename of the image and thumbnail as a function of the image ID. E.g. store it on the server at /some/directory/images/123.jpg and /some/directory/images/123_thumbnail.jpg where 123 is the id of the image.
use mysql BLOB storage - Example: http://www.anyexample.com/programming/php/php_mysql_example__image_gallery_%28blob_storage%29.xml
You can create the thumbnails and update the database when the images are uploaded to a directory within your http path. A simple php routine can extract the picture names from the db and include them in your gallery.
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 am trying to upload an image to a directory on a server. i'm using the tutorial found at http://www.reconn.us/content/view/30/51/.
First, is that a good method for uploading images using PHP?
Second, I'm also going to store the info in a MySQL database. What is a good way to deal with images that have the same name that the user uploads? For example, if a user uploads a file 'test.png' 2x in a row, what should happen to the second filename? From the script above, both will get a unique filename, but how would I as the user access that image again? I couldn't just query because the only name I know was the duplicate name I gave it, and I definitely don't know the unique name the server gave it using the upload time...
Third, what is a good max file size for images?
You can report the unique URL back to the user after the upload so that the user will know where to find the image. So, the first test.png could be http://www.example.com/images/fjdklagjsdl.jpg and the second could be http://www.example.com/images/jklfsdlkj.jpg
You can also provide some kind of interface for users to view images they've uploaded. If you display a thumbnail of the uploaded image next to the image's unique filename, it will be easy for the user to identify which image is which.
This is the method I use:
Users upload images
Server saves the image with a unique (GUID or something) filename and stores - both - the unique generated filename and the original uploaded filename in a database
Images are linked to using either the original_filename, unique_filename or primary_key for the images table.
The images are taken from the server, and served using the original filename stored in the database. This way you avoid chances of conflicting filenames and you preserve the image's original filename. In addition, this allows you to build a search on the original_filename column for the user to use.
With this method, unique filenames never have to be exposed to the user, instead they're used to locate the image associated with a specific id or original_filename in the 'images` table.
Of course, if you don't care about giving the original filename to the image when it's displayed, you can just generate a unique filename whenever you want to store it.
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'