I've recently managed to successfully plug the very cool Valums File Upload Plugin into an application I’m currently working on with the purpose being to upload images. I now need to modify the server side PHP scripting of this plugin to do some image manipulation (re-sizing, watermarking, thumbnail creating..etc) which I’ve been able to do before using procedural style coding, but looking at the way the upload handling is done in the valums plugin it’s all OOP.
So I’ve spent some time recently trying to get my head around OOP programing styles and I think I get the basics, but I could really use some pointers (I'm NOT asking for someone to do this for me) as to how to implement what I want to do into the existing code, things like where should my image manipulation methods go, how to get a handle on the uploaded file.
The complete code for the server side handling can be found here, but as a overview it goes a bit like this (sorry it's still quite long):
<?php
/**
* Handle file uploads via XMLHttpRequest
*/
class qqUploadedFileXhr {
function save($path) {
$input = fopen("php://input", "r");
$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
fclose($input);
// REMOVED VALIDATION
$target = fopen($path, "w");
fseek($temp, 0, SEEK_SET);
stream_copy_to_stream($temp, $target);
fclose($target);
return true;
}
function getName() {
return $_GET['qqfile'];
}
function getSize() {
// REMOVED
}
}
/**
* Handle file uploads via regular form post (uses the $_FILES array)
*/
class qqUploadedFileForm {
function save($path) {
if(!move_uploaded_file($_FILES['qqfile']['tmp_name'], $path)){
return false;
}
return true;
}
function getName() {
return $_FILES['qqfile']['name'];
}
function getSize() {
return $_FILES['qqfile']['size'];
}
}
class qqFileUploader {
private $allowedExtensions = array();
private $sizeLimit = 10485760;
private $file;
function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
$allowedExtensions = array_map("strtolower", $allowedExtensions);
$this->allowedExtensions = $allowedExtensions;
$this->sizeLimit = $sizeLimit;
$this->checkServerSettings();
if (isset($_GET['qqfile'])) {
$this->file = new qqUploadedFileXhr();
} elseif (isset($_FILES['qqfile'])) {
$this->file = new qqUploadedFileForm();
} else {
$this->file = false;
}
}
private function checkServerSettings(){
// REMOVED
}
private function toBytes($str){
// REMOVED
}
/**
* Returns array('success'=>true) or array('error'=>'error message')
*/
function handleUpload($uploadDirectory, $replaceOldFile = FALSE){
// REMOVED VALIDATION
$pathinfo = pathinfo($this->file->getName());
$filename = $pathinfo['filename'];
//$filename = md5(uniqid());
$ext = $pathinfo['extension'];
// REMOVED VALIDATION
if ($this->file->save($uploadDirectory . $filename . '.' . $ext)){
return array('success'=>true);
} else {
return array('error'=> 'Could not save uploaded file.' .
'The upload was cancelled, or server error encountered');
}
}
}
$allowedExtensions = array("jpeg","jpg","bmp");
// max file size in bytes
$sizeLimit = 10 * 1024 * 1024;
$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
$result = $uploader->handleUpload('uploads/');
// to pass data through iframe you will need to encode all html tags
echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
?>
To clarify, I’m not after spoon feeding, but I’m struggling to get my head around where to start and seeing as I’m on a steep learning curve I don’t really want to set off down the wrong path. I could probably turn this into procedural code but I think this is a good opportunity to learn OOP properly.
Any help is greatly appreciated.
Dan