PHP 中带有“新建文件夹”选项的下拉列表

发布于 2024-12-04 18:24:25 字数 1005 浏览 0 评论 0原文

我有这个脚本,可以获取所有子目录并将它们放入下拉列表中。 我有一个选项“新建文件夹”,可以通过收集文件夹名称来创建一个新文件夹 - 在本例中,它是像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尝蛊 2024-12-11 18:24:26

我有一些提示可以帮助您改进代码。我不完全理解你的问题,但这可能会有所帮助。

如果您想列出一个目录及其所有子目录,您应该这样做,

function list_directory( $dirname ) {
    foreach file in directory      
        if(is_dir($dirname)){
            list_directory($dirname)
        }
        if(is_File($dirname)){
            echo $dirname;
        }
    }
}

这不是可用的代码,而是供您使用的一般想法。

如果奇怪的事情开始发生尝试这样做

var_dump( $var ); // The variable your are suspecting in your case $newbase

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

function list_directory( $dirname ) {
    foreach file in directory      
        if(is_dir($dirname)){
            list_directory($dirname)
        }
        if(is_File($dirname)){
            echo $dirname;
        }
    }
}

This is not usable code but a genral idea for you to work with.

If strange things start happening try doing this

var_dump( $var ); // The variable your are suspecting in your case $newbase
浅沫记忆 2024-12-11 18:24:26

您需要将 if($_POST['folderchoose']==0) 更改为其他内容,因为 0 表示“null”,因此 if 语句为 true 并且它将被执行。

或者...使用 isset() 确保表单确实被提交,例如

if (isset($_POST['folderchoose'])) {

if ($_POST['folderchoose'] == 0) {

//do mkdir or something

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

if (isset($_POST['folderchoose'])) {

if ($_POST['folderchoose'] == 0) {

//do mkdir or something
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文