views:

24

answers:

2

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!.");
A: 

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#.

Emil H
+1  A: 

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.

Tomasz Struczyński
When it comes to class constants, they are defined similar to your code, http://pl.php.net/manual/en/language.oop5.constants.php
Tomasz Struczyński