Scripteen - Web Scripts - Forum: Curl Support For Url Uploading - Scripteen - Web Scripts - Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Curl Support For Url Uploading

#1 User is offline   ptejada Icon

  • Newbie
  • Pip
  • Group: Members
  • Posts: 9
  • Joined: 09-March 10

Posted 09 March 2010 - 09:58 PM

The only downside of my host is that it has allow_url_fopen and allow_url_include disabled and i've learned to just live with it.

So i've implemented a snippet from one of my functions in the uploaderurl.php file to add cURL support while URL uploading. For my own use i removed the whole download function in uploaderurl.php and replace to only use cURL. However cURL support could also be implemented so that if the original(fopen) method fails then use cURL. cURL has proven to be more efficient and faster so it could be set as the default method.
Below are the different versions of the download function located at the bottom of the uploaderurl.php file (line 671):

For "If URL fopen fails use cURL":
function download ($file_source, $file_target)

{

  // Preparations

  $file_source = str_replace(' ', '%20', html_entity_decode($file_source)); // fix url format

  if (file_exists($file_target)) { chmod($file_target, 0777); } // add write permission        

  // Begin transfer
	
  if (($rh = fopen($file_source, 'rb')) === FALSE) { 
  
	// fopen Failed Using cURL instead
		$fp = fopen ($file_target, 'w+');
		$ch = curl_init($file_source);
		curl_setopt($ch, CURLOPT_TIMEOUT, 50);
		curl_setopt($ch, CURLOPT_FILE, $fp);
		$good = curl_exec($ch);
		curl_close($ch);
		fclose($fp);
		if($good == true){
			return true;
		}else{
			return false; // cURL download Failed
		}  
  } 

  if (($wh = fopen($file_target, 'wb')) === FALSE) { return false; } // error messages.

  while (!feof($rh)){
    // unable to write to file, possibly because the harddrive has filled up
    if (fwrite($wh, fread($rh, 1024)) === FALSE) { fclose($rh); fclose($wh); return false; }
  }



  // Finished without errors

  fclose($rh);

  fclose($wh);

  return true;

}



For " if cURL fails use URL fopen":
function download ($file_source, $file_target)

{

  // Preparations

  $file_source = str_replace(' ', '%20', html_entity_decode($file_source)); // fix url format

  if (file_exists($file_target)) { chmod($file_target, 0777); } // add write permission        

  // Begin transfer
	// Using cURL 
	$fp = fopen ($file_target, 'w+');
	$ch = curl_init($file_source);
	curl_setopt($ch, CURLOPT_TIMEOUT, 50);
	curl_setopt($ch, CURLOPT_FILE, $fp);
	$good = curl_exec($ch);
	curl_close($ch);
	fclose($fp);
	if($good == true){
		return true;
	}else{
	 	//Use URL fopen
		if (($rh = fopen($file_source, 'rb')) === FALSE) { return false; } // fopen handles
		
		if (($wh = fopen($file_target, 'wb')) === FALSE) { return false; } // error messages.
		
		while (!feof($rh)){
		// unable to write to file, possibly because the harddrive has filled up
		if (fwrite($wh, fread($rh, 1024)) === FALSE) { fclose($rh); fclose($wh); return false; }
		}
		
		// Finished without errors
		
		fclose($rh);
		
		fclose($wh);
		
		return true;
		
	}
} 


0

#2 User is offline   SMLMATS Icon

  • Member
  • PipPip
  • Group: Members
  • Posts: 11
  • Joined: 22-April 10

Posted 23 April 2010 - 12:15 PM

Thanks man, cuz of this edit, my link uploading start workin.
Thanks a lot.
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users