ls | sed -n '/[A-Z]/s/.*/mv & \L&/e'
公司以前用的windows server 服务器 文件大小写都一样。 新迁移到centos 服务器上,发现有些上传图片是大写的扩展名。
<?php
$path=$_SERVER['DOCUMENT_ROOT'].'/uploadfile';//要查找的目录
echo $path;
//var_dump(opendir($path)); //测试系统是否有权限执行
//die('end');
if($handle = opendir($path)){
while(false !== ($file = readdir($handle))){
if($file !='.' && $file !=".."){
if(is_dir($path.'/'.$file)){
nextdir($path.'/'.$file);
}else{
echo $file;
}
}
}
}
/***循环目录***/
function nextdir($dir){
$handle=opendir($dir);
while(false !== ($file=readdir($handle))){
if($file !='.' && $file !='..'){
if(is_dir($dir.'/'.$file)){
nextdir($dir.'/'.$file); }else{
renamejpg($dir.'/'.$file);
}
}
}
}
/**修改文件名**/
function renamejpg($file){
if(substr($file,-3)=='JPG'){
file_put_contents('rename.log',$file."\n",FILE_APPEND);
rename($file,substr($file,0,-3).'jpg');
echo $file.'<br>';
}
} ?>
在本地调试是ok的,但在服务器上不行。发现是权限的问题。服务器php-fpm 是用nobody运行的,没有权限运行opendir.后新建一个php-fpm 用www帐号运行。