Добавление выгруженных файлов из каталога в элемент HTML <select>с использованием PHP - PullRequest
0 голосов
/ 27 ноября 2011

Я пытаюсь добавить загруженные файлы из каталога в элемент формы в HTML.Я получаю абсолютно ничего, когда я запускаю страницу (даже HTML).Вот код:

<html>
<head>

</head>

<body>

<form name="adgen" id="adgen" method="post">

Choose a Banner Image:&nbsp;&nbsp;<select name="banimg">

<?php

//This variable specifies the file path for the banner image
$filepath="imgdir";

//This variable opens the directory for the correct file path for this select option
$openimg=opendir($filepath);

//This loops through the directory of files and if it finds a file it displays it as an option in the select form element
while($imgsel=readdir($openimg)){

    if($imgsel !=".." && $imgsel !="."){

        echo '<option value="'.$imgsel.'">'.$imgsel.'</option><br />');
        }

    }

closedir($openimg);
?>

</select>

</form>

</body>
</html>

Дайте мне знать, если вы видите, что вешает трубку.

Ответы [ 3 ]

2 голосов
/ 28 ноября 2011

Попробуйте что-то вроде:

<?php
$dirContent = scandir('imgdir'); // don't forget to set full path to your folder
unset($dirContent[0]);
unset($dirContent[1]);
?>
<form name="adgen" id="adgen" method="post">
    Choose a Banner Image:
    <select name="banimg">
        <?php foreach ($dirContent as $image): ?>
            <option value="<?php echo $image?>"><?php echo $image ?></option>
        <?php endforeach; ?>
    </select>
</form>
1 голос
/ 28 ноября 2011

2 Советы:

  1. Используйте хорошую IDE: http://netbeans.org/ или http://www.eclipse.org/. Оба хороши, оба показали бы синтаксическую ошибку.

  2. Включайте ошибки при разработке, отключайте их, когда сайт работает.Поместите это в верхней части страницы: error_reporting(E_ALL);

Проблема в вашем коде была простой (

<html>
<head>

</head>
<?php error_reporting(E_ALL); ?>
<body>

<form name="adgen" id="adgen" method="post">

Choose a Banner Image:&nbsp;&nbsp;<select name="banimg">

<?php

//This variable specifies the file path for the banner image
$filepath="imgdir";

//This variable opens the directory for the correct file path for this select option
$openimg=opendir($filepath);

//This loops through the directory of files and if it finds a file it displays it as an option in the select form element
while($imgsel=readdir($openimg)){
    if($imgsel !=".." && $imgsel !="."){
        echo '<option value="'.$imgsel.'">'.$imgsel.'</option><br />';
    }
}

closedir($openimg);
?>

</select>
1 голос
/ 28 ноября 2011

У вас есть синтаксическая ошибка здесь:

echo '<option value="'.$imgsel.'">'.$imgsel.'</option><br />');

Обратите внимание на дополнительную закрытую скобку в конце строки. В любом случае, хороший совет - использовать error_reporting для отладки и посмотреть, в чем проблема.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...