What is PHP for C# ReadBytes(stream langth)?

眉间皱痕 提交于 2019-12-11 04:14:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!