PHP文件上传
1、upload.php
复制代码 代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ddd</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<!--文件上传要注意:1、要有enctyp,2、method="post"-->
<form enctype="multipart/form-data" action="uploadProcess.php" method="post" >
<table>
<tr><td>请填写用户名</td><td><input type="text" name="username"></td></tr>
<tr><td>请简单介绍文件</td><td><textarea rows="7" cols="50" name="fileintro" style="width:300px;"></textarea></td></tr>
<tr><td>请上传你的文件</td><td><input type="file" name="myfile"></td></tr>
<tr><td colspan="2"><input type="submit" value="上传"><td></tr>
</table>
</form>
</body>
</html>
2、uploadProcess.php
复制代码 代码如下:
<?php
//接收
$username=$_POST['username'];
$fileintro=$_POST['fileintro'];
//echo $username.$fileintro;
//获取文件信息
/* echo "<pre>";
print_r($_FILES);
echo "</pre>";
*/
//获取文件的大小
$file_size=$_FILES['myfile']['size'];
if($file_size>2*1024*1024){
echo "<script type='text/javascript'>window.alert('文件不能大于2M')</script>";
exit();
}
//获取文件类型
$file_type=$_FILES['myfile']['type'];
if($file_type!="image/jpeg" && $file_type!="image/pjpeg"){
echo "文件类型只能是 jpg 格式";
exit();
}
//判断上传是否OK
if(is_uploaded_file($_FILES['myfile']['tmp_name'])){
//得到上传的文件 转存到你希望的目录
$upload_file=$_FILES['myfile']['tmp_name'];
//防止图片覆盖问题,为每个用户建立一个文件夹
$user_path=$_SERVER['DOCUMENT_ROOT']."/file/up/".$username;
if(!file_exists($user_path)){
mkdir ($user_path);
}
//$move_to_file=$user_path."/".$_FILES['myfile']['name'];
//防止用户上传用户名相同的问题
$file_true_name=$_FILES['myfile']['name'];
$move_to_file=$user_path."/".time().rand(1,1000).substr($file_true_name,strripos($file_true_name,"."));
//echo $upload_file.$move_to_file;
//中文要转码
if(move_uploaded_file($upload_file,iconv("utf-8","gb2312","$move_to_file"))){
echo $_FILES['myfile']['name']."上传成功";
}else{
echo "上传失败";
}
}else{
echo "上传失败";
}
?>
3、封装:
复制代码 代码如下:
<?php
class Upload{
public $upload_name; //上传文件名
public $upload_tmp_path; //上传文件保存到服务器的temp路径
public $file_size;
public $file_type;
public $file_save_path;
function __construct(){
$this->upload_name=$_FILES['myfile']['name'];
$this->upload_tmp_path=$_FILES['myfile']['tmp_name'];
$this->file_size=$_FILES['myfile']['size'];
$this->file_type=$_FILES['myfile']['type'];
$this->allow_file_type = array('jpeg','jpg','png','gif','bmp','doc','zip','rar','txt','wps','xlsx','ppt');
$this->file_save_path=$_SERVER['DOCUMENT_ROOT']."/file/up/";
}
public function upload_file($username){
//判断文件大小
if($this->file_size>2*1024*1024){
echo "<script type='text/javascript'>window.alert('文件不能大于2M')</script>";
exit();
}
//获取文件类型
/* if($this->file_type!="image/jpeg" && $this->file_type!="image/pjpeg"){
echo "文件类型只能是 jpg 格式";
exit();
}
*/ //获取文件的扩展名
$file_type=$this->getFileExt($this->upload_name);
if(!in_array($file_type,$this->allow_file_type)){
echo "上传文件类型格式错误";
exit();
}
//判断上传是否OK
if(is_uploaded_file($this->upload_tmp_path)){
//防止图片覆盖问题,为每个用户建立一个文件夹
$user_path=$this->file_save_path.$username;
if(!file_exists($user_path)){
mkdir ($user_path);
}
//$move_to_file=$user_path."/".$_FILES['myfile']['name'];
//防止用户上传用户名相同的问题
//$file_true_name=$_FILES['myfile']['name'];
$move_to_file=$user_path."/".time().rand(1,1000).substr($this->upload_name,strripos($this->upload_name,"."));
//echo $upload_file.$move_to_file;
//中文要转码
if(move_uploaded_file($this->upload_tmp_path,iconv("utf-8","gb2312","$move_to_file"))){
echo $this->upload_name."上传成功";
}else{
echo "上传失败";
}
}else{
echo "上传失败";
}
}
//获取文件的扩展名
public function getFileExt($filename){
$fileExt=pathinfo($filename);
return $fileExt["extension"];
}
}
?>
1、upload.php
复制代码 代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ddd</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<!--文件上传要注意:1、要有enctyp,2、method="post"-->
<form enctype="multipart/form-data" action="uploadProcess.php" method="post" >
<table>
<tr><td>请填写用户名</td><td><input type="text" name="username"></td></tr>
<tr><td>请简单介绍文件</td><td><textarea rows="7" cols="50" name="fileintro" style="width:300px;"></textarea></td></tr>
<tr><td>请上传你的文件</td><td><input type="file" name="myfile"></td></tr>
<tr><td colspan="2"><input type="submit" value="上传"><td></tr>
</table>
</form>
</body>
</html>
2、uploadProcess.php
复制代码 代码如下:
<?php
//接收
$username=$_POST['username'];
$fileintro=$_POST['fileintro'];
//echo $username.$fileintro;
//获取文件信息
/* echo "<pre>";
print_r($_FILES);
echo "</pre>";
*/
//获取文件的大小
$file_size=$_FILES['myfile']['size'];
if($file_size>2*1024*1024){
echo "<script type='text/javascript'>window.alert('文件不能大于2M')</script>";
exit();
}
//获取文件类型
$file_type=$_FILES['myfile']['type'];
if($file_type!="image/jpeg" && $file_type!="image/pjpeg"){
echo "文件类型只能是 jpg 格式";
exit();
}
//判断上传是否OK
if(is_uploaded_file($_FILES['myfile']['tmp_name'])){
//得到上传的文件 转存到你希望的目录
$upload_file=$_FILES['myfile']['tmp_name'];
//防止图片覆盖问题,为每个用户建立一个文件夹
$user_path=$_SERVER['DOCUMENT_ROOT']."/file/up/".$username;
if(!file_exists($user_path)){
mkdir ($user_path);
}
//$move_to_file=$user_path."/".$_FILES['myfile']['name'];
//防止用户上传用户名相同的问题
$file_true_name=$_FILES['myfile']['name'];
$move_to_file=$user_path."/".time().rand(1,1000).substr($file_true_name,strripos($file_true_name,"."));
//echo $upload_file.$move_to_file;
//中文要转码
if(move_uploaded_file($upload_file,iconv("utf-8","gb2312","$move_to_file"))){
echo $_FILES['myfile']['name']."上传成功";
}else{
echo "上传失败";
}
}else{
echo "上传失败";
}
?>
3、封装:
复制代码 代码如下:
<?php
class Upload{
public $upload_name; //上传文件名
public $upload_tmp_path; //上传文件保存到服务器的temp路径
public $file_size;
public $file_type;
public $file_save_path;
function __construct(){
$this->upload_name=$_FILES['myfile']['name'];
$this->upload_tmp_path=$_FILES['myfile']['tmp_name'];
$this->file_size=$_FILES['myfile']['size'];
$this->file_type=$_FILES['myfile']['type'];
$this->allow_file_type = array('jpeg','jpg','png','gif','bmp','doc','zip','rar','txt','wps','xlsx','ppt');
$this->file_save_path=$_SERVER['DOCUMENT_ROOT']."/file/up/";
}
public function upload_file($username){
//判断文件大小
if($this->file_size>2*1024*1024){
echo "<script type='text/javascript'>window.alert('文件不能大于2M')</script>";
exit();
}
//获取文件类型
/* if($this->file_type!="image/jpeg" && $this->file_type!="image/pjpeg"){
echo "文件类型只能是 jpg 格式";
exit();
}
*/ //获取文件的扩展名
$file_type=$this->getFileExt($this->upload_name);
if(!in_array($file_type,$this->allow_file_type)){
echo "上传文件类型格式错误";
exit();
}
//判断上传是否OK
if(is_uploaded_file($this->upload_tmp_path)){
//防止图片覆盖问题,为每个用户建立一个文件夹
$user_path=$this->file_save_path.$username;
if(!file_exists($user_path)){
mkdir ($user_path);
}
//$move_to_file=$user_path."/".$_FILES['myfile']['name'];
//防止用户上传用户名相同的问题
//$file_true_name=$_FILES['myfile']['name'];
$move_to_file=$user_path."/".time().rand(1,1000).substr($this->upload_name,strripos($this->upload_name,"."));
//echo $upload_file.$move_to_file;
//中文要转码
if(move_uploaded_file($this->upload_tmp_path,iconv("utf-8","gb2312","$move_to_file"))){
echo $this->upload_name."上传成功";
}else{
echo "上传失败";
}
}else{
echo "上传失败";
}
}
//获取文件的扩展名
public function getFileExt($filename){
$fileExt=pathinfo($filename);
return $fileExt["extension"];
}
}
?>
标签:
PHP,文件上传
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“PHP 文件编程综合案例-文件上传的实现”评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?