All pastes #355371 Raw Edit

Mine

public php v1 · immutable
·published 2007-02-14 13:15 UTC
rendered paste body
<?php/** * CurlUpload * * Saves files of any type to server.  * Valid mime types set using $obj->addUploadType( $mime, $ext ) * * Copyright 2006 Andrew Krespanis * * @author  Andrew Krespanis * @license http://www.opensource.org/licenses/mit-license.php **/class CurlUpload {		const ERR_FILE_NOT_FOUND = 1; // 404 or similar - could not fetch result	const ERR_INVALID_MIME = 2; // mime type of downloaded file isn't allowed	const ERR_SAVE_ERROR = 3; // could not write file to server	const ERR_FILE_ALREADY_EXISTS = 4; // file already exists	private $result;	private $error = 0;	private $validTypes;	private $mimeType;	private $overwriteExisting;	private $resultFile;		function __construct ( )	{		$this->validTypes = array();	}	public function addFileType ($mime, $extension)	{	  $this->validTypes[$mime] = $extension;	}	  public function getFileName ()  {    return $this->resultFile;  }		/**	 * Get File from URL	 *	 * @param   String  $url  URL of file to upload   * @param   String  $saveName       Name to use when saving file to server,                                      without extension. Extension is determined by mime type   * @param   String  $saveDirectory  Location on server to save file   **/	public function getFromUrl ($url, $saveDirectory, $saveName, $overwriteExisting = false)	{	  if (!is_writable($saveDirectory)) {	    $this->error = self::ERR_SAVE_ERROR;	    return;  	  }	  $url = $this->addUrlProtocol( $url );	  $handle = curl_init();		curl_setopt( $handle, CURLOPT_URL, $url );		curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 3 );		curl_setopt( $handle, CURLOPT_RETURNTRANSFER, 1 );		$this->result = curl_exec( $handle );		if( empty( $this->result ) )		{			$this->error = self::ERR_FILE_NOT_FOUND;		} 		else 		{			$mime = curl_getinfo ( $handle, CURLINFO_CONTENT_TYPE );			$valid = isset($this->validTypes[$mime]);			if (!$valid) $this->error = self::ERR_INVALID_MIME;		}		curl_close($handle);    if( $this->error != 0 ) return;    $this->resultFile = $saveName . '.' . $this->validTypes[$mime];    $filename = $saveDirectory . $this->resultFile;    if (!$this->isSafeToWrite( $filename, $overwriteExisting ) )     {      $this->error = self::ERR_FILE_ALREADY_EXISTS;      return;    }    file_put_contents( $filename, $this->result );	}	  public function getError ()  {    return $this->error;  }		private function addUrlProtocol ( $url )	{		$url = trim($url);		if ( ( strpos($url, 'http://') != 0 ) and ( strpos($url, 'https://')  != 0 ) )		{			$url = 'http://'.$url;		}		return $url;	}		private function isSafeToWrite ( $filename, $overwrite )	{	 if ($overwrite) return true;	 if (file_exists( $filename ) ) return false;	 return true;	}	}/**********************\        USAGE\**********************/$c = new CurlUpload;$c->addFileType( 'image/png', 'png' );$c->addFileType( 'image/jpeg', 'jpg' );$c->addFileType( 'image/pjpeg', 'jpg' );$c->addFileType( 'image/gif', 'gif' );$c->getFromUrl( 'localhost/timwouldlickit/images/tim.png',                 '/Users/andrew/Sites/timwouldlickit/images/',                'testUpload', true);if ($c->getError() != false){  switch ( $c->getError() )  {    case CurlUpload::ERR_FILE_NOT_FOUND :      $msg = 'Unable to fetch URL';    break;        case CurlUpload::ERR_INVALID_MIME:      $msg = 'URL specified points to a file of a type that is not allowed';    break;            case CurlUpload::ERR_SAVE_ERROR:      $msg = 'Could not write file to server. Check your permission settings';    break;        case CurlUpload::ERR_FILE_ALREADY_EXISTS:      $msg = 'File name already exists';    break;  }  echo '<p>'.$msg.'</p>';} else {  echo '<p>File successfully saved to server</p>';  echo '<img src="../images/'. $c->getFileName() .'" />';}?>