Hello, I need to get remote file size through the url to a function. The problem is that I'm not getting! Trying with the functions of libcURL, but I am not getting. Is there any way to get the size of a remote file by url?

Recommended Answers

All 2 Replies

#include <iostream>
#include <cstdio>
#include <curl/curl.h>
#include <boost/regex.hpp>
using namespace boost;
using namespace std;

//
// For this demonstration, the contents of the web page are put
// into this global variable.
//
string contents;
//
// This is the callback function that is called by
// curl_easy_perform(curl)
//
size_t handle_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
	const boost::regex content_length_regex("Content-Length: [0-9]{1,}"); // Regex do video do youtube...
	const boost::regex content_length_remove_regex("Content-Length: ");
	int numbytes = size*nmemb;
	// The data is not null-terminated, so get the last character, and replace
	// it with '\0'.
	char lastchar = *((char *) ptr + numbytes - 1);
	//string last_char = char_to_string(lastchar);
	  //      {
				string nothing = "";
	      //    contents = regex_replace(last_char, content_length_remove_regex, nothing);
			*((char *) ptr + numbytes - 1) = '\0';
			string last_char = ((char *)ptr);
			//cout << last_char << endl;
			//cout << endl;
			//cout << regex_search(last_char,content_length_regex) << endl;
			//cout << endl;
			//cout << "antes" << endl;
			//cout << ((char*)ptr) << endl;
			if (regex_search(last_char,content_length_regex) == 1) // Se for 1, foi retornado sim para o match
			{
				//cout << "depois" << endl;
				//cout << ((char*)ptr) << endl;
				//cout << regex_replace(last_char, content_length_remove_regex, nothing) << endl;
				contents = regex_replace(last_char, content_length_remove_regex, nothing);
				//cout << contents << endl;
			}
			else
			//{
				//contents = "-1";
			//}
			//contents.append((char *)ptr);
			//contents.append(1,lastchar);
			//*((char *) ptr + numbytes - 1) = lastchar;  // Might not be necessary.
//	 }


	return size*nmemb;
}

long remote_filesize(string url)
{
	//string url = "http://www.youtube.com/get_video?video_id=3455GI_uGs4&t=vjVQa1PpcFNWk-V5Ndo6HE9KWZGtRxrct9PNWGe5Nhs=&el=detailpage&ps=&fmt=34";
//string url = "http://www.google.com";
CURL *curl;
  CURLcode res;
  double length = 0.0;
  //double length2 = 0.0;
  curl = curl_easy_init();
  if(curl) {
    //static const char *headerfilename = "head.out";
    //FILE *headerfile;
    //headerfile = fopen(headerfilename,"w");
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
    //curl_easy_setopt(curl, CURLOPT_RANGE, "0-4");                   // só por garantia, não faz o donwload inteiro não...
    curl_easy_setopt(curl, CURLOPT_NOBODY, 1);                      // acho que é aqui que bloqueia o donwload...
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, handle_data);
    curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 500);
    res = curl_easy_perform(curl);

    //curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &length);
	//cout << length << endl;
	//const boost::regex youtube_video_id("^video_id="); // Regex do video do youtube...

    //cout << contents << endl;
    //unlink(headerfilename);
  }
  curl_easy_cleanup(curl);
  //return length;
  if (contents == "")
  {
	  return(-1);
  }
  else
  {
	  return atol(contents.c_str());
  }
}

int main()
{
	// Content-Length: 222
	string url = "http://www.google.com.br";
	remote_filesize(url);
	cout << "contents " << contents << endl;
	return 0;
}

g++ example.cpp -lcurl -lboost_regex
Sorry for the comments in Portuguese and snippets of code. I had to use the libboost_regex because I still do not know another way to do a search for regular expression.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.