2013-01-19 6 views
0

스크립트의 목적은 전자 상거래 사이트의 디렉토리에있는 이미지 목록의 이름을 변경하는 것입니다. 따라서 특히 스크립트가 rand 일 때 사용자는 접두사가 붙을 파일 세트 나 목록을 원하는 단어 나 구를 입력합니다. 스크립트는 각 파일에 대해 접두어를 변경하고 0에서 시작하여 다음으로 사용할 수있는 번호를 추가합니다.디렉토리 내의 이미지 이름 변경 및 변경된 파일 이름 표시

어떤 파일이 변경되었는지 사용자에게 페이지에 표시/렌더링하고 싶습니다. 스크립트가 실행될 때 현재 디렉토리 내의 현재 파일을 표시 한 다음 변경된 파일과 해당 이름을 디렉토리에 나열합니다.

스크립트가 새 이름 처리를 마쳤 으면 파일 목록 만 표시되도록하려면 어떻게해야합니까?

왜 스크립트가 파일 이름에 적절한 증가 숫자를 추가하지 않습니까? 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>"; 
     } 


?> 

답변

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);