用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java 文件上传类

2015-01-01 作者: java源代码大全举报

[java]代码库

<?PHP
/**
 * Uploader
 * 
 * @package my
 * @author php100.com
 * @copyright 2010
 * @version $Id$
 * @access public
 */
class Uploader {

    public $files = array();
    private $errors = array();
    private $file_names = array();
    private $directory = NULL;
    private $uploaded = false;

    # uploaded file name
    public $uploaded_files = array();
    private $file_new_name = NULL;
    private $results = NULL;

    /**
     * set directory attribute
     * 
     * @param mixed $directory
     * @return
     */
    function directory($directory) {
        if (!is_dir($directory)) {
            $this -> errors []= $directory.' not a directory';
        }
        if (!is_writable($directory)) {
            $this -> errors []= $directory.' unwritable';
        }
        $this -> directory = $directory;
    }

    /**
     * set files attribute
     * 
     * @param mixed $files
     * @return
     */
    function files($files)
    {
        if(empty($files)) {
            throw new Exception('file array is empty');
        }
        if (!is_array($files['tmp_name'])) {
            $this -> files['tmp_name'][] = $files['tmp_name'];
            $this -> files['name'][] = $files['name'];
            $this -> files['type'][] = $files['type'];
            $this -> files['size'][] = $files['size'];
        }
        else {
            for ($i = 0; $i < count($files['name']); $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];
                }
            }
        }
    }

    /**
     * Uploader::bad_character_rewrite()
     * 
     * @param mixed $text
     * @return
     */
    private function bad_character_rewrite($text)
    {
            $first = array("\\\\", "/", ":", ";", "~", "|", "(", ")", "\\"", "#", "*", "$", "@", "%", "[", "]", "{", "}", "<", ">", "`", "'", ",", " ", "臒", "臑", "眉", "脺", "艧", "艦", "谋", "陌", "枚", "脰", "莽", "脟");
            $last = array("_", "_", "_", "_", "_", "_", "", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "", "_", "_", "g", "G", "u", "U", "s", "S", "i", "I", "o", "O", "c", "C");
            $text_rewrite = str_replace($first, $last, $text);
            return $text_rewrite;
    }

    /**
     * get file extension
     * 
     * @param mixed $file_name
     * @return
     */
    public function file_extension($file_name)
    {
        $file_extension = strtolower(substr(strrchr($file_name, '.'), 1));
        return $file_extension;
    }

    private function file_name($file)
    {
        return strtolower(substr($file, 0, strpos($file, '.')));
    }

    /**
     * create random file name
     * 
     * @param mixed $file_name  no used
     * @return
     */
    private function _file_name_control($file_name)
    {
        if(file_exists($this->directory.'/'.$file_name)) {
            //return $this->file_name($file_name).strtolower(rand() % 1000000).'.'.$this->file_extension($file_name);
            return strtolower(rand() % 1000000).'_'.$file_name;
        }
        else
            return $file_name;
    }

    /**
     * Uploader::_begin_upload()
     * 
     * @param mixed $extensions
     * @return void
     */
    function _begin_upload($extensions)
    {
        if(!count($this->errors)) {
            for ($i = 0; $i < count($this -> files['tmp_name']); $i++) 
            {
                if (in_array($this->file_extension($this->files['name'][$i]), $extensions)) 
                {
                    $this->file_new_name = $this ->_file_name_control($this-> files['name'][$i]);
                    move_uploaded_file($this -> files['tmp_name'][$i], $this-> directory.'/'.$this -> file_new_name);
                    $this -> uploaded_files[] = $this -> file_new_name;
                    $this -> results .= '<li><strong>'.$this -> files['name'][$i].'</strong> change name to  <strong>'.$this -> file_new_name.'</strong>, file size <br />(~'.round($this -> files['size'][$i] / 1024, 2).' kb). </li>';
                }
                else
                {
                    $this -> results .= '<li>'.$this -> files['name'][$i].' no access type</li>';
                }
            }
            $this -> uploaded = true;
        } else {
            foreach($this->errors as $error) {
                throw new Exception($error);
            }
        }
    }

    /**
     * print report
     * 
     * @return
     */
    function result_report()
    {
        if ($this -> uploaded == true) {
            echo '<ul>';
            echo $this -> results;
            echo '</ul>';
        }
    }

    /**
     * Uploader::upload()
     * 
     * @param mixed $files
     * @param mixed $directory
     * @param mixed $extensions
     * @return
     */
    function upload($files, $directory, $extensions=array('jpg', 'jpeg', 'png', 'gif'))
    {
        $this -> directory($directory);
        $this -> files($files);
        $this -> _begin_upload($extensions);
    }
}
//该片段来自于http://yuncode.net


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...