PHP使用随机名称重命名目录中的所有文件

PHP使用随机名称重命名目录中的所有文件,php,image,rename,Php,Image,Rename,我的网站上有5000张图片的目录。在PHP中,如何使用完全随机的名称重命名目录中的所有图像(例如ri32rif293jf32f3f或其他名称) 我需要知道这一点,这样我就可以每隔几个月对它们进行随机重命名。这很简单: function random($numchars,$digits,$small,$capital,$splchr) { $dig = "0123456789"; $sml = "abcdefghijklmnopqrstuvwxyz"; $spl = "!@#$%*"; $c

我的网站上有5000张图片的目录。在PHP中,如何使用完全随机的名称重命名目录中的所有图像(例如ri32rif293jf32f3f或其他名称)


我需要知道这一点,这样我就可以每隔几个月对它们进行随机重命名。

这很简单:

function random($numchars,$digits,$small,$capital,$splchr)
{
$dig = "0123456789";

$sml = "abcdefghijklmnopqrstuvwxyz";

$spl = "!@#$%*";

$cap = "ABCDEFGHJKLMNOPQRSTUVWXYZ";

if($digits == 1)
{
    $str .= $dig;
}

if($small == 1)
{
    $str .= $sml;
}

if($capital == 1)
{
    $str .= $cap;
}

if($splchr == 1)
{
    $str .= $spl;
}

for($j=0; $j < $numchars; $j++)
{
    $randomized .= $str{ rand() % strlen($str) };
}

return $randomized;
}

    $source = "/source direcotry/";
    $destination = "/destination directory/";
    function getDirectory( $source, $destination, $level=0)
    {
    $count =1;

       $ignore = array( 'cgi-bin', '.', '..' );
       if($handle = opendir($source))
       { 
        //one by one reading a file
        while(false !== ($file = readdir($handle))) 
        {
           if( is_dir( "$source/$file" ) )
              if (!file_exists($destination.$file))
                 mkdir("$destination$file", 0700);
           if( !in_array( $file, $ignore ) )
           {
              if( is_dir( "$source/$file" ) )
              {
                getDirectory( "$source$file/", "$destination$file/", ($level+1));
              }
              else 
              { 
                  $rand = random(16,1,1,1,1);
                  $srcfile = file_get_contents($source.$file);
                  $extfile=substr($file, strrpos($file, "."));      
                  file_put_contents($destination.$rand.$extfile , $srcfile);
              }
           }
        }
        $total[$level] = $total + intval($count-1);
        echo "<br/><b> $destination  ---- ".intval($count-1)."</b><br/><br/>";
      }
      closedir($handle); 
    }
    //calling getDirectory function for repeat the for all sub folders.
    getDirectory( $source , $destination, 0);  
  • 您自己创建了一个可以生成随机文件名的函数
  • 您将获得该目录中的文件列表
  • 您可以创建第二个列表,其中旧列表的每个文件都有一个条目,其中包含使用函数从1.)生成的新名称,该名称在2.)列表中不存在,目前不属于3.)列表的一部分
  • 将2.)的文件重命名为新名称3.)
  • 完成了。将所有这些都封装到它自己的函数中,这样你就可以很容易地每隔几个月调用一次。

    函数random($numchars、$digits、$small、$capital、$splchr)
    
    function random($numchars,$digits,$small,$capital,$splchr)
    {
    $dig = "0123456789";
    
    $sml = "abcdefghijklmnopqrstuvwxyz";
    
    $spl = "!@#$%*";
    
    $cap = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
    
    if($digits == 1)
    {
        $str .= $dig;
    }
    
    if($small == 1)
    {
        $str .= $sml;
    }
    
    if($capital == 1)
    {
        $str .= $cap;
    }
    
    if($splchr == 1)
    {
        $str .= $spl;
    }
    
    for($j=0; $j < $numchars; $j++)
    {
        $randomized .= $str{ rand() % strlen($str) };
    }
    
    return $randomized;
    }
    
        $source = "/source direcotry/";
        $destination = "/destination directory/";
        function getDirectory( $source, $destination, $level=0)
        {
        $count =1;
    
           $ignore = array( 'cgi-bin', '.', '..' );
           if($handle = opendir($source))
           { 
            //one by one reading a file
            while(false !== ($file = readdir($handle))) 
            {
               if( is_dir( "$source/$file" ) )
                  if (!file_exists($destination.$file))
                     mkdir("$destination$file", 0700);
               if( !in_array( $file, $ignore ) )
               {
                  if( is_dir( "$source/$file" ) )
                  {
                    getDirectory( "$source$file/", "$destination$file/", ($level+1));
                  }
                  else 
                  { 
                      $rand = random(16,1,1,1,1);
                      $srcfile = file_get_contents($source.$file);
                      $extfile=substr($file, strrpos($file, "."));      
                      file_put_contents($destination.$rand.$extfile , $srcfile);
                  }
               }
            }
            $total[$level] = $total + intval($count-1);
            echo "<br/><b> $destination  ---- ".intval($count-1)."</b><br/><br/>";
          }
          closedir($handle); 
        }
        //calling getDirectory function for repeat the for all sub folders.
        getDirectory( $source , $destination, 0);  
    
    { $dig=“0123456789”; $sml=“abcdefghijklmnopqrstuvxyz”; $spl=“!@$%*”; $cap=“abcdefghjklmnopqrstuvxyz”; 如果($digits==1) { $str.=$dig; } 如果($small==1) { $str.=$sml; } 如果($capital==1) { $str.=$cap; } 如果($splchr==1) { $str.=$spl; } 对于($j=0;$j<$numchars;$j++) { $randominated.=$str{rand()%strlen($str)}; } 返回$0; } $source=“/source direcotry/”; $destination=“/destination directory/”; 函数getDirectory($source,$destination,$level=0) { $count=1; $ignore=array('cgi bin','.','..); 如果($handle=opendir($source)) { //一个接一个地读取文件 while(false!=($file=readdir($handle))) { if(is_dir(“$source/$file”)) 如果(!file_存在($destination.$file)) mkdir(“$destination$file”,0700); 如果(!in_数组($file,$ignore)) { if(is_dir(“$source/$file”)) { getDirectory(“$source$file/”、“$destination$file/”、($level+1)); } 其他的 { $rand=随机(16,1,1,1,1); $srcfile=file\u get\u contents($source.$file); $extfile=substr($file,strrpos($file,“.”); 文件内容($destination.$rand.$extfile,$srcfile); } } } $total[$level]=$total+intval($count-1); echo“
    $destination----”.intval($count-1)。“

    ”; } closedir($handle); } //调用getDirectory函数以对所有子文件夹重复。 getDirectory($source,$destination,0);
    您尝试过什么?展示你已经做了什么。这类问题在社区中是不受欢迎的。你是想为了安全起见重命名这些文件吗?因为我不知道怎么做,所以我没有真正尝试过任何东西。我知道的唯一一点是如何随机命名,这就是为什么我没有写任何代码;你同意吗?谢谢,但我想更多的是一种方法,它只是在目录中迭代,我想在开始时将它们全部放入一个数组中。是的,这就是上面清单中的2。)。