Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在目录中自动显示文件名并将其自动填充为值_Php_Arrays_Filenames_Fill - Fatal编程技术网

Php 在目录中自动显示文件名并将其自动填充为值

Php 在目录中自动显示文件名并将其自动填充为值,php,arrays,filenames,fill,Php,Arrays,Filenames,Fill,我想要这样的东西(最终版): <?php //named as config.php $fn[0]["long"] = "file name"; $fn[0]["short"] = "file-name.txt"; $fn[1]["long"] = "file name 1"; $fn[1]["short"] = "file-name_1.txt"; ?> 和上面的config.php需要另一个文件,例如 <? //named as form.php include

我想要这样的东西(最终版):

<?php
//named as config.php
$fn[0]["long"] = "file name";   $fn[0]["short"] = "file-name.txt";  
$fn[1]["long"] = "file name 1"; $fn[1]["short"] = "file-name_1.txt";
?>
和上面的config.php需要另一个文件,例如

<? //named as form.php
include "config.php";
for($tint = 0;isset($text_index[$tint]);$tint++)
{
if($allok === TRUE && $tint === $index) echo("<option VALUE=\"" . $text_index[$tint]["short"] . "\" SELECTED>" . $text_index[$tint]["long"] . "</option>\n");
else echo("<option VALUE=\"" . $text_index[$tint]["short"] . "\">" . $text_index[$tint]["long"] . "</option>\n");
} ?>

因此,我尝试搜索并放置php代码,希望它能够处理所有问题: e、 g


将此用作if条件,以避免结果中出现“..”

if($file != "." && $file != "..")

将此用作if条件,以避免结果中出现“..”

if($file != "." && $file != "..")
改变

你会得到你想要的行为

如果您从linux环境中读取所有文件,您总是可以得到。和。。作为文件,表示当前目录(.)和父目录(..)。在您的代码中,您只忽略“.”,而您也希望忽略“…”

编辑: 如果要打印您编写的内容,请将内部循环中的代码更改为:

if($file != "." ) {
  echo "\$fn[\$i]['long'] = '$file'<br />"; // Test
    $i++;
}
(您可以删除$i,因为php会自动递增数组)。确保在while循环之前初始化$fn: $fn=array()

变化

