I was wonder how do byte arrays work in php? Can anyone give me a simple example of getting the bytes of an online file then reading and writing the first few bytes? or something, please.
Printable View
I was wonder how do byte arrays work in php? Can anyone give me a simple example of getting the bytes of an online file then reading and writing the first few bytes? or something, please.
PHP deals with file data as strings. That does not however mean that you cannot access the byte values, because a string is an array of bytes. You can read a specific number of bytes using the fread/fwrite function with the file open binary, read or write mode.
You can then convert the string to an array of characters using the str_split function.
PHP Code:
$hwnd = fopen('/path/to/file', 'rb');
$data = fread($hwnd, 1024);
$data = str_split($data);
I saw something like
$str{10} to access the 10th character. Does that work too?
Yes you can do that too.
Yes, but that is the eleventh character, not the tenth.