如何将文件中的所有字符转换为php中的ascii数字

I want to covert all the characters in a file to ASCII code in php? I know of ord function but whether there is any function that will do for the entire file?

iconv may do the work

http://php.net/manual/de/function.iconv.php

it convertes chars of a specified charset in a string to another one. look at the //TRANSLIT and //IGNORE specials for chars that cannot be converted 1:1.

to get the file in a string you can use file_get_contents and save it after iconv etc. is applied with file_put_contents.

$inputFile = fopen("input.txt", "rb");
$outputFile = fopen("output.txt", "w+");

while (!feof($inputFile)) {
    $inputBlock = fread($inputFile, 8192);
    $outputBlock = '';
    $inputLength = strlen($inputBlock);
    for ($i = 0; $i < $inputLength; ++$i) {
        $outputBlock .= str_pad(dechex(ord($inputBlock{$i})),2,'0',STR_PAD_LEFT);
    }
    fwrite($outputFile,$outputBlock);
}

fclose($inputFile);
fclose($outputFile);