[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