给初学者提供的PHP文件上传小程序

废话略过,直接上代码:

首先前端代码:index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>文件上传Demo</title>
</head>
<body>
<form method="post" action="upload.php" enctype="multipart/form-data">   
	<table border=0 cellspacing=0 cellpadding=0 align=center width="100%">   
		<tr>    
			<td width=55 height=20 align="center">
				<input type="hidden" name="MAX_FILE_SIZE" value="2000000">文件: 
			</td>   
			<td height="16">    
			<input name="file" type="file"  value="浏览" />              
			<input type="submit" value="上传" name="submit" />   
			</td>   
		</tr>   
	</table>   
</form>
</body>
</html>
接下来是重点:upload.php

<?php
/**
 * @title 文件上传示例
 * @author FeoniX
 */
header("Content-Type:text/html; charset=utf-8");
if($_POST['submit']){
	$upfiles = new Upload();
	$upfiles->upload_file();
}
class Upload{
	public $upload_name;					//上传文件名
	public $upload_tmp_name;				//上传临时文件名
	public $upload_final_name;				//上传文件的最终文件名
	public $upload_target_dir;				//文件被上传到的目标目录
	public $upload_target_path;			    //文件被上传到的最终路径
	public $upload_filetype ;				//上传文件类型
	public $allow_uploadedfile_type;		//允许的上传文件类型
	public $upload_file_size;				//上传文件的大小
	public $allow_uploaded_maxsize=10000000;//允许上传文件的最大值
	//构造函数
	public function __construct()
	{
		$this->upload_name = $_FILES["file"]["name"]; //取得上传文件名
		$this->upload_filetype = $_FILES["file"]["type"];
		$this->upload_tmp_name = $_FILES["file"]["tmp_name"];
		$this->allow_uploadedfile_type = array('jpeg','jpg','png','gif','bmp','doc','xls','csv','zip','rar','txt','wps');
		$this->upload_file_size = $_FILES["file"]["size"];
		$this->upload_target_dir="./upload";
	}
	//文件上传
	public function upload_file()
	{
		$upload_filetype = $this->getFileExt($this->upload_name);//获取文件扩展名
		if(in_array($upload_filetype,$this->allow_uploadedfile_type))//判断文件类型是否符合要求
		{
			if($this->upload_file_size < $this->allow_uploaded_maxsize)//判断文件大小是否超过允许的最大值
			{
				if(!is_dir($this->upload_target_dir))//如果文件上传目录不存在
				{
					mkdir($this->upload_target_dir);//创建文件上传目录
					chmod($this->upload_target_dir,0777);//改权限
				}
				$this->upload_final_name = date("YmdHis").rand(0,100).'.'.$upload_filetype;//生成随机文件名
				$this->upload_target_path = $this->upload_target_dir."/".$this->upload_final_name;//文件上传目标目录
				if(!move_uploaded_file($this->upload_tmp_name,$this->upload_target_path))//文件移动失败
				{
					echo "<font color=red>文件上传失败!</font>";
				}
				else
				{
					echo "<font color=green>文件上传成功!</font>";
				}
			}
			else
			{
				echo("<font color=red>文件太大,上传失败!</font>");
			}
		}
		else
		{
			echo("<font color=red>仅支持一下文件类型,请重新选择:<br>".implode(',',$this->allow_uploadedfile_type)."</font>");
		}
	}
   	/**
     *获取文件扩展名
     *@param String $filename 要获取文件名的文件
     */
   	public function getFileExt($filename){
   		$info = pathinfo($filename);
   		return @$info["extension"];
   	}
}
?>



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值