Matteo Sala :
I have a php code that has a dropdown menu filled with the names of images files in a specific folder, what i want is once the user select one of the names the selected image is displayed, at the moment i'm trying to use a submit button. i am trying to not use jquery or other languages
<?php
$folder = 'C:\Users\source\\';
echo '<form action="" method="post">'."\n".'<select name="image">'."\n".
dropdown(image_filenames($folder), @$_POST['image']).
'</select>'."\n".'<input type="submit" name="submit">'."\n".'
</form>';
function image_filenames($dir)
{
$handle = @opendir($dir)
or die("I cannot open the directory '<b>$dir</b>' for reading.");
$images = array();
while (false !== ($file = readdir($handle)))
{
if (preg_match('/^.*\.(jpg|jpeg|png|gif|svg)$/', $file))
{
$images[] = $file;
}
}
closedir($handle);
return $images;
}
function dropdown($options_array, $selected = null)
{
$return = null;
foreach($options_array as $option)
{
$return .= '<option value="'.$option.'"'.
(($option == $selected) ? ' selected="selected"' : null ).
'>'.$option.'</option>'."\n";
}
return $return;
}
if (isset($_POST['submit'])) {
echo '<img src=C:\Users\source'. $row['images'] . ' />';
}
?>
Antony Jack :
You must need to keep your file inside your project ...
change
$folder = 'C:\Users\source\\';
to
$folder = 'images\\';
images is the folder name which inside your project...
xampp or wampp not consider the local path files...
And it won't display the images as well...
Hope this Helpful...
I have changed two places in your code...
<?php
$folder = 'images\\'; // changed here
echo '<form action="" method="post">'."\n".'<select name="image">'."\n".
dropdown(image_filenames($folder), @$_POST['image']).
'</select>'."\n".'<input type="submit" name="submit">'."\n".'
</form>';
function image_filenames($dir)
{
$handle = @opendir($dir)
or die("I cannot open the directory '<b>$dir</b>' for reading.");
$images = array();
while (false !== ($file = readdir($handle)))
{
if (preg_match('/^.*\.(jpg|jpeg|png|gif|svg)$/', $file))
{
$images[] = $file;
}
}
closedir($handle);
return $images;
}
function dropdown($options_array, $selected = null)
{
$return = null;
foreach($options_array as $option)
{
$return .= '<option value="'.$option.'"'.
(($option == $selected) ? ' selected="selected"' : null ).
'>'.$option.'</option>'."\n";
}
return $return;
}
if (isset($_POST['submit'])) {
echo '<img src=images\\'. $_POST['image'] . ' />'; // changed here
}
?>
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=342287&siteId=1