function file_extension($file) { $ext = array_pop(explode(".", $file)); if ($ext==$file) return false; return $ext; }
function set_max_size($size=0) { $this->_max_size = $size; } function get_max_size() { return $this->_max_size; }
function get_error() { switch ($this->_errno) { case UPLOAD_ERR_INI_SIZE : return 'The uploaded file exceeds the upload_max_filesize directive in php.ini.'; case UPLOAD_ERR_FORM_SIZE : return ' The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.'; case UPLOAD_ERR_PARTIAL : return 'The uploaded file was only partially uploaded.'; case UPLOAD_ERR_NO_FILE : return 'No file was uploaded.'; case UPLOAD_ERR_NO_TMP_DIR : return 'Missing a temporary folder.'; case UPLOAD_ERR_CANT_WRITE : return 'Failed to write file to disk.'; case FILE_UPLOAD_ERR_PATH : return 'Upload path is not a directory.'; case FILE_UPLOAD_ERR_SIZE : return 'The uploaded file exceeds the max file size.'; case FILE_UPLOAD_ERR_TYPE: return 'The uploaded file type is invalid.'; case FILE_UPLOAD_ERR_INVALID: return 'The file is not an actual uploaded file.'; } return 'Unknown error ('.intval($this->errno).')'; }
function is_error() { return $this->_is_error; } function set_path($path) { $this->path = $path; } function get_path() { return $this->path; }
it will need to be modified to allow other file types.. so hopefully someone can reply to this thread for whatever their need is and I can update it ... it is currently used for uploading images.
Multiple files by request:
I had to revise the main class to accept multiple files (upload_multiple method). copy and paste the updated class.
example usage php for doing the actual upload:
PHP Code:
<?php $is_upload = $_GET['upload'] && !empty($_FILES); $max_size = 250000; $max_uploads = 5; if ($is_upload) { if (count($_FILES['file']['name'])<=$max_uploads) { $upload = new File_upload(); $upload->allow('images'); $upload->set_path('/home/ecnet/public_html/uploads/'); $upload->set_max_size($max_size);
$files = $upload->upload_multiple($_FILES['file']); $error = false; if ($upload->is_error()) { $error = true; $errstr= $upload->get_error(); } } else { $error = true; $errstr= 'Trying to upload to many files'; } } ?>
example usage as far as HTML etc
PHP Code:
<h1>Image Upload</h1><br /> <script type="text/javascript"><!-- var gFiles = 0; function addFile() { var tr = document.createElement('tr'); tr.setAttribute('id', 'file-' + gFiles); var td = document.createElement('td'); td.innerHTML = '<input type="file" size="30" name="file[]"><span onclick="removeFile(\'file-' + gFiles + '\')" style="cursor:pointer;">Remove</span>'; tr.appendChild(td); document.getElementById('files-root').appendChild(tr); gFiles++; } function removeFile(aId) { var obj = document.getElementById(aId); obj.parentNode.removeChild(obj); }
--></script> <form enctype="multipart/form-data" method="POST" action="?mode=misc&action=upload&upload=1"> <span onclick="addFile()" style="cursor:pointer;cursor:hand;">Add</span> <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_size; ?>" /> <table><tbody id="files-root"> <tr><td><input type="file" name="file[]" size="30"></td></tr> </table> <input type="submit" value="Upload Image"> </form> <p style="font-size:8pt;"> allowed extensions are: <strong>JPG, JPEG PNG, GIF</strong>; max size per file: <strong>250kb</strong>; max number of files per upload <strong><?php echo $max_uploads; ?> </p> <?php if ($is_upload && $error) { print '<strong>Error: '.$errstr.'</strong><br />'; } else if ($is_upload) { foreach ($files as $file) { $image = _URL_.'uploads/'.$file; print '<input type="text" size="'.strlen($image).'" value="[img]'.$image.'[/img]"><br />'; print '<img src="'.$image.'">'; } }
to do(or to not do, depending on how it goes ):
- make sure filename is completely unique by checking if file_exists again and again until it passes
- better way to set allowed file types and a get method to return them as an array.
- use is_uploaded_file http://us3.php.net/manual/en/functio...oaded-file.php
edit, i just felt like doing it tonight (updated top post again)
function file_extension($file) { $ext = array_pop(explode(".", $file)); if ($ext==$file) return false; return $ext; }
function set_max_size($size=0) { $this->_max_size = $size; } function get_max_size() { return $this->_max_size; }
function get_error() { switch ($this->_errno) { case UPLOAD_ERR_INI_SIZE : return 'The uploaded file exceeds the upload_max_filesize directive in php.ini.'; case UPLOAD_ERR_FORM_SIZE : return ' The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.'; case UPLOAD_ERR_PARTIAL : return 'The uploaded file was only partially uploaded.'; case UPLOAD_ERR_NO_FILE : return 'No file was uploaded.'; case UPLOAD_ERR_NO_TMP_DIR : return 'Missing a temporary folder.'; case UPLOAD_ERR_CANT_WRITE : return 'Failed to write file to disk.'; case FILE_UPLOAD_ERR_PATH : return 'Upload path is not a directory.'; case FILE_UPLOAD_ERR_SIZE : return 'The uploaded file exceeds the max file size.'; case FILE_UPLOAD_ERR_TYPE: return 'The uploaded file type is invalid.'; case FILE_UPLOAD_ERR_INVALID: return 'The file is not an actual uploaded file.'; } return 'Unknown error ('.intval($this->errno).')'; }
function is_error() { return $this->_is_error; } function set_path($path) { $this->path = $path; } function get_path() { return $this->path; }