将文件读入字节数组(PHP) [英] Reading a File into a Byte Array (PHP)

查看:133
本文介绍了将文件读入字节数组(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件.但现在需要将此文件读入一个字节数组.在Java或C ++中,这样做非常容易.但找不到如何在PHP中阅读.

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.

推荐答案

您可以将文件读取为这样的字符串:

You can read the file into a string like this:

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

您可以获得的单个字节类似于在C中的情况:

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";
}

参考文献:

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

这篇关于将文件读入字节数组(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