Skip to content

Instantly share code, notes, and snippets.

@hejrobin
Created August 5, 2011 13:39
Show Gist options
  • Save hejrobin/1127555 to your computer and use it in GitHub Desktop.
Save hejrobin/1127555 to your computer and use it in GitHub Desktop.
<?php
class UploadrException extends Exception {}
class Uploadr {
public $upload_path;
protected $max_file_size;
public $max_file_count = 0;
protected $file_type_filter;
protected $files;
public $batch_sort_files = false;
public $warn_on_duplecate_file = false;
public function __construct($upload_path = null) {
$this->setUploadPath($upload_path);
$this->setMaxFileSize();
$this->setFileTypeFilter();
}
public function setUploadPath($upload_path = null) {
if(is_dir($upload_path))
$this->upload_path = ($upload_path) ? $upload_path : '';
}
public function setMaxFileSize($max_file_size = 0) {
$max_file_size = ($max_file_size > 0) ? $max_file_size : 2 * 1024 * 1024;
if(is_int($max_file_size))
$this->max_file_size = $max_file_size;
}
public function setFileTypeFilter($file_type_filter = array()) {
if(is_array($file_type_filter))
$this->file_type_filter = $file_type_filter;
}
public function normalizeFileQueue($_files) {
foreach($_files as $batch => $segment) {
foreach($segment as $key => $file) {
foreach($file as $index => $value) {
if($this->batch_sort_files)
$files[$batch][$index][$key] = $value;
else
$files[$index][$key] = $value;
}
}
}
return $this->filterFileQueue($files);
}
protected function filterFileQueue($_files) {
if($this->batch_sort_files) {
foreach($_files as $batch => $files) {
foreach($files as $key => $file) {
if(empty($file['name']) && array_key_exists('name', $file))
$_files[$batch][$key] = array();
}
$_files[$batch] = array_filter($_files[$batch]);
}
} else {
foreach($_files as $key => $file) {
if(empty($file['name']) && array_key_exists('name', $file))
$_files[$key] = array();
}
$_files = array_filter($_files);
}
return $_files;
}
public function setFiles($_files) {
$this->files = $this->normalizeFileQueue($_files);
}
protected function getFile($key, $batch = null) {
if($this->batch_sort_files && $batch)
return $this->files[$batch][$key];
else
return $this->files[$key];
}
protected function validateFile($key, $batch = null) {
$file = (object) $this->getFile($key, $batch);
if(!is_uploaded_file($file->tmp_name))
throw new UploadrException('UPLOADR_INVALID_UPLOAD');
else {
if($file->size > $this->max_file_size)
throw new UploadrException('UPLOADR_FILE_SIZE_TOO_BIG');
if(in_array($file->type, $this->file_type_filter))
throw new UploadrException('UPLOADR_INVALID_FILE_TYPE');
return true;
}
}
public function uploadFile($key, $batch = null) {
if($this->validateFile($key, $batch)) {
$file = (object) $this->getFile($key, $batch);
$file_path = $this->upload_path . $file->name;
if(file_exists($file_path) && $this->warn_on_duplecate_file)
throw new UploadrException('UPLOADR_FILE_ALREADY_EXISTS');
if(!move_uploaded_file($file->tmp_name, $file_path))
throw new UploadrException('UPLOADR_COULD_NOT_UPLOAD');
}
}
public function upload() {
$file_count = 0;
if($this->batch_sort_files) {
foreach($this->files as $batch => $files) {
foreach($files as $key => $file) {
$file_count++;
if($this->max_file_count == 0 || $file_count <= $this->max_file_count) {
$this->uploadFile($key, $batch);
} else {
throw new UploadrException('UPLOADR_FILE_LIMIT_REACHED');
}
}
}
} else {
foreach($this->files as $key => $file) {
$file_count++;
if($this->max_file_count == 0 || $file_count <= $this->max_file_count) {
$this->uploadFile($key);
} else {
throw new UploadrException('UPLOADR_FILE_LIMIT_REACHED');
}
}
}
}
}
try {
if(!$_FILES) {
throw new UploadrException('UPLOADR_NO_FILE_ARRAY');
} else {
$uploadr = new Uploadr('uploads/');
$uploadr->batch_sort_files = true;
$uploadr->max_file_count = 5;
$uploadr->setFiles($_FILES);
$uploadr->upload();
echo 'UPLOADR_UPLOAD_OK';
}
} catch(UploadrException $exception) {
echo $exception->getMessage();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment