Location: PHPKode > scripts > Advanced file upload and ajax upload > advanced-file-upload-and-ajax-upload/stnc_upload_class_v4.php
<?php 
/**
 *################################################################################
 *#                           Stnc File Upload and Ajax v4.0
 *################################################################################
 *# Class Name :Stnc File Upload and Ajax v4.0
 *# Script-Version:     4.0
 *# File-Release-Date:  22/12/2009 21:34
 *# update Date : 02/ 07/ 2010
 *# Php Version  : PHP 4.3.0+
 *# Official web site and latest version:  selmantunc.com
 *#==============================================================================
 *# Authors: selman tunç (<hide@address.com)
 *# Copyright © 2010   -   selmantunc.com    All Rights Reserved.
 *#
 *################################################################################
 * <br> This program is free software; you can redistribute it and/or             <br>
 * <br> modify it under the terms of the GNU General var License              	   <br>
 * <br> as published by the Free Software Foundation; either version 2            <br>
 * <br> of the License, or (at your option) any later version.                    <br>
 * <br>                                                                           <br>
 * <br> This program is distributed in the hope that it will be useful,           <br>
 * <br> but WITHOUT ANY WARRANTY; without even the implied warranty of            <br>
 * <br> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             <br>
 * <br> GNU General var License for more details.                            	   <br>
 * <br>                                                                           <br>
 * +---------------------------------------------------------------------------+
 */

 
 /**
 * STNC upload Class
 *
 * @version   4.0
 * @author    SeLman TunÇ <hide@address.com>
 * @license   http://opensource.org/licenses/gpl-license.php GNU var License
 * @copyright SeLman TunÇ
 * @package   upload
 * @subpackage external
 */


 
 /**
 * @package   upload
 * @subpackage external
 */
 


class stnc_file_upload {


    /**
     * resim kontrol olcakm&#305;
     *
     * @access puplic
     * @var boolean
     */
    var $picture_control_value = true;
    
    /**
     * the image file type
     *
     * Byte     = B
     * Kilobyte = KB
     * MegaByte = MB
     * GigaByte = GB // not support but only just . :) desteklenmez
     *
     * @access puplic
     * @var string
     */
    var $pic_size_type = 'MB';
    
    /**
     * image size
     *
     * @access puplic
     * @var string
     */
    var $picture_size = '5.00';

    
    /**
     * added to the image before writing the future
     *
     * @access puplic
     * @var string
     */
    var $_prefix = '';
    
    /**
     * will spell the end of the image attachment
     *
     * @access puplic
     * @var string
     */
    var $suffix_ = '';

    
    /**
     * except for the file type of in pictures (example (exe,pdf))
     *
     * @access puplic
     * @var string
     */
    var $size_type_file = 'KB';
    
    /**
     * except the file size of images
     *
     * @access puplic
     * @var string
     */
    var $size_files = '1.00';

    
    /**
     * have enough overwrite
     *
     * @access puplic
     * @var boolean
     */
    var $overwrite = false;

    
    var $language = '';

    
    /**
     * fixed file name
     *
     * @access puplic
     * @var string
     */
    var $constant_name = '';

    
    /**
     * upload files
     *
     * @access puplic
     * @var array
     */
    var $files = array();
    
    /**
     * error information
     *
     * @access puplic
     * @var string
     */
    var $error = NULL;

    
    /**
     * upload files
     *
     * @access puplic
     * @var string
     */
    var $upload_dir = NULL;
    
    /**
     * upload info
     *
     * @access puplic
     * @var boolean
     */
    var $uploaded = false;
    
    /**
     * uploaded files
     *
     * @access puplic
     * @var array
     */
    var $uploaded_files = array();
    
    /**
     * new filename
     *
     * @access puplic
     * @var string
     */
    var $new_file_name = NULL;
    
    /**
     * information
     *
     * @access puplic
     * @var string
     */
    var $info = NULL;

    
    //languages
    var $LNG_1 = ' file named ';
    var $LNG_1_1 = ' loaded with the name ';
    var $LNG_2 = ' Language file not found ';
    var $LNG_3 = ' Please enter at least one file';
    var $LNG_4 = ' Could not find a directory named!';
    var $LNG_5 = ' does not have write permissions to the directory named!';
    var $LNG_6 = ' sizes too big ';
    var $LNG_7 = ' mime type is invalid';
    var $LNG_8 = ' not allow an extension'; 
    var $LNG_9 = ' not a true picture file ';


    
    /**
     
     *extension control, assigning a new name, image editing
     *
     * @access puplic
     * @return boolean uploaded
     */
     
    function upload() {
        if (!$this->error) {
        
            for ($i = 0; $i < count($this->files['tmp_name']); $i++) {

            
                $this->new_file_name = $this->file_name_control($this->files['name'][$i]);
                
                move_uploaded_file($this->files['tmp_name'][$i], $this->upload_dir.'/'.$this->new_file_name);

                
                $this->uploaded_files[] = $this->new_file_name;
                
                $this->info .= '<br>'.$this->files['name'][$i].$this->LNG_1.$this->new_file_name.$this->LNG_1_1;

                
            }
            return $this->uploaded = true;
        }
    }
    
    /**
     * language files
     */
     
    function set_language() {
        $path = pathinfo($this->language);
        $path = $path['dirname'];
        $lang_type = $this->language;
        if (file_exists($lang_type))
            include ($lang_type);
            
        else if (file_exists($path.'/english.php'))
            include ($path.'/english.php');
        else {
        
            $this->error .= ('<br>'.$this->LNG_2);
        }

        
    }

    
    /**
     *total file control
     *
     *
     * @access puplic
     * @param  boolean $total_files
     * @return string
     */
    function total_files($total_files) {
        if ($total_files == true)
            if ($this->files['tmp_name'] == null) {
                $this->error .= '<br>'.$this->LNG_3;
            }
    }

    
    /**
     *file_extension
     *find the file extension
     *
     * @access puplic
     * @param  string $file_name dosya isimleri
     * @return string
     */
    function file_extension($file_name) {
        $file_extension = strtolower(substr(strrchr($file_name, '.'), 1));
        return $file_extension;
    }

    
    /**
     *looks to double check and write permissions to the directory.
     *
     * @access puplic
     * @param  string
     * @return string
     */
    function upload_dir($upload_dir) {
        // dizin var mi?
        if (!is_dir($upload_dir)) {
            $this->error .= '<br>'.$upload_dir.$this->LNG_4;
        }
        
        if (is_dir($path) && !is_writable($upload_dir)) {
            $this->error .= '<br>'.$upload_dir.$this->LNG_5;
        }
        $this->upload_dir = $upload_dir;
    }

    
    /**
     *bad_character_rewrite
     *incompatible and removes unnecessary characters
     *
     * @access puplic
     * @param  string  $text dosya isimleri
     * @return string $text_rewrite
     */
    function bad_character_rewrite($text) {
    
        $first = array("\\", "/", ":", ";", "~", "<br>", "(", ")", "\"", "#", "*", "$", "@", "%", "[", "]", "{", "}", "<", ">", "`", "'", ",", " ", "&#287;", "&#286;", "ü", "Ü", "&#351;", "&#350;", "&#305;", "&#304;", "ö", "Ö", "ç", "Ç");
        $last = array("_", "_", "_", "_", "_", "_", "", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "", "_", "_", "g", "G", "u", "U", "s", "S", "i", "I", "o", "O", "c", "C");
        $text_rewrite = str_replace($first, $last, $text);
        return $text_rewrite;
    }

    
    function only_extension($filename) {
        $file_info = pathinfo($filename);
        $file_name = $file_info['filename'];
        $ext = '.'.$file_info['extension'];
        return $ext;
        
    }

    
    /**
     *clean bad characters, the file Do the same name checks, generate random numbers
     *
     * @access puplic
     * @param  array
     * @return string
     */
    function file_name_control($file_name) {
        $file_name = $this->bad_character_rewrite($file_name);
        $file_name = $this->only_extension($file_name);
        if ($this->overwrite == true) {
            $file_name = $this->only_extension($file_name);
            $constant_name = $this->bad_character_rewrite($this->constant_name);
            $result = $this->constant_name.$file_name;
        } else {
            $unique_name = $this->_prefix.rand(0001, 9999).'_'.rand(0001, 99999).'_'.rand(0001, 99999).$this->suffix_.$file_name;
            $result = $unique_name;
        }
        return $result;
    }

    
    /**
     * file information
     *
     * @access puplic
     * @param  array
     */
    function files($files) {
        if ($files) {
            for ($i = 0; $i < count($files); $i++) {
                if ($files['name'][$i]) {
                    $this->files['tmp_name'][] = $files['tmp_name'][$i];
                    $this->files['name'][] = $files['name'][$i];
                    $this->files['type'][] = $files['type'][$i];
                    $this->files['size'][] = $files['size'][$i];
                    
                }
            }
        }
    }

    
    /**
     *convert all values to the genus kb
     *
     * @access puplic
     * @param   string  $size_type_file
     * @param  integer value
     * @return	integer
     */
    function all2kbytes($value, $size_type_file) {
    
        switch ($size_type_file) {
            case 'B':
                $values = $value;
                break;
            case 'KB':
                $values = $value * 1024;
                break;
            case 'MB':
                $values = $value * 1024 * 1024;
                break;
            /*case 'GB':
             $values=$value*1024*1024*1024;
             */
        }
        //return $values = round($value);//byte
        $values = round($values / 1024); //kb
        return $values = round(($values * 1024), 2);//reapat byte
        // return $values=round($values / 1024 / 1024);//mb
        // return  $values=$values / 1024 / 1024 / 1024;//gb
        
    }

    
    /**
     *compares the size
     *
     * @access puplic
     * @param   string  $file
     * @param  integer $size
     * @param  integer $file_size
     * @return	integer
     */
    function size_compare($size, $file_size, $file) {
        if ($size > $file_size) {
            $this->error .= '<br>'.$file.$this->LNG_6;
            // $this->error .= '6:'.$size.' sizes too big  '.$file_size;
        }
    }
    
    /**
     *checking size of
     *
     * @access puplic
     * @return	boolean
     */
    function size_find() {
        if (!$this->error) {
            $extension_types_picture = array('image/pjpeg', 'image/jpeg', 'image/gif', 'image/png', 'image/x-png');
            
            for ($i = 0; $i < count($this->files['tmp_name']); $i++) {
                if (in_array($this->files['type'][$i], $extension_types_picture)) {
                
                    $file_size_pic = $this->all2kbytes($this->picture_size, $this->pic_size_type);
                    
                    $this->size_compare($this->files['size'][$i], $file_size_pic, $this->files['name'][$i]);
                    
                } else {
                    $file_size = $this->all2kbytes($this->size_files, $this->size_type_file);
                    $this->size_compare($this->files['size'][$i], $file_size, $this->files['name'][$i]);
                }
            }
        }
    }

    
    /**
     *checking size of   // codeigniter
     *
     * @access puplic
     * @return	array
     */
    function extension_types_controls($extension_types) {
    
        $extensions = array('hqx'=>'application/mac-binhex40', 'cpt'=>'application/mac-compactpro', 'csv'=>array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'), 'bin'=>'application/macbinary', 'dms'=>'application/octet-stream', 'lha'=>'application/octet-stream', 'lzh'=>'application/octet-stream', 'exe'=>'application/octet-stream', 'class'=>'application/octet-stream', 'psd'=>'application/x-photoshop', 'so'=>'application/octet-stream', 'sea'=>'application/octet-stream', 'dll'=>'application/octet-stream', 'oda'=>'application/oda', 'pdf'=>array('application/pdf', 'application/x-download'), 'ai'=>'application/postscript', 'eps'=>'application/postscript', 'ps'=>'application/postscript', 'smi'=>'application/smil', 'smil'=>'application/smil', 'mif'=>'application/vnd.mif', 'xls'=>array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'), 'ppt'=>array('application/powerpoint', 'application/vnd.ms-powerpoint'), 'wbxml'=>'application/wbxml', 'wmlc'=>'application/wmlc', 'dcr'=>'application/x-director', 'dir'=>'application/x-director', 'dxr'=>'application/x-director', 'dvi'=>'application/x-dvi', 'gtar'=>'application/x-gtar', 'gz'=>'application/x-gzip', 'php'=>'application/x-httpd-php', 'php4'=>'application/x-httpd-php', 'php3'=>'application/x-httpd-php', 'phtml'=>'application/x-httpd-php', 'phps'=>'application/x-httpd-php-source', 'js'=>'application/x-javascript', 'swf'=>'application/x-shockwave-flash', 'sit'=>'application/x-stuffit', 'tar'=>'application/x-tar', 'tgz'=>'application/x-tar', 'xhtml'=>'application/xhtml+xml', 'xht'=>'application/xhtml+xml', 'zip'=>array('application/x-zip', 'application/zip', 'application/x-zip-compressed'), 'mid'=>'audio/midi', 'midi'=>'audio/midi', 'mpga'=>'audio/mpeg', 'mp2'=>'audio/mpeg', 'mp3'=>array('audio/mpeg', 'audio/mpg'), 'aif'=>'audio/x-aiff', 'aiff'=>'audio/x-aiff', 'aifc'=>'audio/x-aiff', 'ram'=>'audio/x-pn-realaudio', 'rm'=>'audio/x-pn-realaudio', 'rpm'=>'audio/x-pn-realaudio-plugin', 'ra'=>'audio/x-realaudio', 'rv'=>'video/vnd.rn-realvideo', 'wav'=>'audio/x-wav', 'bmp'=>'image/bmp', 'gif'=>'image/gif', 'jpeg'=>array('image/jpeg', 'image/pjpeg'), 'jpg'=>array('image/jpeg', 'image/pjpeg'), 'jpe'=>array('image/jpeg', 'image/pjpeg'), 'png'=>array('image/png', 'image/x-png'), 'tiff'=>'image/tiff', 'tif'=>'image/tiff', 'css'=>'text/css', 'html'=>'text/html', 'htm'=>'text/html', 'shtml'=>'text/html', 'txt'=>'text/plain', 'text'=>'text/plain', 'log'=>array('text/plain', 'text/x-log'), 'rtx'=>'text/richtext', 'rtf'=>'text/rtf', 'xml'=>'text/xml', 'xsl'=>'text/xml', 'mpeg'=>'video/mpeg', 'mpg'=>'video/mpeg', 'mpe'=>'video/mpeg', 'qt'=>'video/quicktime', 'mov'=>'video/quicktime', 'avi'=>'video/x-msvideo', 'movie'=>'video/x-sgi-movie', 'doc'=>'application/msword', 'docx'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'xlsx'=>'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'word'=>array('application/msword', 'application/octet-stream'), 'xl'=>'application/excel', 'eml'=>'message/rfc822');

        
        if (!$this->error) {
            for ($i = 0; $i < count($this->files['tmp_name']); $i++) {
            
                if (!in_array($this->file_extension($this->files['name'][$i]), $extension_types))
                    $this->error .= '<br>'.$this->files['name'][$i].$this->LNG_7;
            }
        }
    }

    
    /**	 
     * of a file, check if the extension would be compatible
     *
     * @access  puplic
     * @param  array
     */
    function is_file_extension($extension_types) {
    
        if (!$this->error) {
            for ($i = 0; $i < count($this->files['tmp_name']); $i++) {
            
                if (!in_array($this->file_extension($this->files['name'][$i]), $extension_types))
                                
                    $this->error .= '<br>'.$this->files['name'][$i].$this->LNG_8;
            }
        }
    }

    
    /**	 
     * gd check my add-in installed
     * checks whether a real picture
     *
     *
     * @access  puplic
     */
    function picture_control() {
    
        if ($this->picture_control_value == true) {
            $extension_types_picture = array('image/pjpeg', 'image/jpeg', 'image/gif', 'image/png', 'image/x-png');
            
            for ($i = 0; $i < count($this->files['tmp_name']); $i++) {
                if (in_array($this->files['type'][$i], $extension_types_picture)) {

                
                    if (extension_loaded('gd') && !imagecreatefromstring(file_get_contents($this->files['tmp_name'][$i])))
                    
                        $this->error .= '<br>'.$this->files['name'][$i].$this->LNG_9;
                        
                    elseif (!getimagesize($this->files['tmp_name'][$i]))
                        $this->error .= '<br>'.$this->files['name'][$i].$this->LNG_9;
                }
                
            }
        }
    }

    
    /**
     *reports
     *
     * @access puplic
     */
    function result_report() {
        if (isset($this->error)) {
            //echo '<ul>';
            echo $this->error;
            //echo '</ul>';
        }
        if ($this->uploaded == true) {
            // echo '<ul>';
            echo $this->info;
            // echo '</ul>';
        }
    }

    
    /**
     * first define the values
     * @param string $_prefix
     * @param string $suffix_
     * @param string $size_type_file
     * @param string $size_files
     */
    function file_upload($size_type_file, $size_files) {
    
        $this->size_type_file = $size_type_file;
        $this->size_files = $size_files;
    }

    
    /**
     * prefixes and suffixes for naming and file overwriting the settings
     * isimlendirme ve dosya üzerine yazma ve önek ve sonek ayarlar&#305;
     * @param boolean $overwrite
     * @param string $constant_name
     * @param string $_prefix
     * @param string $suffix_
     
     */
    function name_format($overwrite, $constant_name, $_prefix, $suffix_) {
    
        $this->overwrite = $overwrite;
        $this->constant_name = $constant_name;
        $this->suffix_ = $suffix_;
        $this->_prefix = $_prefix;
    }

    
    /**
     * picture_uploads values
     *
     * @param boolean $picture_control
     * @param integer $pic_width
     * @param integer $pic_height
     * @param string $pic_size_type
     * @param string $picture_size
     */
    function picture_uploads($picture_control, $pic_size_type, $picture_size) {
        $this->picture_control_value = $picture_control;
        
        $this->pic_size_type = $pic_size_type;
        $this->picture_size = $picture_size;
        
    }
    
    /**
     *founder of the function, all the work starts here
     *
     * @access puplic
     * @param   array  $files
     * @param  string $upload_dir
     * @param  array $extension_types
     */
    function uploader_set($files, $upload_dir, $extension_types, $total_files) {
    
        $this->set_language();
        $this->upload_dir($upload_dir);
        $this->files($files);
        
        $this->total_files($total_files);
        
        $this->is_file_extension($extension_types);
        $this->extension_types_controls($extension_types);
        
        $this->size_find();
        $this->picture_control();
        $this->upload();
        
    }
}
error_reporting(0);

?>
Return current item: Advanced file upload and ajax upload