PHP read file as an array of bytes
I believe the code you are looking for is:
$byteArray = unpack("N*",file_get_contents($filename));
UPDATE: Working code supplied by OP
$filename = "myFile.sav";
$handle = fopen($filename, "rb");
$fsize = filesize($filename);
$contents = fread($handle, $fsize);
$byteArray = unpack("N*",$contents);
print_r($byteArray);
for($n = 0; $n < 16; $n++)
{
echo $byteArray [$n].'<br/>';
}
how to convert the file content to byte array with php
PHP doesn't have a "byte array" data type. What it has is a string type, which is a byte array for all intents and purposes. To read the binary content of a file into a variable which is as close to a byte array as you'll ever get in PHP, do:
$content = file_get_contents($_FILES['my_file']['tmp_name']);
Yup, that's it. Nothing more to do.
I'm not particular familiar with the sqlsrv API, but perusing its documentation it appears that you can (need to?) set a flag this way to flag the data as being binary:
sqlsrv_query($conn, 'INSERT INTO files (file_data) VALUES (?)', array(
array($content, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING, SQLSRV_SQLTYPE_BINARY)
));
Get part of a file by byte number in php
Use fseek()
and fread()
$fp = fopen('somefile.txt', 'r');
// move to the 7th byte
fseek($fp, 7);
$data = fread($fp, 8); // read 8 bytes from byte 7
fclose($fp);
read single byte from PHP uploaded file
Thank you very much for the "ultimate goal" -- it will help us make sure that you don't do something silly by accident.
Encryption is a very complex topic. You should not write your own encryption routine. Use a complete, pre-built solution.
Mcrypt is PHP's best encryption extension. Mcrypt supports multiple common ciphers including AES ("Rijndael"). Encryption is pretty darn easy -- use "CBC" mode for your file.
The Vigenère cipher is a 460-ish year old substitution cipher based on a simple lookup table.
This explains the whole one-character-at-a-time thing.
While you can read the whole file one byte at a time, you might find it more convenient to work with a string. You can read the entire file into a variable (through file_get_contents
or whatever), and then use substr
or the square bracket substring syntax to read the string one byte at a time. Remember, PHP strings are just simple lists of bytes.
Given $data
is the complete data in the file, you can process every byte thusly:
$length = strlen($data);
for($i = 0; $i < $length; $i++) {
$byte = $data[$i];
// Or
$byte = substr($data, $i, 1);
// Process the byte here.
}
This is only practical for small files (smaller than a few megabytes), as it requires loading the whole file into memory.
I want to print out the acscii (HEX) code for this byte
You can get the decimal value using ord
, then convert that to a hexadecimal string using dechex
:
$hex_value = dechex(ord($byte));
Post-substitution, you can reverse it -- use hexdec
to go from hex to decimal, then chr
to go from decimal to byte:
$converted_byte = chr(hexdec($ciphered_hex));
PHP Read Byte Range from String or File
You should be able to do this with fseek
and fread
.
<?php
$byteOffset = 1024;
$readLength = 256;
$fileHandle = fopen('myfile', 'r');
fseek($fileHandle, $byteOffset);
$bytes = fread($fileHandle, $readLength);
how to covert xml file to byte array in php
A variation of this question has been answered before.
See the approved answer here:
String to byte array in php
For completeness, I'll put this in the context of your question:
$xmlStr = file_get_contents('sample.xml'); // read file to string
$byte_array = unpack('C*', $xmlStr); // convert string to byte array
var_dump($byte_array); // output your resulting byte array
See the PHP documentation for details on unpack: http://php.net/manual/en/function.unpack.php
Edit: I saw in a comment you mentioned this method didn't work. Attached are my results from a small test:
Related Topics
Flat PHP Array to Hierarchy Tree
How to Check for Special Characters PHP
Next Business Day of Given Date in PHP
Inet_Aton() and Inet_Ntoa() in PHP
Laravel Form HTML with Put Method for Put Routes
Return One Value from Database with MySQL PHP Pdo
Sending Data Along with a Redirect in Codeigniter
Laravel 5.2: the Process Class Relies on Proc_Open, Which Is Not Available on Your PHP Installation
How to Find Annotations in a PHP5 Object
How to Get Jquery Code Completion in Netbeans
How to Get Random Image from Directory Using PHP
Handling Multiple File Uploads in Sonata Admin Bundle
Allow Users to Download Files Outside Webroot
How to Parse Soap Response Without Soapclient