src/CLI/Downloader.php
public function downloadUrl( $url, $savePath = "", $force = FALSE )
	{
		if( getEnv( 'HTTP_HOST' ) )															//  called via Browser
			die( "Usage in Console, only." );

		$this->loadSize	= 0;																//  clear Size of current Load
		$this->fileSize	= 0;																//  clear Size og File to download
		$this->redirected	= FALSE;

		if( $savePath && !file_exists( $savePath ) )
			if( !@mkDir( $savePath, 0777, TRUE ) )
				throw new RuntimeException( 'Save path could not been created.' );

		$savePath	= $savePath ? preg_replace( "@([^/])$@", "\\1/", $savePath ) : "";		//  correct Path
		$parts		= parse_url( $url );													//  parse URL
		$info		= pathinfo( $parts['path'] );											//  parse Path
		$fileName	= $info['basename'];													//  extract File Name
		if( !$fileName )																	//  no File Name found in URL
			throw new RuntimeException( 'File Name could not be extracted.' );

		$this->fileUri	= $savePath.$fileName;												//  store full File Name
		$this->tempUri	= sys_get_temp_dir().$fileName.".part";								//  store Temp File Name
		if( file_exists( $this->fileUri ) )													//  File already exists
		{
			if( !$force )																	//  force not set
				throw new RuntimeException( 'File "'.$this->fileUri.'" is already existing.' );
			if( !@unlink( $this->fileUri ) )												//  remove File, because forced
				throw new RuntimeException( 'File "'.$this->fileUri.'" could not been cleared.' );
		}
		if( file_exists( $this->tempUri ) )													//  Temp File exists
			if( !@unlink( $this->tempUri ) )												//  remove Temp File
				throw new RuntimeException( 'Temp File "'.$this->tempUri.'" could not been cleared.' );

		if( $this->showFileName && $this->templateFileName )								//  show extraced File Name
			printf( $this->templateFileName, $fileName );									//  use Template
		
		$this->clock	= new Alg_Time_Clock;												//  start clock
		$ch = curl_init();																	//  start cURL
		curl_setopt( $ch, CURLOPT_URL, $url );												//  set URL in cURL Handle
		curl_setopt( $ch, CURLOPT_HEADERFUNCTION, array( $this, 'readHeader' ) );			//  set Callback Method for Headers
		curl_setopt( $ch, CURLOPT_WRITEFUNCTION, array( $this, 'readBody' ) );				//  set Callback Method for Body
		curl_exec( $ch );																	//  execute cURL Request

		$error	= curl_error( $ch );														//  get cURL Error
		if( $error )																		//  an Error occured
			throw new RuntimeException( $error, curl_errno( $ch ) );						//  throw Exception with Error

		return $this->loadSize;																//  return Number of loaded Bytes
	}
public function downloadUrl( $url, $savePath = "", $force = FALSE )
	{
		if( getEnv( 'HTTP_HOST' ) )															//  called via Browser
			die( "Usage in Console, only." );

		$this->loadSize	= 0;																//  clear Size of current Load
		$this->fileSize	= 0;																//  clear Size og File to download
		$this->redirected	= FALSE;

		if( $savePath && !file_exists( $savePath ) )
			if( !@mkDir( $savePath, 0777, TRUE ) )
				throw new RuntimeException( 'Save path could not been created.' );

		$savePath	= $savePath ? preg_replace( "@([^/])$@", "\\1/", $savePath ) : "";		//  correct Path
		$parts		= parse_url( $url );													//  parse URL
		$info		= pathinfo( $parts['path'] );											//  parse Path
		$fileName	= $info['basename'];													//  extract File Name
		if( !$fileName )																	//  no File Name found in URL
			throw new RuntimeException( 'File Name could not be extracted.' );

		$this->fileUri	= $savePath.$fileName;												//  store full File Name
		$this->tempUri	= sys_get_temp_dir().$fileName.".part";								//  store Temp File Name
		if( file_exists( $this->fileUri ) )													//  File already exists
		{
			if( !$force )																	//  force not set
				throw new RuntimeException( 'File "'.$this->fileUri.'" is already existing.' );
			if( !@unlink( $this->fileUri ) )												//  remove File, because forced
				throw new RuntimeException( 'File "'.$this->fileUri.'" could not been cleared.' );
		}
		if( file_exists( $this->tempUri ) )													//  Temp File exists
			if( !@unlink( $this->tempUri ) )												//  remove Temp File
				throw new RuntimeException( 'Temp File "'.$this->tempUri.'" could not been cleared.' );

		if( $this->showFileName && $this->templateFileName )								//  show extraced File Name
			printf( $this->templateFileName, $fileName );									//  use Template
		
		$this->clock	= new Alg_Time_Clock;												//  start clock
		$ch = curl_init();																	//  start cURL
		curl_setopt( $ch, CURLOPT_URL, $url );												//  set URL in cURL Handle
		curl_setopt( $ch, CURLOPT_HEADERFUNCTION, array( $this, 'readHeader' ) );			//  set Callback Method for Headers
		curl_setopt( $ch, CURLOPT_WRITEFUNCTION, array( $this, 'readBody' ) );				//  set Callback Method for Body
		curl_exec( $ch );																	//  execute cURL Request

		$error	= curl_error( $ch );														//  get cURL Error
		if( $error )																		//  an Error occured
			throw new RuntimeException( $error, curl_errno( $ch ) );						//  throw Exception with Error

		return $this->loadSize;																//  return Number of loaded Bytes
	}
