php实现文件编码批量转换

有些问题,不能重复转,比如gbk转到utf8,然后有在转成utf8,这样会乱码,我本来试图在转换之前去检测编码的,貌似失败了。我特意试了一个文件,我检测它是是否是gbk或者是utf-8,都返回true。这就不懂了。

代码如下:

<?php
/**
 * 转换文件编码
 * 依赖的扩展filesystem 和 mbstring
 * @example
 * <pre>
 * include_once 'ConvertEncode.php';
 * $convert = new ConvertEncode();
 * try{
 *   $convert->setPath('my', true, true);//目录
 *    //$convert->setPath('my.php');//单文件
 *   $convert->setEncode('GBK', 'UTF-8');
 *   $convert->convert();
 * }catch(ConvertException $e) {
 *   echo $e->getMessage();
 * }
 * </pre>
 */
class ConvertEncode {

/**
  * 要转换成的编码
  * @var string
  */
 private $_to_encoding;

/**
  * 转换前的编码
  * @var string
  */
 private $_from_encoding;

/**
  * 要转换的的目录或者单文件
  * @var string
  */
 private $_path;

/**
  * 是否是一个目录,当给出的是目录是才设置
  * @var boolean
  */
 private $_directory;

/**
  * 是否递归遍历,仅对目录有效
  * @var boolean
  */
 private $_recursion;

/**
  * 保存所有待转换的文件,仅当转换目录里面的文件时才用
  * @var array
  */
 private $_files = array();

/**
  * 构造函数
  */
 public function __construct() {
  if( ! function_exists('mb_convert_encoding') ) {
   throw new ConvertException('mbstring extension be required');
  }
 }

/**
  * 设置需要转换的目录或者单文件
  * @param string $path 目录或者文件
  * @param boolean 是否是目录
  * @param boolean 是否递归目录
  * @return boolean
  */
 public function setPath($path, $is_dir = false, $rec = false) {
  $this->_path = $path;
  $this->_directory = $is_dir;
  $this->_recursion = $rec;
  return true;
 }

/**
  * 设置转换前的编码和要转换到的编码
  * @param string $encode 转换前的编码
  * @param string $encode 转换到的编码
  * @return boolean
  */
 public function setEncode($encode_from, $encode_to) {
  $this->_from_encoding = $encode_from;
  $this->_to_encoding   = $encode_to;
  return true;
 }

/**
  * 转换编码,根据是否是目录的设置分别转换
  * @return boolean
  */
 public function convert() {
  if($this->_directory ) {
   return $this->_convertDirectory();
  }
  return $this->_convertFile();
 }

/**
  * 转换文件
  * @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;

}

/**
  * 转换目录
  * @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;
 }

/**
  * 判断文件或者目录是否可读写
  * @return boolean 可读写时返回true,否则返回false
  */
 private function _isWR() {
  if( is_readable($this->_path) && is_writable($this->_path) ) {
   return true;
  }
  return false;
 }

/**
  * 遍历目录,找出所有文件,加上绝对路径
  * @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;
 }
}

/**
 * 转换异常
 *
 */
class ConvertException extends Exception {

}

(0)

相关推荐

  • php中批量替换文件名的实现代码

    代码如下 复制代码 代码如下: $dir = 'D:\Program Files\resource\application\Skin\PNG\\';//注意这里的路径,最后要加两个\,第一个表示转意,但是这样容易遇到其他特定转义,还要仔细判断,可以写为如下方式 $dir = 'D:/Program Files/resource/application/Skin/PNG/';//写成这样的路径,就不用担心转义问题了.最后面的/不要漏写 if ($dh = opendir($dir)) { whil

  • PHP实现批量修改文件后缀名的方法

    本文实例讲述了PHP实现批量修改文件后缀名的方法.分享给大家供大家参考.具体如下: /** * 批量修改文件后缀名 * @param $path 文件夹路径 * @param $sext 原文件后缀名 ($sext=all说明整个目录的所有文件) * @param $dext 目的文件后缀名 * @return void */ function foreachDir($path,$sext,$dext){ $handle=opendir($path); if($handle){ while (f

  • PHP批量修改文件名称的方法分析

    本文实例讲述了PHP批量修改文件名称的方法.分享给大家供大家参考,具体如下: 在这里我们利用一个战地自己写的一个例子来具体分析一下利用PHP批量修改文件名称的思路和注意事项. 从这个例子里你将会看到,PHP如何判断一个路径是不是目录,如何用PHP取得路径中的文件名称和文件的扩展名,随机函数rand的基本应用,以及这个程序的核心内容递归函数的基本应用. 程序功能:利用PHP目录和文件函数遍历用户给出目录的所有的文件和文件夹,修改文件名称: 首先判断用户给出的目录是不是一个合法目录: 我们这里只是修

  • 基于php上传图片重命名的6种解决方法的详细介绍

    一,适用场景:无法使用从数据库中返回的自增长数字,给上传图片重命名. 这是图片或文件上传的流程决定的.一般图片上传处理过程是,先上传图片到服务器,重命名之后,插入到数据库.也就是说,在数据库中非常容易获得的自增长id,无法用于给上传的图片重命名,来避免文件名称的重复,而采用从数据库中获取最大id加1的方式,增加了数据库连接的次数,不适用于高并发和数据量巨大的情况: 二,常规方案: 1,guid:32 字符十六进制数.格式:GUID 的格式为"xxxxxxxx-xxxx-xxxx-xxxx-xxx

  • php中批量修改文件后缀名的函数代码

    复制代码 代码如下: <?php function foreachDir($path){ $handle=opendir($path); if($handle){ while (false !== ($file = readdir($handle))) { if($file!="." && $file!='..'){ if(is_dir($path.$file)){ echo $path.$file."<br/>"; foreach

  • PHP自动重命名文件实现方法

    本文实例讲述了PHP自动重命名文件实现方法.分享给大家供大家参考.具体方法分析如下: PHP重命名文件名我们在实际开发过程中经常会使用到,比如用户上传文件或是一些缓存文件自动生成的功能我们都需要使用到自动重命名功能.但一般我们在制作上传文件时命名方式都是使用取系统当前时间加上随时数的方式在进行,这种方法固然可行但有时候并不能满足客户要需求.有些客户就要求我们的文件名命名方式要像windows系统一样自动流水编号,比如上传一个名字为"新建文本文档"当再有其它人上传一个文件名为"

  • php实现批量修改文件名称的方法

    本文实例讲述了php实现批量修改文件名称的方法.分享给大家供大家参考,具体如下: <?php session_start(); set_time_limit(0); //function allfile($dir) // { // $files=array(); // if(is_file($dir)) // { // return $dir; // } // $handle = opendir($dir); // if($handle) { // while(false !== ($file

  • fckeditor php上传文件重命名的设置

    这里经过摸索:找到了一个重命名的方法,大家看看好不好使. 首先:我希望上传的文件根据日期来组织文件夹 请修改editor\editor\filemanager\connectors\php文件夹下的:config.php文件 找到如下的内容: // Path to user files relative to the document root. $Config['UserFilesPath'] = 修改为: // Path to user files relative to the docum

  • PHP实现批量重命名某个文件夹下所有文件的方法

    本文实例讲述了PHP实现批量重命名某个文件夹下所有文件的方法.分享给大家供大家参考,具体如下: 自己手动这样一个个的重命名,累啊.所以还是偷懒一下. 我重命名的规则是把所有有空格的全部替换成"_",然后再后面加一个"_s". <?php $paths = "C://Documents and Settings//sk//Desktop//s//"; $d = dir($paths); while (false !== ($entry = $

  • php 随机数的产生、页面跳转、件读写、文件重命名、switch语句

    复制代码 代码如下: <?php num = rand(1,5); witch($num){ case 1: $fp1=fopen("f1.dat",'r'); $oname = fgets($fp1); $nname = date("YmdHis"); rename($oname,$nname); fclose($fp1); unlink("f1.dat"); $fp1=fopen("f1.dat",'w'); fwr

  • 6种php上传图片重命名的方法实例

    一.适用场景:无法使用从数据库中返回的自增长数字,给上传图片重命名.这是图片或文件上传的流程决定的.一般图片上传处理过程是,先上传图片到服务器,重命名之后,插入到数据库.也就是说,在数据库中非常容易获得的自增长id,无法用于给上传的图片重命名,来避免文件名称的重复,而采用从数据库中获取最大id加1的方式,增加了数据库连接的次数,不适用于高并发和数据量巨大的情况:二.常规方案:1,guid:32 字符十六进制数.格式:GUID 的格式为"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxx

随机推荐