11

コンパートメント番号を変更したいので注意してください。

<?php
    $compartment = "1";

        /* HERE I NEED SOME SCRIPT TO FIND THE EXTENSION OF THE FILE NAME $compartment AND TO SAVE THAT AS A VARIABLE NAMED 'EXTENSION'.*/

    if (file_exists($compartment.$extension)) {
        echo "$compartment.$extension exists!
    } else {
        echo "No file name exists that is called $compartment. Regardless of extension."
    }
?>


<?php
    $compartment = "2";

        /* HERE I NEED SOME SCRIPT TO FIND THE EXTENSION OF THE FILE NAME $compartment AND TO SAVE THAT AS A VARIABLE NAMED 'EXTENSION'.*/

    if (file_exists($$compartment.$extension)) {
        echo "$compartment.$extension exists!
    } else {
        echo "No file name exists that is called $compartment. Regardless of extension."
    }
?>

ありがとう!

4

2 に答える 2

25

必要glob()です。

$compartment = "2";

$files = glob("/path/to/files/$compartment.*"); // Will find 2.txt, 2.php, 2.gif

// Process through each file in the list
// and output its extension
if (count($files) > 0)
foreach ($files as $file)
 {
    $info = pathinfo($file);
    echo "File found: extension ".$info["extension"]."<br>";
 }
 else
  echo "No file name exists called $compartment. Regardless of extension."

ちなみに、あなたが上でやっていることは、ループを求めて泣いていることです。コードブロックを繰り返さないでください。ただし、そのうちの1つを次のようにラップしてください。

 $compartments = array(1, 3, 6, 9); // or whichever compartments 
                                    // you wish to run through

 foreach ($compartments as $compartment)
  {
   ..... insert code here .......
  }
于 2010-07-08T09:29:40.003 に答える
5

見上げる:

  • glob —パターンに一致するパス名を検索します
  • fnmatch —ファイル名をパターンと照合します
  • pathinfo —ファイルパスに関する情報を返します
于 2010-07-08T09:31:13.230 に答える