Skip to content

Instantly share code, notes, and snippets.

@beezee
Created January 29, 2013 16:46
Show Gist options
  • Save beezee/4665697 to your computer and use it in GitHub Desktop.
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
<?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');
@jlyman
Copy link

jlyman commented Apr 12, 2013

Thanks for sharing, this helped me out today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment