Created
January 29, 2013 16:46
-
-
Save beezee/4665697 to your computer and use it in GitHub Desktop.
Shows how to "extend" CUploadedFile (difficult to do classically because of private static $_files property) using a small Factory component and a behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Class to provide factory methods for seamless use | |
class UploadedFile extends CComponent | |
{ | |
public function init() | |
{ | |
parent::init(); | |
} | |
public static function getInstanceByName($name) | |
{ | |
$file = CUploadedFile::getInstanceByName($name); | |
$file->attachBehavior('uploadedFile', new UploadedFileBehavior()); | |
return $file; | |
} | |
} | |
//Actual behavior class | |
class UploadedFileBehavior extends CBehavior | |
{ | |
public function init() | |
{ | |
parent::init(); | |
} | |
private function incrementFileName($file) | |
{ | |
//do stuff to ensure filename is unique in target folder and return modified path | |
} | |
public function saveAsUnique($file, $deleteTempFile=true) | |
{ | |
if (file_exists($file)) | |
return $this->saveAsUnique($this->incrementFileName($file), $deleteTempFile); | |
return $this->owner->saveAs($file, $deleteTempFile); | |
} | |
} | |
//example use | |
$file = UploadedFile::getInstanceByName('upload') | |
$file->saveAsUnique('/destination/path/and/file.jpg'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing, this helped me out today.