Ok, I spent the night writing a simple upload script that pretty much demonstrates what I am talking about. My first example sucked but the following is a class that can actually be used. The main goal of the class is to take a file from a user, clean the filename and upload it to the server. There are also options that the user can toggle in an array that's passed to the constructor.
When I designed this class, I didn't see any other public method that was needed, other than a simple message that lets the user know that something is wrong. It's because of this that I have made all of the members private as well as the methods.
Please have a look at it and let's discuss how it can be made better. I would also like to know if this is a decent class design.
Thanks,
Frank
When I designed this class, I didn't see any other public method that was needed, other than a simple message that lets the user know that something is wrong. It's because of this that I have made all of the members private as well as the methods.
Please have a look at it and let's discuss how it can be made better. I would also like to know if this is a decent class design.
Thanks,
Frank
Code:
<?php class FileUpload { private $_upload = array(); private $_config = array(); private $_fileName; private $_fileMime; private $_fileSize; private $_fileTemp; private $_fileError; private $_setFormName = 'userfile'; private $_setExtension; private $_setNewName; private $_setUploadPath = null; private $_setMaxSize = 0; public $message; public function __construct( array $upload, array $config ) { $this->_upload = $upload; $this->_config = $config; $this->setOptions(); $this->initialize(); } private function setOptions() { $opts = $this->_config; foreach($opts as $opt => $val) { if(!empty($val)) { $this->{$opt} = $val; } } } private function initialize() { $upload = $this->_upload; $form = $this->_setFormName; if(!empty($upload["$form"]['name'])) { if($this->testUploadDir()) { $this->_fileName = $this->_upload["$form"]['name']; $this->_fileMime = $this->_upload["$form"]['type']; $this->_fileSize = $this->_upload["$form"]['size']; $this->_fileTemp = $this->_upload["$form"]['tmp_name']; $this->_fileError = $this->_upload["$form"]['error']; $this->checkExtension(); } } else { $this->message = 'Please Select A File To Upload...'; } } private function testUploadDir() { $path = $this->_setUploadPath; if(file_exists($path)) { if(!is_readable($path)) { $this->message = 'Upload Directory Not Readable'; return false; } if(!is_writeable($path)) { $this->message = 'Upload Directory Not Writeable'; return false; } } else { $this->message = 'Upload Directory Does Not Exist'; return false; } return true; } private function cleanFile() { $temp = $this->_fileName; $temp = strtolower($temp); $temp = str_replace(' ', '_', $temp); $result = ''; for ($i=0; $i<strlen($temp); $i++) { if (preg_match("([0-9]|[a-z]|_|.)", $temp[$i])) { $result = $result . $temp[$i]; } } $this->_fileName = $result; } private function checkExtension() { $this->cleanFile(); $file = $this->_fileName; $getExt = substr($file, strpos($file,'.'), strlen($file)-1); $setExt = $this->_setExtension; if($getExt != $setExt) { $this->message = 'The File You Attempted To Upload Is Not Allowed.'; return false; } $this->checkSize(); } private function checkSize() { $size = $this->_fileSize; $max = $this->_setMaxSize; if($max != 0) { if($size > $max) { $this->message = 'The File You Attempted To Upload Is Too Large.'; return false; } } $this->renameFile(); } private function renameFile() { $old = $this->_fileName; $new = $this->_setNewName; if(!empty($new)) { $this->_fileName = $new; } $this->moveFile(); } private function moveFile() { $temp = $this->_fileTemp; $file = $this->_fileName; $path = $this->_setUploadPath; $error = $this->_fileError; if($error == 0) { if(move_uploaded_file($temp,$path.$file)) { $this->message = 'Your File Upload Was Successful.'; } else { $this->message = 'Your File Upload Failed While Moving It To Its Permanent Location.'; } } else { $this->message = 'Your File Upload Failed. Errno: '.$error; } } } $file = $_FILES; $config = array('_setFormName'=>'test','_setExtension'=>'.jpg','_setNewName'=>'new.jpg','_setUploadPath' => './upload/','_setMaxSize'=>'0'); $test = new FileUpload($file,$config); echo $test->message; ?> <form method="post" action="upload.php" enctype="multipart/form-data"> <input type="file" name="test" size="25"> <input type="submit" name="submit" value="Upload"> </form>
Comment