Jump to content

Retrieving images from a folder using php


liomon41

Recommended Posts

I have a form that has a dropdown menu and a button and the list values for the dropmenu are five countries (france, england, germany, spain, norway) and i also i have in a folder in my local server the flags for these five countries (france.jpg, england.jpg, germany.jpg, spain.jpg, norway.jpg) . The idea is for everytime a user select a country from the dropdown menu and clicks the button, the countries flag is displayed from the folder unto the page right next to the button... Any ideas how to go about this... really appreciate you guys helping out....

Link to comment
Share on other sites

A picture is worth a thousand words. Code to take a submitted country name (france, england, germany, spain, norway) and display the corresponding .jpg flag image -

 

<?php
$folder = 'flags'; // define folder with .jpg flag images
$flags = glob("$folder/*.jpg"); // get a list of the flags (for building the option menu and validating the submitted value)

$selected_flag = isset($_POST['country']) ? $folder.'/'.strtolower($_POST['country']).'.jpg' : ''; // build selected flag from the submitted country name
$selected_flag = in_array($selected_flag,$flags) ? $selected_flag : ''; // test if submitted flag is valid (insure that only expected image files in the correct folder can be selected.)

echo "<img src='$selected_flag' alt=''>"; // display selected flag

 

If there's anything you don't understand after reading the code and comments and trying the code, just ask.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.