2013-06-23 4 views
0

내가 다음 코드를 사용하여 폴더에있는 모든 사진 파일의 파일 확장자를 변경하려고했습니다 :폴더에있는 모든 파일의 이름을 바꿀 수 없습니다

$dh = opendir('JS2C'); 
$files = array(); 
while (($file = readdir($dh)) !== false) { 
    if($file !== '.' && $file !== '..') { 
     $file = pathinfo($file); 
     rename($file, $new . '.jpg'); 
    } 
} 

를 내가받을 다음과 같은 경고 메시지 :

SCREAM: Error suppression ignored for 
Warning: rename(ANAZODO.gif,ANAZODO.jpg): 
The system cannot find the file specified. (code: 2) in C:\wamp2\www\ckcportal\batch2.php on ... 

파일이 들어있는 폴더는 PHP 스크립트가있는 동일한 폴더에 있습니다.

답변

1

$d = 'JS2C/' 
$dh = opendir($d); 
while (($file = readdir($dh)) !== false) { 
    if($file !== '.' && $file !== '..') { 
     //$file_no_ext = substr($file, 0,strrpos($file,'.'));// before php 5.2 

     $path_parts = pathinfo($file); // php 5.2 
     $file_no_ext = $path_parts['filename']; // php 5.2 

     rename($d.$file, $d.$file_no_ext . '.jpg'); 
    } 
} 
의 이름을 변경 당신이 디렉토리를 누락
1

받은 오류에서 전체 경로를 제공해야하며 파일 이름을 지정하는 것처럼 보입니다.

rename('/path/to/old/file', '/path/to/new/file'); 

그리고 왜 $file = pathinfo($file);을 사용하고 있습니까? pathinfo은 assoc를 만듭니다. 전체 경로를 제공해야하는 $file의 정보에서 배열을 찾으십시오. 당신이 이것을 꺼내면 작동 할 것입니다.

당신은 다음에 필요하지 않는 :

$info = pathinfo($file); 
$path = $info['dirname'] 

$new_file = $path . '/' . 'newfile.ext'; 

rename($file, $new_file); 
관련 문제