Reading a File into a Byte Array (PHP)

前端 未结 4 1742
無奈伤痛
無奈伤痛 2020-12-11 05:42

I have one file. but now need to read this file into a bytes array. In java or c++ it\'s very easy to do that. but not found how i can read in PHP.

4条回答
  •  感动是毒
    2020-12-11 06:37

    You can read the file into a string like this:

    $data = file_get_contents("/tmp/some_file.txt");
    

    You can get at the individual bytes similar to how you would in C:

    for($i = 0; $i < strlen($data); ++$i) {
        $char = $data[$i];
        echo "Byte $i: $char\n";
    }
    

    References:

    • http://php.net/file_get_contents
    • http://php.net/manual/en/language.types.string.php#language.types.string.substr

提交回复
热议问题