I have posts system where users can upload images inside a post, I intend to build the images paths url and not store the uploaded images paths in database.
I will simply use unique Post ID to build the url path for post images like:
$path = "images/{$postID}/";
Then when rendering the post I will use
$images = glob($path);
When post is deleted I will simply:
unlink($path);
Why all implementations for php image uploads that I read about contains table to store images paths? Why not this method are there any disadvantages that I am not aware about?
I think that this one can go with a little discussion. Although the image location is recommended to be stored in the database, but it is not always the best way. If you according to your logic find some algorithm to uniquely name images based on blog id or something that would save you database latency cost.
For the same i would like to give example of a sample use case. You are either way fetching blog text from database. Now you can add another field to store the number of images that you have associated with the same. Now take the unique id of blog suppose uq1. If that blog has 6 images , you can name the images as
$path = $uq1.$imageindex
remember that you will also have to store the image in same path. You can always get fancy and add simple XOR encrypt. Although the use of XOR is mainly when you are also associating sensitive data to the same. This will save you some database latency.
Hope this helps
its a good idea doing it in this way.
unless you don't need duplicates of a lot of images
for example if you have image apple.jpg with size 500K,
and you have 2000 posts that should have apple image in them.
so you will save 2000 x 500k of apple image.
but if you have it in the database and you just select images from
something as gallery so you will have only 1 image for apple.jpg
but it linked for 2000 posts using the database table.
but if you dont care! about duplicates and data size, or you are sure that there is no any post have any image like the other! so its ok.
summary: saving all post images in folder with its id is the easy way
but it will cost you space ( duplicates and so on )
the other way, saving the image link in database and just link it with every post need it. its better.
Related
Thanks for jumping by!
I'm building a PHP script to manage employees, and I would like to get your help in deciding which good way do you use to implement multiple size for profile picture thumbnail.
The sizes may change (would like to keep it dynamic), thus, I can't set a specific column name for each thumbnail size.
I assume there are few options:
I can create a database table for each profile picture, there I will save the thumbnail path and "bind" to the user id with a foreign key, then I will be able to get all profile pictures and sort them based on my needs.
I can generate a thumbmail and save its path in the "users" table in a special column called "thumb_path" or something similar - in the same way I can store instead json of the profile picture paths (while the key represent the thumbnail size).
But both seems to be a bit wierd for me, and I can't clearly thing about another way to implement in efficiently.
Will be happy to get your kind help!
Marco.
I suggest to create one table for the thumbnails, with a reference to the user_id, the thumb path and the info about the size. You can have multiple rows for a user.
Hope this will help you.
I am developing a chat application for iOS.
In this app, the users can set up an image as their profile picture.
So my question is, how can i be able to store images in mysql ?
I have seen that many people say, just store the link to the image(on device) in mysql, but how will the images be available on different iOS Devices, from a database right ?
I have also tried using BLOB, but when the table rows are displayed(json encoded), the value for BLOB field comes out to be NULL.
Please answer in brief.
Thanks,
This is fairly simple :) The physical image will be stored on a server and you will store in your DB only the image name, or the relative path to the image, or however you want it. So, if you like, you will have to store in your DB a "pointer" to that image.
So:
- Image in folder on the server
- In DB -> path/to/file or file_name.format OR if you know you path, and you know your format just file_name
Hope this helps! :D
May be late But I just found way to do that.
Way 1: Get image from user and rename with the id (Pref. Primary key) and in Your public_html create folder for images. Set proper permissions for that folder. and save image in that folder and the url of that image will be stored DB column . Like
https://www.example.com/images/user1.png do in that style.
Way 2: use other things to rename that image like user's email,username etc
way 3: In above 2 cases it may happen that other users,hackers may try to download images by using IDs,emails etc.
That's why , another way you can do is you can generate a hash for profile and check if the hash already exists in column if exits then generate another one. Likely I don't think there is possibility of generating similar hashes.but you may check to avoid error in future. and now rename image using that generated hash.
You may also reduce size of that link column using only storing generated hash and in your app declare some variables and achieve
https://www.example.com/images/generated_hash.png
here the url will be same in all columns excluding that hash key.
you may only store that hashes.
Hope it will help
Reference from : https://www.techupdates.live/how-to-save-profile-picture-in-php-in-5-simple-steps/
I am reading a lot about uploading images with PHP. I have come to the conclusion that it's best to have a folder /images to keep the files. And have a db table that holds the path to the file and i'd also like to keep track of what it is an image of (in my case houses).
I would have 3 fields in the table:
id
unit
image
Does this sound like the proper way to handle this? I am unable to find any definitive article on how to do this.
Also, when it comes to uploads, are there any recommended articles on how to accomplish this in the manner I want?
Yes it is.
Although mysql lets you store images into records just as strings or numbers it is generally preferred to keep separated "records" from "files".
I'd suggest to avoid recording the path to the image.
Instead, as the image is associated with a record, give the image a name that links it to the record. Assuming your table have an unique, primary key, auto-increment, integer field named id each image will have filename id.jpg
After the upload you'll move the image from the php upload directory to the images directory renaming the file.
For information on how manage file uploads with php google for php upload files
Lets say I'm building a image gallery using PHP, where users would be able to upload their photos.
Every user would have 1 folder on server side with all their images there.
Now lets say I need to provide information in browser. Users would be able to browse images and should see lots of information about them, like image size, image dimensions, even EXIF information etc.
I could do this in 2 ways:
Save all information about image into database when uploading image.
Use PHP functions to browse through folder, and get information from every image.
I have something like file manager class, that can do all manipulations with files on server side, like deleteDir, deleteFile, countItems, getFileSize, getDirSize.
And it would be easy to only write one more class that would inspect images, and then I could just upload images, and get their information right from the folders without a need for relation database.
And now the question you all been waiting for is: ... :)
What would be faster, first or second solution? Lets say that site gets loads of traffic.
What solution would be better if I want it to be fast, and not to stress server to much?
actually, I got this situation like yours, this is my solution:
Save all information about image into database when uploading image.
Why?
I tested 2 ways:
Using php to get the image info for 1000 times.
Getting image info from database for 1000 times.
And the result is :
Getting image info from database is faster and faster.
Last but not least:
What would you do if you want to do a image info analystics?
If you save all info in database ,you can easily get them and analyse them ,but if you using php to get the info? it's hard to image.
So, just save all information about image into database when uploading image.
Good luck.
storing it in the database once
reading the data from the database and store it in cache,
redoing things always costs especially if it happens all the time
Depending on the size of these images, you probably want to show thumbnails instead of the original when people are browsing, which means you need to generate them. I would generate the thumbnail on upload and grab all the file info. Then save the file info in the database and put the original and thumbnail in the file system. If you get a lot of traffic, throw memcache on there too.
Storing data in separate places has a way of creating maintenance headache. I would just serialize the metadata for images in each folder and dump it to a file there. If you use gzip compression on the file, retrieval and storage should be very fast.
Currently i have system that's do resizing and creating as many images as needed and storing them on web servers file system. Also i make and entry in database of image basename in mysql.
For example i upload 5 images the basename would be 372FDSC and then it will add _0, _1 etc. to each image in filesystem so it makes image look like 372FDSC_0.jpeg and so forth.
So where is my problem? It's that i cannot delete or upload new images since i don't know image name in code.
I wonder is there is some better solution to save image names for article? Like mysql row that holds all names (372FDSC_0.jpgeg;372FDSC_01.jpeg etc.) So when i delete or upload new ones i know what filename i can use.
I hope this all makes some sense to anyone if not please say and i'll add required details.
You could use a timestamp at the end of the image name, so you're sure it's unique.
$new_name = $basename . "_" . time() . $extension;
You could use timestamp as stated above or you could also give your images descriptive names based on what the image contains instead of the 372FDSC naming.
Have 3 columns in your database table titled 'basename', 'no of images', 'article name'.
Now make a loop to get all the images.
eg if you have 5 images in your article 'abc'. Then you will use
for($i=0;$i<5;$i++)
$images[]=$basename.$imageno;
keeping in mind that $basename is the basename retrieved from database and same applies to $imageno.
You can use your article id field to rename images. For example, if your article id is 237, the images will be 237_1.jpg, 237_2.jpg etc.
In that case, you will not need to store image name in db also.