note 98883 added to features.file-upload.post-method
| From: | rutman at aapt dot net dot au | Date: | Tue, 13 Jul 2010 23:10:11 +0000 |
| Subject: | note 98883 added to features.file-upload.post-method | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170883@lists.php.net to get a copy of this message | ||
Here is a file uploader;
<?php
class FileUploader{
protected $fieldname;
protected $err;
protected $size;
protected $types;
protected $uploaddir;
public function set_UploadDirectory($val){
$this->uploaddir = $val;
}
public function set_FieldName($val){
$this->fieldname = $val;
}
public function get_UploadError(){
return $this->err;
}
public function get_Size(){
return $this->size;
}
public function set_Types($val){
$this->types = $val;
}
public function __construct(){
$this->err = false;
$this->size = 0;
$this->types = array('image/jpeg', 'video/x-ms-wmv',
'audio/mpeg', 'video/mpeg', 'video/avi', 'image/bmp',
'image/gif', 'image/png');
if(isset($_FILES[$this->fieldname])){
$this->size = $_FILES[$this->fieldname]['size'];
}
}
public function Upload(){
if(!empty($_FILES[$this->fieldname]['name'])){
if(in_array($_FILES[$this->fieldname]['type'], $this->types)){
$uploadfile = $this->uploaddir . basename($_FILES[$this->fieldname]['name']);
if(move_uploaded_file($_FILES[$this->fieldname]['tmp_name'], $uploadfile)) {
return $uploadfile;
}else{
$this->err = $this->setError();
}
}else{
$this->err = array(99, 'Illegal file type.',
'UPLOAD_ERR_FILE_TYPE', 'The uploaded file type was not found in the file
type array.');
}
}
return false;
}
private function setError(){
$err = '';
switch($_FILES[$this->fieldname]['error']){
case 0;
$err = false;
break;
case 1;
$err = array(1, 'The uploaded file exceeds the maximum file size.',
'UPLOAD_ERR_INI_SIZE',
'The uploaded file exceeds the upload_max_filesize directive in php.ini.');
break;
case 2;
$err = array(2, 'The uploaded file exceeds the maximum file size.',
'UPLOAD_ERR_FORM_SIZE',
'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML
form.');
break;
case 3;
$err = array(3, 'The uploaded file was only partially uploaded.',
'UPLOAD_ERR_PARTIAL', 'The uploaded file was only partially uploaded.');
break;
case 4;
$err = array(4, 'No file was uploaded.', 'UPLOAD_ERR_NO_FILE', 'No file
was uploaded.');
break;
case 6;
$err = array(6, 'Server Error', 'UPLOAD_ERR_NO_TMP_DIR', 'Missing a
temporary folder.');
break;
case 7;
$err = array(7, 'Server error', 'UPLOAD_ERR_CANT_WRITE', 'Failed to
write file to disk.');
break;
case 8;
$err = array(8, 'Server Error', 'UPLOAD_ERR_EXTENSION', 'A PHP
extension stopped the file upload.');
break;
}
return $err;
}
public function Dump(){
echo "<PRE>" . print_r($_FILES, true) . "</PRE>";die;
}
}
?>
set_FieldName($val) where $val is equal to the name of the file element on the form with appropriate
enctype.
The allowable types array can be set using the set_Types() function. The file size is set on post
back.
It will return the path to the uploaded file or false on error. If it errors, it sets the $err
property accessible through the get_UploadError() function.
usage;
$fup = new FileUploader();
$fup->set_UploadDirectory('Uploads/');
$fup->set_FieldName('attachment');
if(!$fup->Upload()){
echo "<PRE>" . print_r($fup->get_UploadError(), true) .
"</PRE>";die;
} }
----
Server IP: 117.55.229.1
Probable Submitter: 59.100.67.51
----
Manual Page -- http://www.php.net/manual/en/features.file-upload.post-method.php
Edit -- https://master.php.net/note/edit/98883
Del: integrated -- https://master.php.net/note/delete/98883/integrated
Del: useless -- https://master.php.net/note/delete/98883/useless
Del: bad code -- https://master.php.net/note/delete/98883/bad+code
Del: spam -- https://master.php.net/note/delete/98883/spam
Del: non-english -- https://master.php.net/note/delete/98883/non-english
Del: in docs -- https://master.php.net/note/delete/98883/in+docs
Del: other reasons-- https://master.php.net/note/delete/98883
Reject -- https://master.php.net/note/reject/98883
Search -- https://master.php.net/manage/user-notes.php