以下代码旨在循环访问图像目录并重命名它们。
我让它运行良好,但我想在文件的开头添加一个"静态"令牌,一旦重命名,它将来会忽略。
例如,假设我们有一个包含 100 个文件的目录。
其中前20个名称为"image-JTzkT1RYWnCqd3m1VXYcmfZ2nhMOCCunucvRhuaR5.jpg"最后80个的名字是"FejVQ881qPO5t92KmItkNYpny.jpg",这绝对可以是任何东西。
我想忽略已经重命名的文件(由文件名开头的"image-"表示(
我该怎么做?
<?php
function crypto_rand_secure($min, $max) {
$range = $max - $min;
if ($range < 0) return $min; // not so random...
$log = log($range, 2);
$bytes = (int) ($log / 8) + 1; // length in bytes
$bits = (int) $log + 1; // length in bits
$filter = (int) (1 << $bits) - 1; // set all lower bits to 1
do {
$rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
$rnd = $rnd & $filter; // discard irrelevant bits
} while ($rnd >= $range);
return $min + $rnd;
}
function getToken($length){
$token = "";
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
$codeAlphabet.= "0123456789";
for($i=0;$i<$length;$i++){
$token .= $codeAlphabet[crypto_rand_secure(0,strlen($codeAlphabet))];
}
return $token;
}
$dir = "/path/to/images";
if ( $handle = opendir ( $dir)) {
echo "Directory Handle = $handles<br />" ;
echo "Files: <br />" ;
while ( false !== ($file = readdir($handle))) {
if ( $file != "." && $file != ".." ) {
$isdir = is_dir ( $file ) ;
if ( $isdir == "1" ) {} // if its a directory do nothing
else {
$file_array[] = "$file" ; // get all the file names and put them in an array
//echo "$file<br />" ;
} // closes else
} // closes directory check
} // closes while
} // closes opendir
//Lets go through the array
$arr_count = count( $file_array ) ; // find out how many files we have found so we can initiliase the counter
for ( $counter=1; $counter<$arr_count; $counter++ ) {
echo "Array = $file_array[$counter] - " ; // tell me how many files there are
//$new = str_replace ( "C", "CIMG", $file_array[$counter] ) ; // now create the new file name
$new =getToken(50);
//if (substr($file_array[$counter]), 0, 3) == "gallery_image")
//{
//}
//else
//{
$ren = rename ( "$dir/$file_array[$counter]" , "image-$dir/$new.jpg" ) ; // now do the actual file rename
echo "$new<br />" ; // print out the new file name
//}
}
closedir ( $handle ) ;
?>
看起来你正在为一些应该非常微不足道的事情做很多不必要的工作。
据我了解,您想从目录中获取文件列表,并重命名所有尚未预先加image-
的文件。
您可以使用以下代码段执行此操作:
$dir = "/path/to/dir/"; //Trailing slash is necessary or you would have to change $dir.$newname to $dir.'/'.$newname further down the code
if(is_dir($dir)){
$dirHandle = opendir($dir);
while($file = readdir($dirHandle)){
if(is_file($dir.$file) === TRUE){
if(strpos($file, 'image-') === 0)
continue; //Skip if the image- is apready prepended
while(TRUE){
$cryptname = md5(mt_rand(1,10000).$file.mt_rand(1,10000)); //Create random string
$newname = 'image-'.$cryptname.'.jpg'; //Compile new file name
if(is_file($dir.$newname) === FALSE) //Check file name doesn't exist
break; //Exit loop if it doesn't
}
rename($dir.$file, $dir.$newname); //Rename file with new name
}
}
}
如果你想使用自己的函数,那么你可以改变行:
$cryptname = md5(mt_rand(1,10000).$file.mt_rand(1,10000));
使用您的函数而不是md5
.喜欢:
$cryptname = getToken(50);
我建议您还应该检查文件名是否不存在,否则 - 尽管不太可能 - 您将面临覆盖文件的风险。
/images/
与./images/
首先,在处理网络时,您必须了解实际上有两个root
目录。
Web 根目录和文档根目录。以示例 url http://www.mysite.com/images/myimage.jpg
就 Web 根目录而言,路径名是/images/myimage.jpg
但是,从php
开始使用文档根目录,它实际上是这样的:/home/mysite/pubic_html/images/myimage.jpg
因此,当您在php中键入/
时,它认为您指的是home
所在的目录。
./images/
工作是因为./
表示此目录,即脚本/php所在的目录。
在您的情况下,您可能有一个这样的文件结构:
>/
>home
>public_html
>images
image-243.jpg
image-243.jpg
index.php
所以因为index.php
与images
:./images/
等于/home/public_html/images/
在同一个文件夹中。 另一方面,/
是指没有图像文件夹的home
的父目录,更不用说您要定位的文件夹了。
如果你更习惯于Windows,可以这样想:在php中,/
表示文档根目录(在Windows上类似于C:'
(。
文件名的目录部分前面有"image-":
$ren = rename ( "$dir/$file_array[$counter]" , "image-$dir/$new.jpg" )
应该是
$ren = rename ( "$dir/$file_array[$counter]" , "$dir/image-$new.jpg" )