0

このスクリプトの目的は、e コマース サイトのディレクトリ内の画像リストの名前を変更することです。そのため、特にスクリプトが rand の場合、ユーザーは、ファイルのセットまたはリストにプレフィックスを付けたい単語またはフレーズを入力します。スクリプトは、プレフィックスを変更し、ゼロから始まる次の使用可能な番号を追加して、各ファイルを反復処理します。

どのファイルが変更されたかをユーザーに表示/レンダリングしたいと思います。スクリプトを実行すると、ディレクトリ内の現在のファイルが表示され、ディレクトリ内の変更されたファイルとその名前が一覧表示されます。

スクリプトが新しい名前の処理を終了したときにのみファイル リストを表示するにはどうすればよいですか?

スクリプトが適切な増分番号をファイル名に追加しないのはなぜですか? 次の順序でファイルの名前が変更されます。 abc0.jpg abc1.jpg abc10.jpg abc11.jpg abc12.jpg abc13.jpg

<?php 
  $display_file_list;

    //Allow user to put choose name
  if (isset($_POST['file_prefix'])){

    $the_user_prefix = $_POST['file_prefix'];


   //open the current directory change this to modify where you are looking
   $dir = opendir('.');

   $i=0;

   //Loop though all the files in the directory 
   while(false !==($file = readdir($dir)))
   {

    //This is the way we would like the page to function 

    //if the extention is .jpg
    if(strtolower(pathinfo($file, PATHINFO_EXTENSION)) =='jpg')
    {
      //Put the JPG files in an array to display to the user
      $display_file_list[]= $file;

      //Do the rename based on the current iteration
      $newName = $the_user_prefix.$i . '.jpg';
      rename($file, $newName);
      //increase for the next loop
      $i++;
    }

       }



     //close the directory handle
     closedir($dir);

      }else{

    echo "No file prefix provided";
  }




?>

    <html>
    <body>
     <form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
    <input type="text" name="file_prefix"><br>
    <input type="submit" value="submit" name="submitMe">
    </form>
    </body>

</html>

<?php
  foreach ($display_file_list as $key => $value) {
           echo  $value. "<br>";
         } 


?>
4

1 に答える 1

0

「スクリプトが新しい名前の処理を終了したときにのみ表示されるファイルリストを取得するにはどうすればよいですか?」

これはあなたのために働くと思います。

$path  = "path/to/files";
$files = glob("{$path}/*.jpg");
$files_renamed = array();
foreach ($files as $i => $file) {
    $name = "{$path}/{$prefix}{$i}.jpg";
    if (true === rename($file)) {
        $files_renamed[] = $name;
    }
}
print_r($files_renamed);
于 2013-01-19T16:14:39.590 に答える