復制代碼 代碼如下:
/**
* 圖片上傳方法
* $maxsize=500000 = 500k;
* $updir="up/";
* $upfile=$_FILES["file_img"];
*/
public function Get_file_upload($upfile, $maxsize, $updir, $newname = 'date') {
if ($newname == 'date')
$newname = date("Ymdhs"); //使用日期做文件名
$name = $upfile["name"];
$type = $upfile["type"];
$size = $upfile["size"];
$tmp_name = $upfile["tmp_name"];
switch ($type) {
case 'image/pjpeg' :
case 'image/jpeg' :
$extend = ".jpg";
break;
case 'image/gif' :
$extend = ".gif";
break;
case 'image/png' :
$extend = ".png";
break;
}
if (empty ($extend)) {
echo '文件類型不正確,只能使用JPG GIF PNG 格式';
}
if ($size > $maxsize) {
$maxpr = $maxsize / 1000;
echo "警告!上傳圖片大小不能超過";
}
if (move_uploaded_file($tmp_name, $updir . $newname . $extend)) {
return $newname . $extend;
}
}