function _get_headers( $arr )
        {
                if( substr($arr['URL'],0,7) != 'http://' )
                {
                        return FALSE;
                }

                $arr['URL'] = explode('/',$arr['URL']);

                $HOST = $arr['URL'][2];

                $FILE = '';

                if( count($arr['URL']) > 3 )
                {
                        foreach( array_splice($arr['URL'],3) as $v )
                        {
                                $FILE .= '/'.$v;
                        }
                }
                else
                {
                        $FILE = $arr['URL'][2];
                }

                $socket = socket_create(AF_INET, SOCK_STREAM, 
SOL_TCP);

                if ($socket < 0)
                {
                        return FALSE;           
                }

                if ( socket_connect($socket, gethostbyname($HOST), 
getservbyname('www', 'tcp')) < 0)
                {
                        return FALSE;
                }

                $in  = "HEAD ".$FILE." HTTP/1.1\r\n";
                $in .= "Host: ".$HOST."\r\n";
                $in .= "Connection: Close\r\n\r\n";

                socket_write($socket, $in, strlen($in));

                $result = trim(socket_read($socket, 1024));
                $result = explode("\n",$result);                

                socket_close($socket);

                if( !ereg("HTTP/1.1 200 OK",$result[0]) )
                {
                        return FALSE;
                }

                if( $arr['check'] )
                {
                        return TRUE;
                }

                if( $arr['mode'] )
                {
                        foreach( $result as $val )
                        {
                                $t = explode(":",$val,2);

                                if( count($t) == 1 )
                                {
                                        $header[0] = trim($t[0]);
                                }
                                else
                                {
                                        $header[$t[0]] = trim($t[1]);
                                }
                        }
                }
                else
                {
                        $header = $result;
                }

                return $header;
        }

Exemple:
Check file exist?

if( _get_headers( array
('http://www.example.com/testfile.txt','check' => 1) ) )
{
    print "File exist";
}
else
{
    Print "File not exist!";
}

Get filesize:

$header = _get_headers( array('URL' 
=> 'http://www.example.com/testfile.txt', 'mode' => 1) );

if( $header )
{
   Print "Filesize is: ".$header['Content-length'];
}








------------------------ Yahoo! Groups Sponsor --------------------~--> 
Something is new at Yahoo! Groups.  Check out the enhanced email design.
http://us.click.yahoo.com/SISQkA/gOaOAA/yQLSAA/HKFolB/TM
--------------------------------------------------------------------~-> 

Community email addresses:
  Post message: [email protected]
  Subscribe:    [EMAIL PROTECTED]
  Unsubscribe:  [EMAIL PROTECTED]
  List owner:   [EMAIL PROTECTED]

Shortcut URL to this page:
  http://groups.yahoo.com/group/php-list 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-list/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to