php session tutorial with dynamic image

Hello, Friends! In the previous tutorial, we have seen all about the php session and successfully built the login and registration portal using php and MySQL database. Now, we’re going to make the image change according to the user.i.e. dynamic image.
Note: Please make sure that you’ve gone through the previous session tutorial in order to understand this tutorial. If not, then please find it here.

php session tutorial

We are using the same files as in previous session tutorial. so I will not mention all of them here. We have to modify the file home.php to support the dynamic showing of the image. Just add one field ‘picpath’ in the database having varchar type and value as ’50’. Now, open the previously coded file home.php and add the below code in it.

We are here using a session variables to manage the image upload, retrieval and display it. We here put variables in image tag and the as the session variable value changes, the image path also changes. So in this way, the value updates and makes different images appear in the place.

You can see the modified content shown by a comment in the above code. We have included the variable containing the value of session in the image ‘img’ tag so that the value will be updated by fetching it in database and it prints the new value containing the path of new image. This results into changing the image dynamically for each user.

Dynamic image

This code will check if the file exceeds the certain limit 5 Mb, then again checks if the file is an image or not, again checks the file is already exists and if all these conditions are false, then the file is successfully uploaded on the server in our specified folder.

To achieve this, we will create a folder to store the uploaded images and also we have to create one additional field in the database to save the image path.

So, create folder ‘images‘ in the same root directory and create the additional field in database structure named ‘picpath‘.  For better understanding, do not remove the comments form the code above.

That’s it! The whole tutorial will work now with the dynamic image of a user in which user can upload and update his profile image. See the screenshot below.
php full session tutorial

As usual, the source code is always freely available for download from below link.

Download Source Code

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.