<?php

@include_once(S_ROOT.'./source/function_cp.php');



function custom_upload($FILE)

{

	global $_SGLOBAL, $_SC;

	

	//允许上传类型

	$allowpictype = array('jpg','gif','png');

	//检查

	$FILE['size'] = intval($FILE['size']);

	if(empty($FILE['size']) || empty($FILE['tmp_name']) || !empty($FILE['error'])) {

		showmessage('无法获取上传文件大小');

	}

	if ( $FILE['size'] > 204800 )

	{

		showmessage('发生延时。上传的图片不能超过200kb');

	}



	//判断后缀

	$fileext = fileext($FILE['name']);

	if(!in_array($fileext, $allowpictype)) {

		showmessage('只允许上传jpg、gif、png标准格式的图片');

	}

	$filepath 	= 	get_custom_file_path($fileext, true);

	$urlpath	=	'/icon/custom/upload/'.$filepath;

	$new_name 	= 	S_ROOT.'./icon/custom/upload/'.$filepath;

	$tmp_name 	= 	$FILE['tmp_name'];

	if(@copy($tmp_name, $new_name)) {

		@unlink($tmp_name);

	} elseif((function_exists('move_uploaded_file') && @move_uploaded_file($tmp_name, $new_name))) {

	} elseif(@rename($tmp_name, $new_name)) {

	} else {

		showmessage('无法转移临时图片到服务器指定目录');

	}

	

	//检查是否图片

	if(function_exists('getimagesize') && !@getimagesize($new_name)) {

		@unlink($new_name);

		showmessage('只允许上传jpg、gif、png标准格式的图片');

	}

	return $urlpath;

}



//获取上传路径

function get_custom_file_path($fileext, $mkdir=false) {

	global $_SGLOBAL;



	$filepath = "{$_SGLOBAL['supe_uid']}_{$_SGLOBAL['timestamp']}".random(4).".$fileext";

	$name1 = gmdate('Ym');

	$name2 = gmdate('j');



	if($mkdir) {

		$newfilename = '../icon/custom/upload/'.$name1;

		if(!is_dir($newfilename)) {

			if(!@mkdir($newfilename)) {

				return $filepath;

			}

		}

		$newfilename .= '/'.$name2;

		if(!is_dir($newfilename)) {

			if(!@mkdir($newfilename)) {

				return $name1.'/'.$filepath;

			}

		}

	}

	return $name1.'/'.$name2.'/'.$filepath;

}

?>