php implements batch conversion of file encoding

  • 2021-01-22 04:57:47
  • OfStack

For example, gbk goes to utf8 and then to utf8 again. This will cause confusion. I tried to check the code before the conversion, but it seems to have failed. I specifically tried a file, I checked whether it was gbk or utf-8, both returned true. This is confusing.


<?php
/**
 *  Convert file encoding 
 *  Extension of Dependencies filesystem  and  mbstring
 * @example
 * <pre>
 * include_once 'ConvertEncode.php';
 * $convert = new ConvertEncode();
 * try{
 *   $convert->setPath('my', true, true);// directory 
 *    //$convert->setPath('my.php');// A single file 
 *   $convert->setEncode('GBK', 'UTF-8');
 *   $convert->convert();
 * }catch(ConvertException $e) {
 *   echo $e->getMessage();
 * }
 * </pre>
 */
class ConvertEncode {

 /**
  *  The encoding to be converted to 
  * @var string
  */
 private $_to_encoding;

 /**
  *  Encoding before conversion 
  * @var string
  */
 private $_from_encoding;

 /**
  *  The directory or single file to be converted 
  * @var string
  */
 private $_path;

 /**
  *  Whether it is 1 A directory is only set when a directory is given 
  * @var boolean
  */
 private $_directory;

 /**
  *  Whether to traverse recursively, only for directories 
  * @var boolean
  */
 private $_recursion;

 /**
  *  Save all files to be converted. Only used when converting files in the directory 
  * @var array
  */
 private $_files = array();

 /**
  *  The constructor 
  */
 public function __construct() {
  if( ! function_exists('mb_convert_encoding') ) {
   throw new ConvertException('mbstring extension be required');
  }
 }

 /**
  *  Sets the directory or single file to be converted 
  * @param string $path  Directories or files 
  * @param boolean  Directory or not 
  * @param boolean  Whether to recurse the directory 
  * @return boolean
  */
 public function setPath($path, $is_dir = false, $rec = false) {
  $this->_path = $path;
  $this->_directory = $is_dir;
  $this->_recursion = $rec;
  return true;
 }

 /**
  *  Sets the encoding before the conversion and the encoding to be converted to 
  * @param string $encode  Encoding before conversion 
  * @param string $encode  Convert to the encoding 
  * @return boolean
  */
 public function setEncode($encode_from, $encode_to) {
  $this->_from_encoding = $encode_from;
  $this->_to_encoding   = $encode_to;
  return true;
 }

 /**
  *  Convert encoding, respectively, depending on whether it is a directory setting 
  * @return boolean
  */
 public function convert() {
  if($this->_directory ) {
   return $this->_convertDirectory();
  }
  return $this->_convertFile();
 }

 /**
  *  Convert file 
  * @throws ConvertException
  * @return boolean
  */
 private function _convertFile() {
  if( ! file_exists($this->_path) ) {
   $message = $this->_path . ' does not exist.';
   throw new ConvertException($message);
  }
  if( ! is_file($this->_path) ) {
   $message = $this->_path . ' is not a file.';
   throw new ConvertException($message);
  }
  if( ! $this->_isWR() ) {
   $message = $this->_path . ' must can be read and write.';
   throw new ConvertException($message);
  }
  $file_real_path    = realpath($this->_path);
  $file_content_from = file_get_contents( $file_real_path );
  if( mb_check_encoding($file_content_from, $this->_from_encoding) ) {
   $file_content_to   = mb_convert_encoding( $file_content_from, $this->_to_encoding, $this->_from_encoding );
   file_put_contents( $file_real_path, $file_content_to );
  }
  return true;

 }

 /**
  *  Conversion directory 
  * @throws ConvertException
  * @return boolean
  */
 private function _convertDirectory() {
  if( ! file_exists($this->_path) ) {
   $message = $this->_path . ' does not exist.';
   throw new ConvertException($message);
  }
  if( ! is_dir($this->_path) ) {
   $message = $this->_path . ' is not a directory.';
   throw new ConvertException($message);
  }
  if( ! $this->_isWR() ) {
   $message = $this->_path . ' must can be read and write.';
   throw new ConvertException($message);
  }
  $this->_scanDirFiles();
  if( empty($this->_files) ) {
   $message = $this->_path . ' is a empty directory.';
   throw new ConvertException($message);
  }
  foreach( $this->_files as $value ) {
   $file_content_from = file_get_contents( $value );
   if( mb_check_encoding($file_content_from, $this->_from_encoding) ) {
    $file_content_to   = mb_convert_encoding( $file_content_from, $this->_to_encoding, $this->_from_encoding );
    file_put_contents( $value, $file_content_to );
   }
  }
  return true;
 }

 /**
  *  Determine whether a file or directory is read-write 
  * @return boolean  Returns when read-write true, Otherwise returns false
  */
 private function _isWR() {
  if( is_readable($this->_path) && is_writable($this->_path) ) {
   return true;
  }
  return false;
 }

 /**
  *  Traverse the directory, find all the files, and add the absolute path 
  * @return boolean
  */
 private function _scanDirFiles($dir = '') {
  $base_path = empty( $dir ) ? realpath($this->_path) . DIRECTORY_SEPARATOR : realpath($dir) . DIRECTORY_SEPARATOR;
  $files_tmp = empty( $dir ) ? scandir($this->_path) : scandir($dir);
  foreach( $files_tmp as $value ) {
   if( $value == '.' || $value == '..' || ( strpos($value, '.') === 0 ) ) {
    continue;
   }
   $value = $base_path . $value;
   if( is_dir($value) ) {
    if( $this->_recursion ) {
     $this->_scanDirFiles($value);
    }
   }
   elseif( is_file($value) ) {
    $this->_files[] = $value;
   }
  }
  return true;
 }
}
/**
 *  Convert exceptions 
 *
 */
class ConvertException extends Exception {

}


Related articles: