0

次のコードは、画像のディレクトリを反復処理して名前を変更するためのものです。

私はそれをうまく機能させましたが、追加したいのは、ファイルの先頭にある「静的」トークンで、一度名前が変更された後は無視されます。

たとえば、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 ) ;
?>
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/

まず、Web を扱う場合、事実上 2 つのrootディレクトリがあることを理解しなければなりません。

Web ルートとドキュメントルートWeb ルートhttp://www.mysite.com/images/myimage.jpgに関する限り、URL の例を取り上げますが、パス名は次のようになりますが、実際には次のようなドキュメント ルートを使用する必要があります。/images/myimage.jpgphp/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

ファイル名のディレクトリ部分の前に「image-」があります。

$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 に答える