Get Remote filesize Using cURL + PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Get Remote filesize Using cURL + PHP

    PHP Code:
    // Create a curl connection
    $chGetSize curl_init();

    // Set the url we're requesting
    curl_setopt($chGetSizeCURLOPT_URL"http://www.example.com/file.exe");

    // Set a valid user agent
    curl_setopt($chGetSizeCURLOPT_USERAGENT"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11");

    // Don't output any response directly to the browser
    curl_setopt($chGetSizeCURLOPT_RETURNTRANSFERtrue);

    // Don't return the header (we'll use curl_getinfo();
    curl_setopt($chGetSizeCURLOPT_HEADERfalse);

    // Don't download the body content
    curl_setopt($chGetSizeCURLOPT_NOBODYtrue);

    // Run the curl functions to process the request
    $chGetSizeStore curl_exec($chGetSize);
    $chGetSizeError curl_error($chGetSize);
    $chGetSizeInfo curl_getinfo($chGetSize);

    // Close the connection
    curl_close($chGetSize);// Print the file size in bytes

    echo $chGetSizeInfo['download_content_length']; 
    Unamos los corazones,hoy todos somos multicolores!

    #2
    also you can use php 'get_headers()' function.
    PHP Code:
    <?php
    $url 
    'http://www.example.com';
    print_r(get_headers($url1));
    ?>
    out put:
    PHP Code:
    Array
    (
        [
    0] => HTTP/1.1 200 OK
        
    [Date] => Sat29 May 2004 12:28:14 GMT
        
    [Server] => Apache/1.3.27 (Unix)  (Red-Hat/Linux)
        [
    Last-Modified] => Wed08 Jan 2003 23:11:55 GMT
        
    [ETag] => "3f80f-1b6-3e1cb03b"
        
    [Accept-Ranges] => bytes
        
    [Content-Length] => 438
        
    [Connection] => close
        
    [Content-Type] => text/html


    Free Mobile Web Scripts by me: Free Youtube Downloader, Tweets Reader, Facebook Wall Posts Reader
    PHP Tutorials: How to Secure Your PHP Script (PHP SECURITY)
    Want to Develop/Edit your WAP/Web Site? Add me to Gtalk (gmail) 'lakshan1989' or PM me.

    Comment


      #3
      hy , thanks for reply !

      I always like to send valide headers, at least USERAGENT when I take something from other site .
      Unamos los corazones,hoy todos somos multicolores!

      Comment


        #4
        PHP Code:
        /**
         * Returns the size of a file without downloading it, or -1 if the file
         * size could not be determined.
         *
         * @param $url - The location of the remote file to download. Cannot
         * be null or empty.
         *
         * @return The size of the file referenced by $url, or -1 if the size
         * could not be determined.
         */
        function curl_get_file_size$url ) {
          
        // Assume failure.
          
        $result = -1;

          
        $curl curl_init$url );

          
        // Issue a HEAD request and follow any redirects.
          
        curl_setopt$curlCURLOPT_NOBODYtrue );
          
        curl_setopt$curlCURLOPT_HEADERtrue );
          
        curl_setopt$curlCURLOPT_RETURNTRANSFERtrue );
          
        curl_setopt$curlCURLOPT_FOLLOWLOCATIONtrue );
          
        curl_setopt$curlCURLOPT_USERAGENTget_user_agent_string() );

          
        $data curl_exec$curl );
          
        curl_close$curl );

          if( 
        $data ) {
            
        $content_length "unknown";
            
        $status "unknown";

            if( 
        preg_match"/^HTTP\/1\.[01] (\d\d\d)/"$data$matches ) ) {
              
        $status = (int)$matches[1];
            }

            if( 
        preg_match"/Content-Length: (\d+)/"$data$matches ) ) {
              
        $content_length = (int)$matches[1];
            }

            
        // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
            
        if( $status == 200 || ($status 300 && $status <= 308) ) {
              
        $result $content_length;
            }
          }

          return 
        $result;

        PHP Code:
        function retrieve_remote_file_size($url){
             
        $ch curl_init($url);

             
        curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
             
        curl_setopt($chCURLOPT_HEADERTRUE);
             
        curl_setopt($chCURLOPT_NOBODYTRUE);

             
        $data curl_exec($ch);
             
        $size curl_getinfo($chCURLINFO_CONTENT_LENGTH_DOWNLOAD);

             
        curl_close($ch);
             return 
        $size;

        PHP Code:
        function remote_file_size($url){
            
        $head "";
            
        $url_p parse_url($url);

            
        $host $url_p["host"];
            if(!
        preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$host)){

                
        $ip=gethostbyname($host);
                if(!
        preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$ip)){

                    return -
        1;
                }
            }
            if(isset(
        $url_p["port"]))
            
        $port intval($url_p["port"]);
            else
            
        $port    =    80;

            if(!
        $port$port=80;
            
        $path $url_p["path"];

            
        $fp fsockopen($host$port$errno$errstr20);
            if(!
        $fp) {
                return 
        false;
                } else {
                
        fputs($fp"HEAD "  $url  " HTTP/1.1\r\n");
                
        fputs($fp"HOST: " $host "\r\n");
                
        fputs($fp"User-Agent: http://www.example.com/my_application\r\n");
                
        fputs($fp"Connection: close\r\n\r\n");
                
        $headers "";
                while (!
        feof($fp)) {
                    
        $headers .= fgets ($fp128);
                    }
                }
            
        fclose ($fp);

            
        $return = -2;
            
        $arr_headers explode("\n"$headers);
            foreach(
        $arr_headers as $header) {

                
        $s1 "HTTP/1.1";
                
        $s2 "Content-Length: ";
                
        $s3 "Location: ";

                if(
        substr(strtolower ($header), 0strlen($s1)) == strtolower($s1)) $status substr($headerstrlen($s1));
                if(
        substr(strtolower ($header), 0strlen($s2)) == strtolower($s2)) $size   substr($headerstrlen($s2));
                if(
        substr(strtolower ($header), 0strlen($s3)) == strtolower($s3)) $newurl substr($headerstrlen($s3));  
            }

            if(
        intval($size) > 0) {
                
        $return=intval($size);
            } else {
                
        $return=$status;
            }

            if (
        intval($status)==302 && strlen($newurl) > 0) {

                
        $return remote_file_size($newurl);
            }
            return 
        $return;

        www.inbuzunar.mobi - Your mobile portal pocket

        Comment

        Working...
        X