public function downloadUrl ( $ url , $ savePath = "" , $ force = FALSE ) { if ( getEnv ( 'HTTP_HOST' ) ) //  called via Browser die ( "Usage in Console, only." ) ; $ this -> loadSize = 0 ; //  clear Size of current Load $ this -> fileSize = 0 ; //  clear Size og File to download $ this -> redirected = FALSE ; if ( $ savePath && ! file_exists ( $ savePath ) ) if ( ! @ mkDir ( $ savePath , 0777 , TRUE ) ) throw new RuntimeException ( 'Save path could not been created.' ) ; $ savePath = $ savePath ? preg_replace ( "@([^/])$@" , "\\1/" , $ savePath ) : "" ; //  correct Path $ parts = parse_url ( $ url ) ; //  parse URL $ info = pathinfo ( $ parts [ 'path' ] ) ; //  parse Path $ fileName = $ info [ 'basename' ] ; //  extract File Name if ( ! $ fileName ) //  no File Name found in URL throw new RuntimeException ( 'File Name could not be extracted.' ) ; $ this -> fileUri = $ savePath . $ fileName ; //  store full File Name $ this -> tempUri = sys_get_temp_dir ( ) . $ fileName . ".part" ; //  store Temp File Name if ( file_exists ( $ this -> fileUri ) ) //  File already exists { if ( ! $ force ) //  force not set throw new RuntimeException ( 'File "' . $ this -> fileUri . '" is already existing.' ) ; if ( ! @ unlink ( $ this -> fileUri ) ) //  remove File, because forced throw new RuntimeException ( 'File "' . $ this -> fileUri . '" could not been cleared.' ) ; } if ( file_exists ( $ this -> tempUri ) ) //  Temp File exists if ( ! @ unlink ( $ this -> tempUri ) ) //  remove Temp File throw new RuntimeException ( 'Temp File "' . $ this -> tempUri . '" could not been cleared.' ) ; if ( $ this -> showFileName && $ this -> templateFileName ) //  show extraced File Name printf ( $ this -> templateFileName , $ fileName ) ; //  use Template $ this -> clock = new Alg_Time_Clock ; //  start clock $ ch = curl_init ( ) ; //  start cURL curl_setopt ( $ ch , CURLOPT_URL , $ url ) ; //  set URL in cURL Handle curl_setopt ( $ ch , CURLOPT_HEADERFUNCTION , array ( $ this , 'readHeader' ) ) ; //  set Callback Method for Headers curl_setopt ( $ ch , CURLOPT_WRITEFUNCTION , array ( $ this , 'readBody' ) ) ; //  set Callback Method for Body curl_exec ( $ ch ) ; //  execute cURL Request $ error = curl_error ( $ ch ) ; //  get cURL Error if ( $ error ) //  an Error occured throw new RuntimeException ( $ error , curl_errno ( $ ch ) ) ; //  throw Exception with Error return $ this -> loadSize ; //  return Number of loaded Bytes }
Loads a File from an URL, saves it using Callback Methods and returns Number of loaded Bytes.
@access		public
@param		string		$url				URL of File to download
@param		string		$savePath			Path to save File to
@param		bool		$force				Flag: overwrite File if already existing
@return		int
Loads a File from an URL saves it using Callback Methods and returns Number of loaded Bytes .