Php For循环以保持文件有序

Php For循环以保持文件有序,php,Php,Linux盒 我需要帮助,试图找出如何让我的for循环将文件以数字顺序保存在它们写入的目录中 我有item1.php,item2.php等等。。。 但是当它到达item10.php时,它将位于item1.php之后,我发现保持它们有序的唯一方法是在前9个文件前面加一个零。但这在代码中并不是那么容易 我试着做了 for ($i = 01; $i < 45; ++$i) { ($i=01;$i

Linux盒

我需要帮助,试图找出如何让我的for循环将文件以数字顺序保存在它们写入的目录中

我有item1.php,item2.php等等。。。 但是当它到达item10.php时,它将位于item1.php之后,我发现保持它们有序的唯一方法是在前9个文件前面加一个零。但这在代码中并不是那么容易

我试着做了

for ($i = 01; $i < 45; ++$i) {
($i=01;$i<45;++$i)的
{
但是“0”被忽略

for ($i = 1; $i < 45; ++$i) {
    if (!file_exists($dirPath . '/item'. $i . '.php')) {
        // fopen, frwite, fclose...
        $myfile = fopen($dirPath . '/item'. $i . '.php', "w") or die("Unable to open item$i file!");
        $txt = "something to write";
        fwrite($myfile, $txt);
        fclose($myfile);
        break;
    }
}
($i=1;$i<45;++$i)的
{
如果(!file_存在($dirPath./item.$i.'.php')){
//fopen,frwite,fclose。。。
$myfile=fopen($dirPath./item'.$i..php',“w”)或die(“无法打开item$i文件!”);
$txt=“要写的东西”;
fwrite($myfile,$txt);
fclose($myfile);
打破
}
}
谢谢你的帮助


Bob

我不明白为什么目录中的顺序很重要,但如果您确实需要它,可以使用以下内容:

for ($i = 1; $i < 45; ++$i) {
  $filename = $dirPath . '/item'. sprintf("%02s", $i) . '.php';
                                             ^ length of number you need, 2 in this case
  if (!file_exists($filename)) {
    $myfile = fopen($filename, "w") or die("Unable to open item$i file!");
    // etc.
($i=1;$i<45;++$i)的
{
$filename=$dirPath.'/item'.sprintf(“%02s”,$i)。'.php';
^所需数字的长度,在本例中为2
如果(!file_存在($filename)){
$myfile=fopen($filename,“w”)或die(“无法打开项目$i文件!”);
//等等。
请参阅上的手册


顺便说一句,我不建议写入php文件,这样做可能会带来安全风险。

你可以做一个非常简单的字符串填充。它会在前面添加0,但最多只能作为2位数字的填充符,所以它可以执行01-09,但不能在之后执行

for($i = 1; $i < 45; ++$i) {
        // The second digit (2) is how many characters it will pad in front so
        // if you go past 2 digits, put it to 3 and it will be 001,002,003, etc...
        // then in double digits it switches to 010,011, etc...
        $i_mod  =   str_pad($i,2,0,STR_PAD_LEFT);
        if(!file_exists($dirPath . '/item'. $i_mod . '.php')) {
                // fopen, frwite, fclose...
                $myfile =   fopen($dirPath . '/item'. $i_mod . '.php', "w") or die("Unable to open item$i_mod file!");
                $txt    =   "something to write";
                fwrite($myfile, $txt);
                fclose($myfile);
                break;
            }
    } 
($i=1;$i<45;++$i)的
{
//第二个数字(2)是它将在前面填充多少个字符
//如果超过2位,将其设为3,则为001002003,以此类推。。。
//然后以两位数切换到010011,以此类推。。。
$i_mod=str_pad($i,2,0,str_pad_左);
如果(!file_存在($dirPath./item.$i_mod..php))){
//fopen,frwite,fclose。。。
$myfile=fopen($dirPath./item'.$i_mod..php',“w”)或die(“无法打开item$i_mod file!”);
$txt=“要写的东西”;
fwrite($myfile,$txt);
fclose($myfile);
打破
}
} 

但是当它到达item10.php时,它将在item1.php之后。php
之后得到什么以及它将在哪里?您显示的循环并不关心文件系统顺序-它从1到44。那么,您所说的
顺序是什么意思,您想在哪里使用它?我正在尝试获取,item01.php,item02.php,item09.php,item10.php在目录中按顺序排列,1、2、3、4、5、6、7、8、9、10、11、12、13等等。我不想让它们混合在一起。我有一个例程,可以抓取名称并将它们放在一个inframe中,如果它们混合在一起,它们就不会按提交的顺序排列。php文件是动态构建的网页。@bpross是的,这就是为什么我想提及它:-)谢谢。我有所有的斜杠stripped,只允许在少数几个字段中输入文本和数字。大多数是下拉式和照片上传,用于租赁检查。它不向公众开放。这些是我自己的私人工作页面。我将其设置为SSL,还需要两次登录。工作很好。非常感谢!这比硬编码这些内容容易得多这将节省300多行不必要的代码。