<?php
/**
*@desc 将$origDir文件夹里的所有文件转化为$newDir文件夹中后缀名为.$newExt的文件
*@author yuanj
*@param string $origDir
*@param string $newDir
*@param string $newExt
*@return void
*@date 2013-3-27
*/
function fileTransfer($origDir,$newDir,$newExt='jpg'){
if (!is_dir($origDir) or !is_dir($newDir)) {
return;
}
$origDirArray = scandir($origDir);
foreach ($origDirArray as $v) {
if ($v == '.' or $v == '..') {//若为文件夹则跳过
continue;
}
$orig_file = $origDir.'/'.$v;
$new_location = $newDir.'/'.$v;
if (is_dir($orig_file)) {
if (!file_exists($new_location)) {
mkdir($new_location,0700);//创建新文件夹
}
fileTransfer($origDir.'/'.$v,$newDir.'/'.$v,$newExt);
continue;
}
if (file_exists($orig_file)){
$newFileName = dirname($orig_file);
$newFileName = str_replace($newFileName, '', $orig_file);
$origExt = pathinfo($orig_file,PATHINFO_EXTENSION);
$newFileName = str_replace($origExt, $newExt, $newFileName);
$newFileName = $newDir.$newFileName;
$fileData = file_get_contents($orig_file);
$newFile = fopen($newFileName, 'a');
fwrite($newFile, $fileData);
fclose($newFile);
echo '<br>',$orig_file,' ---> ',$newFileName;
ob_flush();
flush();
// sleep(1);
}
}
}
//调用方法如下
$origDir = 'C:/AppServ/www/intra/uploads';
$newDir = 'C:/AppServ/www/intra/images';
echo 'start transfer,please wait ^V^';
fileTransfer($origDir,$newDir,'jpg');
echo '<br>transfer finished';
评论3
最新资源