PHP 中带有“新建文件夹”选项的下拉列表
我有这个脚本,可以获取所有子目录并将它们放入下拉列表中。 我有一个选项“新建文件夹”,可以通过收集文件夹名称来创建一个新文件夹 - 在本例中,它是像 995 这样的数字 - 并将其减一以创建一个名为 994 的目录 有
<form action="index.php" method="post">
<select name="folderchoose" id="folderchoose" onchange="this.form.submit();">
<?php
$base = basename("$items[1]", ".php").PHP_EOL;
$newbase = $base -1;
if($_POST['folderchoose']==0){ mkdir("../albums/$newbase", 0700); }
$items = glob("../albums/*", GLOB_ONLYDIR);
natsort($items);
{?><option>select:</option><?
foreach($items as $item)
{
?> <option value="1"><? echo "$item\n "; ?></option><?
} ?> <option value="0" >New folder</option> <?
}
?>
</select>
</form>
Directory:<?php echo $_POST[folderchoose] ?><br />
<?php $base = basename("$items[1]", ".php").PHP_EOL;
$newbase = $base -1;
echo $newbase ?>
两件事无法正常工作: mkdir 函数即使我没有选择“新文件夹”,也不会自动获取 $newbase 并创建一个名为 dir(??) 的目录。
I have this script that gets all the sub-directories and puts them into a dropdown list.
I have an option "New folder" to create a new folder by gathering the folder name - in this case, it's numbers like 995 - and discount it by one to create a dir called 994
<form action="index.php" method="post">
<select name="folderchoose" id="folderchoose" onchange="this.form.submit();">
<?php
$base = basename("$items[1]", ".php").PHP_EOL;
$newbase = $base -1;
if($_POST['folderchoose']==0){ mkdir("../albums/$newbase", 0700); }
$items = glob("../albums/*", GLOB_ONLYDIR);
natsort($items);
{?><option>select:</option><?
foreach($items as $item)
{
?> <option value="1"><? echo "$item\n "; ?></option><?
} ?> <option value="0" >New folder</option> <?
}
?>
</select>
</form>
Directory:<?php echo $_POST[folderchoose] ?><br />
<?php $base = basename("$items[1]", ".php").PHP_EOL;
$newbase = $base -1;
echo $newbase ?>
Two things aren't working properly: the mkdir function doesn't get the $newbase and create a directory called dir(??) automatically even if I'm not choosing 'New Folder'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一些提示可以帮助您改进代码。我不完全理解你的问题,但这可能会有所帮助。
如果您想列出一个目录及其所有子目录,您应该这样做,
这不是可用的代码,而是供您使用的一般想法。
如果奇怪的事情开始发生尝试这样做
I have a couple of hints for you to improve your code. I do not fully understand your problem but this might help.
If you want to list a directory with all of its sub directories you should do something like this
This is not usable code but a genral idea for you to work with.
If strange things start happening try doing this
您需要将 if($_POST['folderchoose']==0) 更改为其他内容,因为 0 表示“null”,因此 if 语句为 true 并且它将被执行。
或者...使用 isset() 确保表单确实被提交,例如
You need to change if($_POST['folderchoose']==0) to something else because 0 mean "null" and therefore the if statement is true and it will be executed.
or... Use isset() to make sure the form really get submitted, for example