Fri, 17 May 2024
NetArt Media     Pay 2, Get 3!    


PHP Random Image Script

By: Peter, NetArt Media
Wed, 1 August 2018
PHP Random Image Script

Who couldn't use a random image script? Most people use one for displaying images in headers or sidebars that usually lead to a sponsored product, but sometimes they can also be used to show random pictures that represent a hobby or other interests. Whatever the reason, this PHP script could come in handy for anyone looking to display images in a random context.

First Step - Create A MySQL Database Table



The first step is to create a MySQL database table. I called my table 'random_pics' and assigned 4 columns to it.

1. id | 11 characters | PRIMARY KEY | AUTO INCREMENT

2. sponsor | VARCHAR - 100

3. url | LONGTEXT

4. pic | VARCHAR - 100

Second Step - HTML Form & Processing File



1. Create an HTML form with our variable names in it. These of course would be 'sponsor', 'url' and 'pic'. Remember to include 'enctype="multipart/form-data"' in the form or the image won't upload during processing.


2. Code up the processing file that will upload our image and insert the appropriate values into the MySQL table.

After calling your database connection file, check that the form has been filled out properly. This includes making sure an image has been uploaded. This script was created on a development server in a makeshift secure administration area, but in an actual live environment you would want to check for file extensions for better security. I will cover that in another article in the future.

A Prepared INSERT Statement takes care of the insertion for us after the upload has taken place. We then define a success message to display in the event the processing has completed within the Conditional Statement. In the event the form does not process we get redirected back to the form page and nothing gets processed.

The series of images below show the form before submission and then the processing page with the success message displayed. The third image is of the MySQL database table after the data has been inserted. I created two entries for this tutorial, using products from Amazon.




Third Step - The Link Jump Script File


Those who don't plan on using this script to market sponsored products can skip this next step if they desire.

For those who may use this script for marketing sponsored content, this next step is something you should consider in order to hide those long sponsor links we sometimes see. You could also embellish on the script below by updating each click with an incremented hit that you can store in MySQL. I cover that in this article.


Fourth Step - Displaying The Random Images



Above is the code block used to display the random images. Either place this directly within the HTML on your webpage where you want, or place it into another file and call it by using include or require.

In the event you want to show two or more images at a time, simply change the number after 'LIMIT' to whatever number you desire. You will need a number of rows stored in your MySQL table for that to work though, remember.

I hope this random image script gives you some ideas on creating one for yourself. Remember to do a search on checking for file extensions while processing something like this though before you use it on a live environment. While this should be used within a secure administration area, it's always better to be safe than sorry.


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox