php實(shí)現(xiàn)文件編碼批量轉(zhuǎn)換
有些問題,不能重復(fù)轉(zhuǎn),比如gbk轉(zhuǎn)到utf8,然后有在轉(zhuǎn)成utf8,這樣會(huì)亂碼,我本來試圖在轉(zhuǎn)換之前去檢測(cè)編碼的,貌似失敗了。我特意試了一個(gè)文件,我檢測(cè)它是是否是gbk或者是utf-8,都返回true。這就不懂了。
<?php
/**
* 轉(zhuǎn)換文件編碼
* 依賴的擴(kuò)展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 {
/**
* 要轉(zhuǎn)換成的編碼
* @var string
*/
private $_to_encoding;
/**
* 轉(zhuǎn)換前的編碼
* @var string
*/
private $_from_encoding;
/**
* 要轉(zhuǎn)換的的目錄或者單文件
* @var string
*/
private $_path;
/**
* 是否是一個(gè)目錄,當(dāng)給出的是目錄是才設(shè)置
* @var boolean
*/
private $_directory;
/**
* 是否遞歸遍歷,僅對(duì)目錄有效
* @var boolean
*/
private $_recursion;
/**
* 保存所有待轉(zhuǎn)換的文件,僅當(dāng)轉(zhuǎn)換目錄里面的文件時(shí)才用
* @var array
*/
private $_files = array();
/**
* 構(gòu)造函數(shù)
*/
public function __construct() {
if( ! function_exists('mb_convert_encoding') ) {
throw new ConvertException('mbstring extension be required');
}
}
/**
* 設(shè)置需要轉(zhuǎn)換的目錄或者單文件
* @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;
}
/**
* 設(shè)置轉(zhuǎn)換前的編碼和要轉(zhuǎn)換到的編碼
* @param string $encode 轉(zhuǎn)換前的編碼
* @param string $encode 轉(zhuǎn)換到的編碼
* @return boolean
*/
public function setEncode($encode_from, $encode_to) {
$this->_from_encoding = $encode_from;
$this->_to_encoding = $encode_to;
return true;
}
/**
* 轉(zhuǎn)換編碼,根據(jù)是否是目錄的設(shè)置分別轉(zhuǎn)換
* @return boolean
*/
public function convert() {
if($this->_directory ) {
return $this->_convertDirectory();
}
return $this->_convertFile();
}
/**
* 轉(zhuǎn)換文件
* @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;
}
/**
* 轉(zhuǎn)換目錄
* @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 可讀寫時(shí)返回true,否則返回false
*/
private function _isWR() {
if( is_readable($this->_path) && is_writable($this->_path) ) {
return true;
}
return false;
}
/**
* 遍歷目錄,找出所有文件,加上絕對(duì)路徑
* @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;
}
}
/**
* 轉(zhuǎn)換異常
*
*/
class ConvertException extends Exception {
}
- PHP自動(dòng)重命名文件實(shí)現(xiàn)方法
- php 隨機(jī)數(shù)的產(chǎn)生、頁面跳轉(zhuǎn)、件讀寫、文件重命名、switch語句
- fckeditor php上傳文件重命名的設(shè)置
- 6種php上傳圖片重命名的方法實(shí)例
- 基于php上傳圖片重命名的6種解決方法的詳細(xì)介紹
- PHP批量修改文件名稱的方法分析
- php實(shí)現(xiàn)批量修改文件名稱的方法
- PHP實(shí)現(xiàn)批量修改文件后綴名的方法
- php中批量修改文件后綴名的函數(shù)代碼
- php中批量替換文件名的實(shí)現(xiàn)代碼
- PHP實(shí)現(xiàn)批量重命名某個(gè)文件夾下所有文件的方法
相關(guān)文章
在laravel5.2中實(shí)現(xiàn)點(diǎn)擊用戶頭像更改頭像的方法
今天小編就為大家分享一篇在laravel5.2中實(shí)現(xiàn)點(diǎn)擊用戶頭像更改頭像的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10layui框架實(shí)現(xiàn)文件上傳及TP3.2.3(thinkPHP)對(duì)上傳文件進(jìn)行后臺(tái)處理操作示例
這篇文章主要介紹了layui框架實(shí)現(xiàn)文件上傳及TP3.2.3對(duì)上傳文件進(jìn)行后臺(tái)處理操作,結(jié)合實(shí)例形式分析了layui框架結(jié)合thinkPHP進(jìn)行文件上傳與處理操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-05-05PHP strcmp()和strcasecmp()的區(qū)別實(shí)例
這篇文章主要介紹了PHP strcmp()和strcasecmp()的區(qū)別實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-11-11php生成二維碼不保存服務(wù)器還有下載功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了php生成二維碼不保存服務(wù)器還有下載功能的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08PHP laravel中的多對(duì)多關(guān)系實(shí)例詳解
數(shù)據(jù)表之間是縱橫交叉、相互關(guān)聯(lián)的,laravel的一對(duì)一,一對(duì)多比較好理解,本文重點(diǎn)通過實(shí)例給大家講解 laravel中的多對(duì)多關(guān)系,感興趣的朋友一起看看吧2017-06-06Laravel框架數(shù)據(jù)庫CURD操作、連貫操作總結(jié)
這篇文章主要介紹了Laravel框架數(shù)據(jù)庫CURD操作、連貫操作、鏈?zhǔn)讲僮骺偨Y(jié),本文包含大量數(shù)據(jù)庫操作常用方法,需要的朋友可以參考下2014-09-09tp5框架使用cookie加密算法實(shí)現(xiàn)登錄功能示例
這篇文章主要介紹了tp5框架使用cookie加密算法實(shí)現(xiàn)登錄功能,結(jié)合實(shí)例形式分析了thinkPHP5使用cookie加密算法的原理及登錄功能相關(guān)操作技巧,需要的朋友可以參考下2020-02-02