How to save profile picture in PHP in 5 simple steps

How to save profile picture in PHP in 5 simple steps

How to save profile picture in php ? or How to save profile picture of User in Your application? or how to save images in Database?

This is what you are looking for ,you might be looking specifically for android,PHP,web app,iOS,Windows or whichever app it doesn’t matter. This article will help you in achieving that.

Requirements

As you want to add profile picture in Profile system . It is required that you should have server. You can save the profile of user locally but it doesn’t make sense. Profile picture should be visible to everyone.

That’s why you need your server from where you can serve your users.

If you want to read more about servers, Hosting. Read here. You should also have domain although it is not required, but using domain is best practice. We will cover why? in this article.

Setting up

Setting up is not very difficult. You will just need to updates your domain name servers to the name servers provided by your hosting provider.

How to

I will guide you step by step from here.

You must be having your database at your server. Now alter your table and add one column as pro_link or name you want. This is column where we’re going to save image attri.

ALTER TABLE table_name
ADD column_name datatype;

Now you have just added column. Next is we will create a script in PHP . Which will be stored on your server. and we will call it using domain name.

Although having PHP script is not essential, You can use directly your server IP address. But problem here is that in case you upgrade your hosting or moved to another or hosted self server then . Your previous configurations are useless. This is why we required a domain.

I will suggest that for storing your Scripts you should create another subdomain . Which will make sense. Like

https://api.justinclicks.com/AppName/files

I will suggest you that you should install SSL certificate. You can install it free from Let’s Encrypt. See guide for installing SSL here.

Final

Now final , I hope that you are developing that kind of complex app. You must be added primary key in your user table. IF yes then Good. else Go and add primary key.

ALTER TABLE Persons
ADD PRIMARY KEY (ID);

Now you have 3 ways to do that


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://api.justinclicks.com/images/user1.png 

do in that style.

Way 2:

use other things to rename that image like user’s email,username etc.

https://api.justinclicks.com/images/username.png

https://api.justinclicks.com/images/email.png

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://api.justinclicks.com/images/generated_hash.png

here the url will be same in all columns excluding that hash key. you may only store that hashes.

To achieve way 3 you can generate hash. There are multiple ways to genrate hashes.

Hashing

MD5

To generate MD5 hash from email

$hash=MD5($email);

password hash

<?php
/*Here techupdates.live is your password which you want to encrypt*/
echo password_hash("TechUpdates.live", PASSWORD_DEFAULT);

?>

Read more about hashing the string here

Leave a Reply