Home > Backend Development > PHP Tutorial > Comprehensive example of PHP file upload_PHP tutorial

Comprehensive example of PHP file upload_PHP tutorial

WBOY
Release: 2016-07-20 11:12:56
Original
1004 people have browsed it

1、upload.php

<!DOCTYPE HTML <span PUBLIC</span> "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Add文件上传_www.jbxue.com</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" 2、uploadProcess.php</span></p>

<pre class="code"><?<span php
</span><span /*</span><span *
* 文件上传 接收数据
* by www.jbxue.com
</span><span */</span>
<span $username</span>=<span $_POST</span>['username'<span ];
</span><span $fileintro</span>=<span $_POST</span>['fileintro'<span ];

</span><span //</span><span echo $username.$fileintro;
//获取文件信息</span><span 
/*</span><span  echo "<pre class="brush:php;toolbar:false">";
print_r($_FILES);
echo "
"; =['myfile']['size'(>2*1024*1024 "" =['myfile']['type'(!="image/jpeg" && !="image/pjpeg" "文件类型只能是 jpg 格式" ((['myfile']['tmp_name' =['myfile']['tmp_name' =['DOCUMENT_ROOT']."/file/up/".(!( ( =['myfile']['name'=."/".().(1,1000).(,(,"." ((,("utf-8","gb2312","" ['myfile']['name']."上传成功" "上传失败" "上传失败"?>
Copy after login

<?<span php
</span><span /*</span><span *
* 文件上传类
* by www.jbxue.com
</span><span */</span>
<span class</span><span  Upload{
</span><span public</span> <span $upload_name</span>; <span //</span><span 上传文件名</span>
<span public</span> <span $upload_tmp_path</span>; <span //</span><span 上传文件保存到服务器的temp路径</span>
<span public</span> <span $file_size</span><span ;
</span><span public</span> <span $file_type</span><span ;
</span><span public</span> <span $file_save_path</span><span ;
</span><span function</span><span  __construct(){
</span><span $this</span>->upload_name=<span $_FILES</span>['myfile']['name'<span ];
</span><span $this</span>->upload_tmp_path=<span $_FILES</span>['myfile']['tmp_name'<span ];
</span><span $this</span>->file_size=<span $_FILES</span>['myfile']['size'<span ];
</span><span $this</span>->file_type=<span $_FILES</span>['myfile']['type'<span ];
</span><span $this</span>->allow_file_type = <span array</span>('jpeg','jpg','png','gif','bmp','doc','zip','rar','txt','wps','xlsx','ppt'<span );
</span><span $this</span>->file_save_path=<span $_SERVER</span>['DOCUMENT_ROOT']."/file/up/"<span ;
}
</span><span public</span> <span function</span> upload_file(<span $username</span><span ){
</span><span //</span><span 判断文件大小</span>
<span if</span>(<span $this</span>->file_size>2*1024*1024<span ){
</span><span echo</span> "<script type='text/javascript'>window.alert('文件不能大于2M')</script>"<span ;
</span><span exit</span><span ();
}
</span><span //</span><span 获取文件类型</span><span 
/*</span><span  if($this->file_type!="image/jpeg" && $this->file_type!="image/pjpeg"){
echo "文件类型只能是 jpg 格式";
exit();
}
</span><span */</span> <span //</span><span 获取文件的扩展名</span>
<span $file_type</span>=<span $this</span>->getFileExt(<span $this</span>-><span upload_name);
</span><span if</span>(!<span in_array</span>(<span $file_type</span>,<span $this</span>-><span allow_file_type)){
</span><span echo</span> "上传文件类型格式错误"<span ;
</span><span exit</span><span ();
} 
</span><span //</span><span 判断上传是否OK</span>
<span if</span>(<span is_uploaded_file</span>(<span $this</span>-><span upload_tmp_path)){

</span><span //</span><span 防止图片覆盖问题,为每个用户建立一个文件夹 </span>
<span $user_path</span>=<span $this</span>->file_save_path.<span $username</span><span ;
</span><span if</span>(!<span file_exists</span>(<span $user_path</span><span )){
</span><span mkdir</span> (<span $user_path</span><span );
}
</span><span //</span><span $move_to_file=$user_path."/".$_FILES['myfile']['name'];
//防止用户上传用户名相同的问题
//$file_true_name=$_FILES['myfile']['name'];</span>
<span $move_to_file</span>=<span $user_path</span>."/".<span time</span>().<span rand</span>(1,1000).<span substr</span>(<span $this</span>->upload_name,<span strripos</span>(<span $this</span>->upload_name,"."<span ));
</span><span //</span><span echo $upload_file.$move_to_file;
//中文要转码</span>
<span if</span>(<span move_uploaded_file</span>(<span $this</span>->upload_tmp_path,<span iconv</span>("utf-8","gb2312","<span $move_to_file</span>"<span ))){
</span><span echo</span> <span $this</span>->upload_name."上传成功"<span ;
}</span><span else</span><span {
</span><span echo</span> "上传失败"<span ;
}
}</span><span else</span><span {
</span><span echo</span> "上传失败"<span ;
}
}

</span><span //</span><span 获取文件的扩展名</span>
<span public</span> <span function</span> getFileExt(<span $filename</span><span ){
</span><span $fileExt</span>=<span pathinfo</span>(<span $filename</span><span );
</span><span return</span> <span $fileExt</span>["extension"<span ];
}
}
</span>?>
Copy after login

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440411.htmlTechArticle1、upload.php !DOCTYPE HTML "-//W3C//DTD HTML 4.01 Transitional//EN"htmlheadtitleAdd文件上传_www.jbxue.com/titlemeta http-equiv="content-type" content="text/html; charset=UTF-...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template