Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php class file upload

<?php
class Uploader
{
    private $destinationPath;
    private $errorMessage;
    private $extensions;
    private $maxSize;
    private $uploadName;
    public $name='Uploader';

    function setDir($path){
        $this->destinationPath  =   $path;
    }

    function setMaxSize($sizeMB){
        $this->maxSize  =   $sizeMB * (1024*1024);
    }

    function setExtensions($options){
        $this->extensions   =   $options;
    }

    function setMessage($message){
        $this->errorMessage =   $message;
    }

    function getMessage(){
        return $this->errorMessage;
    }

    function getUploadName(){
        return $this->uploadName;
    }

    function uploadFile($fileBrowse){
        $result =   false;
        $size   =   $_FILES[$fileBrowse]["size"];
        $name   =   $_FILES[$fileBrowse]["name"];
        $ext    =   pathinfo($name,PATHINFO_EXTENSION);

        $this->uploadName=  $name;

        if(empty($name))
        {
            $this->setMessage("File not selected ");
        }
        else if($size>$this->maxSize)
        {
            $this->setMessage("Too large file !");
        }
        else if(in_array($ext,$this->extensions))
        {
            if(!is_dir($this->destinationPath))
                mkdir($this->destinationPath);
            if(!is_dir($this->destinationPath.'/'.$ext))
                mkdir($this->destinationPath.'/'.$ext);

            if(file_exists($this->destinationPath.'/'.$ext.'/'.$this->uploadName))
                $this->setMessage("File already exists. !");
            else if(!is_writable($this->destinationPath.'/'.$ext))
                $this->setMessage("Destination is not writable !");
            else
            {
                if(move_uploaded_file($_FILES[$fileBrowse]["tmp_name"],$this->destinationPath.'/'.$ext.'/'.$this->uploadName))
                {
                    $result =   true;
                }
                else
                {
                    $this->setMessage("Upload failed , try later !");
                }
            }
        }
        else
        {
            $this->setMessage("Invalid file format !");
        }
        return $result;
    }

    function deleteUploaded($fileBrowse){
        $name   =   $_FILES[$fileBrowse]["name"];
        $ext    =   pathinfo($name,PATHINFO_EXTENSION);
        unlink($this->destinationPath.'/'.$ext.'/'.$this->uploadName);
    }
}
?>
Comment

PREVIOUS NEXT
Code Example
Php :: nginx codeigniter remove index.php 
Php :: php version command linux 
Php :: how hide empty category wordpress woocommerce 
Php :: update checkbox value in laravel 
Php :: array length php 
Php :: wordpress create comment programmatically 
Php :: laravel model uploaded file name 
Php :: wordpress enable post thumbnail 
Php :: IlluminateContractsContainerBindingResolutionException 
Php :: php dies while parsing json 
Php :: how to print on console with phpunit 
Php :: php convert array to json 
Php :: session forget laravel 
Php :: drupal 9 custom blocks dependency injection 
Php :: php if time is greater than 
Php :: Remove .php extension & Remove trailing slash 
Php :: php slice array by key 
Php :: magento2 move Exception #0 (Exception): Notice: Undefined offset: 2 in /var/www/nucleus/htdocs/vendor/magento/framework/Encryption/Encryptor.php on line 591 
Php :: strpos php 
Php :: nested for loop in php 
Php :: find object in array php 
Php :: laravel Access to HMLHttpRequest from origin has been blocked by CORS policy: No Access-Control-Allow-Origin 
Php :: constants in php 
Php :: how to get array key in php 
Php :: php localhost:8000 
Php :: laravel pagination with search filter 
Php :: laravel array search blade 
Php :: error pdo php Exception 
Php :: laravel collection get 
Php :: php unit expect exception 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =