简体   繁体   中英

How to store user's profile picture in mysql

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM