Current Location: Home> Latest Articles> How to Change File Encoding in PHP? Detailed Methods and Code Examples

How to Change File Encoding in PHP? Detailed Methods and Code Examples

gitbox 2025-06-28

How to Change File Encoding in PHP

When working with text files, file encoding is crucial because it determines the character set and encoding method of the file. If the encoding format is incorrect, it can lead to garbled text or make the file unreadable.

PHP is a widely used server-side programming language that provides powerful functions to manipulate files. In this article, we will explore how to change file encoding in PHP.

Detecting File Encoding

Before changing a file's encoding format, it's important to first determine the current encoding format of the file. PHP provides the mb_detect_encoding() function to detect the file's encoding format.


$file = 'example.txt';
$encoding = mb_detect_encoding(file_get_contents($file));
echo "File encoding: " . $encoding;

This code will output the file's encoding format. Common encoding formats include UTF-8, GBK, ISO-8859-1, etc. Based on the detected encoding, you can decide whether or not you need to convert the file's encoding.

Changing File Encoding

If you need to change the file's encoding format, you can use PHP's iconv() function. This function allows you to convert a string from one encoding to another.


$file = 'example.txt';
$content = file_get_contents($file);
$newContent = iconv('GB2312', 'UTF-8', $content);
file_put_contents($file, $newContent);
echo "File encoding has been changed."

This code converts the file's encoding from GB2312 to UTF-8 and writes the modified content back to the file. You can modify the parameters in the iconv() function to implement different encoding conversions based on your needs.

Batch Converting File Encoding in a Folder

If you need to batch convert all files in a folder, you can use a recursive method to traverse the folder and process each file individually. Below is an example PHP code for batch processing files in a folder:


$folder = 'example_folder';
function convertFilesInFolder($folder) {
    $files = scandir($folder);
    foreach ($files as $file) {
        if ($file != '.' && $file != '..') {
            $path = $folder . '/' . $file;
            if (is_dir($path)) {
                convertFilesInFolder($path);
            } else {
                $encoding = mb_detect_encoding(file_get_contents($path));
                if ($encoding != 'UTF-8') {
                    $content = file_get_contents($path);
                    $newContent = iconv($encoding, 'UTF-8', $content);
                    file_put_contents($path, $newContent);
                }
            }
        }
    }
}
convertFilesInFolder($folder);
echo "All files in the folder have been converted."

This code recursively traverses all files in the specified folder and checks the encoding of each file. If the file is not in UTF-8 encoding, it will be converted to UTF-8.

Conclusion

File encoding is crucial for text processing. PHP provides several powerful functions, such as mb_detect_encoding and iconv, to detect and convert file encodings. Whether you need to convert a single file or batch convert files in an entire folder, PHP can handle these tasks efficiently.

When working with file encoding, always back up your files to prevent data loss or file corruption due to errors during encoding conversion. Also, consider the semantic differences between different encodings to avoid introducing errors during the conversion process.