if($file != "." ) {

你会得到你想要的行为

如果您从linux环境中读取所有文件,您总是可以得到。和。。作为文件,表示当前目录(.)和父目录(..)。在您的代码中,您只忽略“.”,而您也希望忽略“…”

编辑: 如果要打印您编写的内容,请将内部循环中的代码更改为:

if($file != "." ) {
  echo "\$fn[\$i]['long'] = '$file'<br />"; // Test
    $i++;
}
(您可以删除$i,因为php会自动递增数组)。确保在while循环之前初始化$fn:
$fn=array()

我也经历过同样的事情。我刚刚使用了array_shift()来修剪数组的顶部
查看文档

我也经历过同样的事情。我刚刚使用了array_shift()来修剪数组的顶部
查看文档

查看以下功能:

  • -查找与模式匹配的路径名
  • -列出指定路径内的文件和目录
  • -提供一个简单的界面,用于查看文件系统目录的内容
因此,使用DirectoryIterator,您只需执行以下操作:

$dir = new DirectoryIterator('.');
foreach($dir as $item) {
    if($item->isFile()) {
        echo $file;
    }
} 
请注意,$dir中的每个$item都是一个实例,并提供对许多有用的其他函数的访问,例如

进行递归目录遍历同样容易。只需将a与a一起使用,然后执行以下操作:


注意恐怕我不明白你问题中的以下几行是什么意思:

echo "$fn[$i]['long'] = '$file'<br />"; // Test
echo“$fn[$i]['long']='$file'
”;//试验

但是,通过上面给出的函数和示例代码,您应该能够对目录中的文件执行任何您想执行的操作。

请查看以下函数:

  • -查找与模式匹配的路径名
  • -列出指定路径内的文件和目录
  • -提供一个简单的界面,用于查看文件系统目录的内容
因此,使用DirectoryIterator,您只需执行以下操作:

$dir = new DirectoryIterator('.');
foreach($dir as $item) {
    if($item->isFile()) {
        echo $file;
    }
} 
请注意,$dir中的每个$item都是一个实例,并提供对许多有用的其他函数的访问,例如

进行递归目录遍历同样容易。只需将a与a一起使用,然后执行以下操作:


注意恐怕我不明白你问题中的以下几行是什么意思:

echo "$fn[$i]['long'] = '$file'<br />"; // Test
echo“$fn[$i]['long']='$file'
”;//试验
但是有了上面给出的函数和示例代码,您应该能够对目录中的文件做任何您想做的事情。

OP编辑了他的问题后的新答案

从您编辑的问题中,我了解到您希望在HTML网页上的
SelectBox
元素中动态填充
选项值的特定目录中的文件。这些值应按破折号、下划线和数字分割,以提供
选项名称
,例如

Directory with Files    >    SelectBox Options
filename1.txt           >    value: filename1.txt, text: Filename 1
file_name2.txt          >    value: filename1.txt, text: File Name 2
file-name3.txt          >    value: filename1.txt, text: File Name 3
根据我在另一个答案中给出的代码,您可以使用
DirectoryIterator
实现这一点,如下所示:

$config = array();
$dir    = new DirectoryIterator('.');
foreach($dir as $item) {
    if($item->isFile()) {
        $fileName = $item->getFilename();
        // turn dashes and underscores to spaces
        $longFileName = str_replace(array('-', '_'), ' ', $fileName);
        // prefix numbers with space
        $longFileName = preg_replace('/(\d+)/', ' $1', $fileName);
        // add to array
        $config[] = array('short' => $filename,
                          'long'  => $longFilename);
    }
} 
foreach($config as $short => $long)
{
    printf( '<option value="%s">%s</option>' , $short, $long);
}
但是,由于目录中的文件名是唯一的,因此也可以将其用作数组:

$config[$filename] => $longFilename;
在构建配置数组时。短文件名将构成数组的键,然后您可以按如下方式构建选择框:

$config = array();
$dir    = new DirectoryIterator('.');
foreach($dir as $item) {
    if($item->isFile()) {
        $fileName = $item->getFilename();
        // turn dashes and underscores to spaces
        $longFileName = str_replace(array('-', '_'), ' ', $fileName);
        // prefix numbers with space
        $longFileName = preg_replace('/(\d+)/', ' $1', $fileName);
        // add to array
        $config[] = array('short' => $filename,
                          'long'  => $longFilename);
    }
} 
foreach($config as $short => $long)
{
    printf( '<option value="%s">%s</option>' , $short, $long);
}
希望这就是你想要的。我强烈建议大家看一看标题为“如果到目前为止您没有或很少使用PHP”的章节。OP编辑了他的问题后,在

上还有一本免费的在线书《新答案》

从您编辑的问题中,我了解到您希望在HTML网页上的
SelectBox
元素中动态填充
选项值的特定目录中的文件。这些值应按破折号、下划线和数字分割,以提供
选项名称
,例如

Directory with Files    >    SelectBox Options
filename1.txt           >    value: filename1.txt, text: Filename 1
file_name2.txt          >    value: filename1.txt, text: File Name 2
file-name3.txt          >    value: filename1.txt, text: File Name 3
根据我在另一个答案中给出的代码,您可以使用
DirectoryIterator
实现这一点,如下所示:

$config = array();
$dir    = new DirectoryIterator('.');
foreach($dir as $item) {
    if($item->isFile()) {
        $fileName = $item->getFilename();
        // turn dashes and underscores to spaces
        $longFileName = str_replace(array('-', '_'), ' ', $fileName);
        // prefix numbers with space
        $longFileName = preg_replace('/(\d+)/', ' $1', $fileName);
        // add to array
        $config[] = array('short' => $filename,
                          'long'  => $longFilename);
    }
} 
foreach($config as $short => $long)
{
    printf( '<option value="%s">%s</option>' , $short, $long);
}
但是,由于目录中的文件名是唯一的,因此也可以将其用作数组:

$config[$filename] => $longFilename;
在构建配置数组时。短文件名将构成数组的键,然后您可以按如下方式构建选择框:

$config = array();
$dir    = new DirectoryIterator('.');
foreach($dir as $item) {
    if($item->isFile()) {
        $fileName = $item->getFilename();
        // turn dashes and underscores to spaces
        $longFileName = str_replace(array('-', '_'), ' ', $fileName);
        // prefix numbers with space
        $longFileName = preg_replace('/(\d+)/', ' $1', $fileName);
        // add to array
        $config[] = array('short' => $filename,
                          'long'  => $longFilename);
    }
} 
foreach($config as $short => $long)
{
    printf( '<option value="%s">%s</option>' , $short, $long);
}

希望这就是你想要的。我强烈建议大家看一看标题为“如果到目前为止您没有或很少使用PHP”的章节。在

Ok Coddict上还有一本免费的在线书,你给了我一个好东西(谢谢),删除了['short']='..'行,但我想要更完整的,从['short']='filename.txt'到$fn[0]['short']='filename.txt”;好的,Coddict,你给了一个好东西(谢谢),行['short']='..'被删除了,但是我想要更完整的,从['short']='filename.txt'到$fn[0][“short”]='filename.txt”;好的,提示删除了['short']='..',比我需要的['short']='filename.txt'更完整到$fn[0][“short”]='filename.txt”;,有什么想法吗?非常感谢你你想把它打印出来,还是