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スクリプトと同じフォルダーにあります。

4

2 に答える 2

1

受け取ったエラーから、完全なパスを指定する必要があります。ファイル名を指定しているように見えます。

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

そして、なぜあなたは使用してい$file = pathinfo($file);ますか?pathinfoは関連付けを作成します。情報からの配列は$file、完全なパスを提供する必要があります。これを取り出せば、うまくいくはずです。

あなたが従う必要がない限り:

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

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

rename($file, $new_file);
于 2013-06-23T12:43:40.583 に答える