0

以下代码旨在遍历图像目录并重命名它们。

我让它运行良好,但我想在文件开头添加一个“静态”标记,一旦重命名,它将在未来忽略。

例如,假设我们有一个包含 100 个文件的目录。

其中前 20 个名称为“image-JTzkT1RYWnCqd3m1VXYcmfZ2nhMOCCunucvRhuaR5.jpg”,后 80 个名称为“FejVQ881qPO5t92KmItkNYpny.jpg”,这绝对可以是任何东西。

我想忽略已经重命名的文件(由文件名开头的“图像-”表示)

我怎样才能做到这一点?

    <?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 ) ;
?>
4

2 回答 2

1

似乎您正在为应该非常微不足道的事情做很多不必要的工作。

据我了解,您希望从目录中获取文件列表并重命名所有尚未以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 根目录和文档根目录http://www.mysite.com/images/myimage.jpgweb 根目录而言,以示例 url 为例,但是路径名是/images/myimage.jpg,从php你有你使用的文档根目录,它实际上是这样的:/home/mysite/pubic_html/images/myimage.jpg

因此,当您输入/php 时,它认为您的意思home是所在的目录。

./images/之所以有效,是因为这./意味着这个目录,即 script/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:\)。

于 2013-10-01T10:34:10.220 回答
0

您的文件名的目录部分前面有“图像-”:

$ren = rename ( "$dir/$file_array[$counter]" , "image-$dir/$new.jpg" )

应该

$ren = rename ( "$dir/$file_array[$counter]" , "$dir/image-$new.jpg" )
于 2013-10-01T09:53:28.300 回答