View Single Post
Old 17-07-2008, 13:43   #3 (permalink)
Cborrow
I like code.
 
Join Date: Dec 2004
Location: Chesapeake, VA
Posts: 296
Send a message via AIM to Cborrow
There are a lot of free scripts you can find out there for what you need.

http://www.verot.net/php_class_upload.htm
http://www.phpsimplicity.com/scripts.php?id=3
Secure File Upload with PHP
HyperSilence.net - Silentum Uploader - PHP Upload Script

Of you can use the script below.

Just save this as something like FileUpload.php
PHP Code:
<?php
    
class FileUpload {
        public 
$MaxSize "2MB";
        public 
$AllowedExtensions "jpeg,jpg,gif,png,bmp,xbm,xpm";
        public 
$UploadDirectory "./images/";
        
        private 
$filetemp;
        private 
$filename;
        private 
$filetype;
        private 
$filesize;
        private 
$fileerror;
        private 
$fileextension;
        
        public function 
__construct($filesArray null) {
            if(isset(
$filesArray)) {
                
self::SetFileInfo($filesArray);
            }
        }
        
        public function 
SetFileInfo($filesArray) {
            if(isset(
$filesArray) && is_array($filesArray)) {
                
$this->filetemp $filesArray['tmp_name'];
                
$this->filename $filesArray['name'];
                
$this->filetype $filesArray['type'];
                
$this->filesize $filesArray['size'];
                
$this->fileerror $filesArray['error'];
                
$this->fileextension strtolower(array_pop(explode("."$this->filename)));
            }
        }
        
        public function 
Upload($uploadDir null) {
            if(isset(
$this->filetemp) && isset($this->filename) && $this->fileerror == 0) {
                if(empty(
$uploadDir)) { $uploadDir $this->UploadDirectory; }
                
                if(!
in_array($this->fileextensionexplode(","$this->AllowedExtensions))) {
                    echo 
"The file is not of a supported type, allowed types are {$this->AllowedExtensions}";
                    return 
false;
                }
                if(
$this->filesize self::ParseSize($this->MaxSize)) {
                    echo 
"File is too large, the max size is {$this->MaxSize}";
                    return 
false;
                }
                
                if(
is_uploaded_file($this->filetemp)) {
                    if(
move_uploaded_file($this->filetemp$uploadDir $this->filename)) {
                        if(
file_exists($uploadDir $this->filename)) {
                            echo 
"Your file has been uploaded";
                            return 
true;
                        }
                    }
                }
            }
            return 
false;
        }
        
        private function 
ParseSize($size) {
            
preg_match("(([0-9]+)([a-zA-Z]+))"$size$match);
            
$multiplier 1;
            
            if(
is_string($match[2])) {
                switch(
strtolower($match[2])) {
                    case 
"b":
                        
$multiplier 1;
                        break;
                    case 
"kb":
                        
$multiplier 1024;
                        break;
                    case 
"mb":
                        
$multiplier = (1024 1024);
                        break;
                    case 
"gb":
                        
$multiplier = ((1024 1024) * 1024);
                        break;
                    default:
                        
$multiplier 1;
                        break;
                }
                
                if(
is_integer((int)$match[1])) {
                    return 
$match[1] * $multiplier;
                }
            }
            return 
false;
        }
    }
?>

Then use this to upload files.
Don't forget to change "/path/to/uploadfile.php" to point to
whatever you saved the above script as.

PHP Code:
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<div>
<label>File</label>
<input type="file" name="myFile" size="30" /><br /><br />
<input type="submit" name="upload" value="Upload" />
</div>
</form>
<?php
    
if(isset($_POST['upload'])) {
        include 
"/path/to/uploadfile.php";
        
        
$upload = new FileUpload($_FILES['myFile']);
        
$upload->Upload("images/");
    }
?>
__________________

Last edited by Cborrow : 18-07-2008 at 10:44.
  Reply With Quote