问题
What is PHP for C# (asuming we open some local (on server) file instead of OpenFileDialog
private const int HEADER_LENGTH = 13;
stream = File.OpenRead(openFileDialog.FileName);
header = ReadBytes(stream, HEADER_LENGTH);
And will we be able to do something like this in PHP as a next step
private const byte SIGNATURE1 = 0x46;
private const byte SIGNATURE2 = 0x4C;
private const byte SIGNATURE3 = 0x56;
if ((SIGNATURE1 != header[0]) || (SIGNATURE2 != header[1]) || (SIGNATURE3 != header[2]))
throw new InvalidDataException("Not a valid FLV file!.");
回答1:
Hmm, I think you look for something like that
$handle = fopen(FILE, 'r');
if ($handle)
{
$head = fread ( $handle , 13 );
if ($head[0] != chr (0x46)) ...
...
}
Of course you can create constants for this signature, but this way:
define('SIG1', chr(0x46));
then you can use them as normal: $head[0] == SIG1
etc. You can use functions when defining constants, for both constant names and values.
回答2:
Use fopen and fread:
$fh = fopen($filename, "r");
if ($fh) {
$data = fread($fh, 13);
}
PHP supports the []-operator on strings, so you will be able to validate the signature in basically the same way as you did in C#.
来源:https://stackoverflow.com/questions/3070298/what-is-php-for-c-sharp-readbytesstream-langth