Jump to content
Fórum Script Brasil
  • 0

Class Files


vini_loock

Question

<?php
    /*----------------------------------------------------------------------------------------------------//
    //----------------------------Class de tratamento de arquivos - PHP-----------------------------------//
    //----------------------------------------------------------------------------------------------------//
    //-----Todos os direitos reservados - Vinicius Siuta Borges-------------------------------------------//
    //-----Contato: viniciussiutaborges@hotmail.com-------------------------------------------------------//
    //--------||--: viniciussiutaborges@gmail.com---------------------------------------------------------//
    //----------------------------------------------------------------------------------------------------*/
    class Files{
        private $file,
                $file_ext,
                $file_name,
                $file_dir;
        
        private $ext_validas = array('jpg', 'png', 'gif', 'bmp', 'rar', 'zip', 'wma');
        
        private $upload_dir = 'temp',
                $temp_dir = 'temp';
        
        public function __construct($file = ''){
            if($file != '')$this->start($file);
        }
        
        private function start($file = ''){
            $this->file = $file;
            $info = pathinfo($file);
            $this->file_name = $info['filename'];
            $this->file_ext = $info['extension'];
            $this->file_dir = str_replace($this->file_name.'.'.$this->file_ext, '', $this->file);
        }
        
        public function setFile($file){
            $this->start($file);
        }
        
        public function getFile(){
            return $this->file;
        }
        
        public function setExt($ext = array('zip')){
            $this->ext_validas = $ext;
        }
        
        public function getExt(){
            return $this->ext_validas;
        }
        
        public function setUploadDir($dir){
            $this->upload_dir = $dir;
        }
        
        public function getUploadDir(){
            return $this->upload_dir;
        }
        
        public function setTempDir($dir){
            $this->temp_dir = $dir;
        }
        
        public function getTempDir(){
            return $this->temp_dir;
        }
        
        public function copy($old, $new){
            if(copy($old, $new)){
                return true;
            }
            return false;
        }
        
        public function move($old, $new){
            if(rename($old, $new)){
                return true;
            }
            return false;
        }
        
        public function upload($file){
            $pre_name = date('YmdHis').rand(0, 999);
            move_uploaded_file($file['tmp_name'], $this->temp_dir.'/'.$pre_name.$file['name']);
            $this->start($this->temp_dir.'/'.$pre_name.$file['name']);
            if($this->valida()){
                $this->move($this->file, $this->upload_dir.'/'.$pre_name.$file['name']);
                $this->start($this->upload_dir.'/'.$pre_name.$file['name']);
            }else{
                $this->delete();
            }
        }
        
        public function delete(){
            if(file_exists($this->file) && unlink($this->file)){
                return true;
            }
            return false;
        }
        
        public function valida(){
            if(in_array($this->file_ext, $this->ext_validas)){
                return true;
            }
            return false;
        }
    }
?>

Edited by vini_loock
Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      651.8k
×
×
  • Create New...