一个超简单的php上传类代码
2011-09-16 16:17
531 查看
这个是我昨晚写的一个超简单的上传类,只要 $up->upload($_FILES['imgfile']); 即可。 一条语句搞定上传
送给所有草根的个人站长
上传文件:
up.php
<?php
//验证
if(empty($_COOKIE['login'])) {
die('index');
}
//加载类
include_once("_inc/class_uppic.php");
//上传
if(!empty($_GET['action']) && $_GET['action'] == "upload"){
$up->upload($_FILES['imgfile']);
echo "上传成功!文件名:{$up->new_url} <a href=?yes>重新上传</a>";
}
?>
<form action="?action=upload" method="post" enctype="multipart/form-data">
图片来源:<input type="file" name="imgfile" style="width:200px;height:25px;">
<input type="submit" name="submit" value="上传" style="width:50px;height:25px;">
//================分割线==============
类文件:
class_uppic.php
<?php
/*
power by jtxxol.com
2011-06-20
*/
$up=new class_uppic;
class class_uppic{
function __construct(){
//保存目录
$this->save_path='/uploadfile/';
//文件链接
$this->save_url='/uploadfile/';
//允许大小 300k
$this->allow_size=300000;
//允许类型
$this->allow_ext='jpg,jpeg,gif,png';
//新文件名
$this->new_name=date("YmdHis").rand(10,99);
//初始化错误提示
$this->err='';
}
//上传
function upload($arr){
$this->file=$arr;
//初始化设置
$this->allow_ext=explode(',',$this->allow_ext);
$this->save_path=$_SERVER['DOCUMENT_ROOT'].$this->save_path.date('Y-m').'/';
$this->save_url=$this->save_url.date('Y-m').'/';
//获得扩展名
$temp_arr = explode(".", $this->file['name']);
$file_ext = array_pop($temp_arr);
$file_ext = trim(strtolower($file_ext));
//检查类型
if(!in_array($file_ext,$this->allow_ext)){
$this->err="上传图片类型错误";
}
//检查大小
if($this->file['size']>$this->allow_size){
$this->err="文件超出限制大小";
}
//递归建立目录
$this->creatdir($this->save_path);
//上传后的路径
$this->new_path=$this->save_path.$this->new_name.'.'.$file_ext;
$this->new_url =$this->save_url.$this->new_name.'.'.$file_ext;
//检查错误
if (!empty($this->err)){
$this->show($this->err);
}
//上传
if( !move_uploaded_file($this->file['tmp_name'],$this->new_path) ) {
$this->err= "上传文件出错";
$this->show($this->err);
}
}
//建立目录
function creatdir($Dir)
{
if (is_dir($Dir))
return true;
if (@ mkdir($Dir,0777))
return true;
if (!$this->creatdir(dirname($Dir)))
return false;
return mkdir($Dir,0777);
}
//错误提示
function show($errorstr){
echo "<script language=javascript>alert('$errorstr');location='javascript:history.go(-1);';</script>";
exit();
}
}
?>
送给所有草根的个人站长
上传文件:
up.php
<?php
//验证
if(empty($_COOKIE['login'])) {
die('index');
}
//加载类
include_once("_inc/class_uppic.php");
//上传
if(!empty($_GET['action']) && $_GET['action'] == "upload"){
$up->upload($_FILES['imgfile']);
echo "上传成功!文件名:{$up->new_url} <a href=?yes>重新上传</a>";
}
?>
<form action="?action=upload" method="post" enctype="multipart/form-data">
图片来源:<input type="file" name="imgfile" style="width:200px;height:25px;">
<input type="submit" name="submit" value="上传" style="width:50px;height:25px;">
//================分割线==============
类文件:
class_uppic.php
<?php
/*
power by jtxxol.com
2011-06-20
*/
$up=new class_uppic;
class class_uppic{
function __construct(){
//保存目录
$this->save_path='/uploadfile/';
//文件链接
$this->save_url='/uploadfile/';
//允许大小 300k
$this->allow_size=300000;
//允许类型
$this->allow_ext='jpg,jpeg,gif,png';
//新文件名
$this->new_name=date("YmdHis").rand(10,99);
//初始化错误提示
$this->err='';
}
//上传
function upload($arr){
$this->file=$arr;
//初始化设置
$this->allow_ext=explode(',',$this->allow_ext);
$this->save_path=$_SERVER['DOCUMENT_ROOT'].$this->save_path.date('Y-m').'/';
$this->save_url=$this->save_url.date('Y-m').'/';
//获得扩展名
$temp_arr = explode(".", $this->file['name']);
$file_ext = array_pop($temp_arr);
$file_ext = trim(strtolower($file_ext));
//检查类型
if(!in_array($file_ext,$this->allow_ext)){
$this->err="上传图片类型错误";
}
//检查大小
if($this->file['size']>$this->allow_size){
$this->err="文件超出限制大小";
}
//递归建立目录
$this->creatdir($this->save_path);
//上传后的路径
$this->new_path=$this->save_path.$this->new_name.'.'.$file_ext;
$this->new_url =$this->save_url.$this->new_name.'.'.$file_ext;
//检查错误
if (!empty($this->err)){
$this->show($this->err);
}
//上传
if( !move_uploaded_file($this->file['tmp_name'],$this->new_path) ) {
$this->err= "上传文件出错";
$this->show($this->err);
}
}
//建立目录
function creatdir($Dir)
{
if (is_dir($Dir))
return true;
if (@ mkdir($Dir,0777))
return true;
if (!$this->creatdir(dirname($Dir)))
return false;
return mkdir($Dir,0777);
}
//错误提示
function show($errorstr){
echo "<script language=javascript>alert('$errorstr');location='javascript:history.go(-1);';</script>";
exit();
}
}
?>
相关文章推荐
- PHP分页初探 一个最简单的PHP分页代码实现
- PHP分页初探 一个最简单的PHP分页代码的简单实现
- 超简单的php上传类代码
- php和数据库结合的一个 简单的web实例 代码分析 (php初学者)
- 一个简单的php分页类代码(转载)
- 百度大脑和图灵机器人制作一个简单的自动聊天机器人【PHP代码】
- php和数据库结合的一个简单的web实例 代码分析 (php初学者)
- PHP分页初探 一个最简单的PHP分页代码的简单实现
- 一个简单至极的PHP缓存类代码
- 一个简单的PHP验证码实现代码
- php和数据库结合的一个简单的web实例 代码分析 (php初学者)
- php和数据库结合的一个简单的web实例 代码分析 (php初学者)
- 一款简单PHP图片上传类带图片显示代码
- 一个简单的php MVC留言本实例代码(必看篇)
- php的一个简单加密解密代码
- php和数据库结合的一个 简单的web实例 代码分析 (php初学者)
- 发一个flash+PHP的简单上传代码
- php的一个简单加密解密代码
- 一个简单至极的PHP缓存类代码
- 【旧代码整理】做的一个简单的php mvc框架