PHP 文件編程綜合案例-文件上傳的實(shí)現(xiàn)
更新時(shí)間:2013年07月03日 09:30:50 作者:
本篇文章是對(duì)PHP中文件上傳的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
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>請(qǐng)?zhí)顚懹脩裘?lt;/td><td><input type="text" name="username"></td></tr>
<tr><td>請(qǐng)簡(jiǎn)單介紹文件</td><td><textarea rows="7" cols="50" name="fileintro" style="width:300px;"></textarea></td></tr>
<tr><td>請(qǐng)上傳你的文件</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'])){
//得到上傳的文件 轉(zhuǎn)存到你希望的目錄
$upload_file=$_FILES['myfile']['tmp_name'];
//防止圖片覆蓋問(wèn)題,為每個(gè)用戶建立一個(gè)文件夾
$user_path=$_SERVER['DOCUMENT_ROOT']."/file/up/".$username;
if(!file_exists($user_path)){
mkdir ($user_path);
}
//$move_to_file=$user_path."/".$_FILES['myfile']['name'];
//防止用戶上傳用戶名相同的問(wèn)題
$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;
//中文要轉(zhuǎn)碼
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; //上傳文件保存到服務(wù)器的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();
}
*/ //獲取文件的擴(kuò)展名
$file_type=$this->getFileExt($this->upload_name);
if(!in_array($file_type,$this->allow_file_type)){
echo "上傳文件類型格式錯(cuò)誤";
exit();
}
//判斷上傳是否OK
if(is_uploaded_file($this->upload_tmp_path)){
//防止圖片覆蓋問(wèn)題,為每個(gè)用戶建立一個(gè)文件夾
$user_path=$this->file_save_path.$username;
if(!file_exists($user_path)){
mkdir ($user_path);
}
//$move_to_file=$user_path."/".$_FILES['myfile']['name'];
//防止用戶上傳用戶名相同的問(wèn)題
//$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;
//中文要轉(zhuǎn)碼
if(move_uploaded_file($this->upload_tmp_path,iconv("utf-8","gb2312","$move_to_file"))){
echo $this->upload_name."上傳成功";
}else{
echo "上傳失敗";
}
}else{
echo "上傳失敗";
}
}
//獲取文件的擴(kuò)展名
public function getFileExt($filename){
$fileExt=pathinfo($filename);
return $fileExt["extension"];
}
}
?>
1、upload.php
復(fù)制代碼 代碼如下:
<!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>請(qǐng)?zhí)顚懹脩裘?lt;/td><td><input type="text" name="username"></td></tr>
<tr><td>請(qǐng)簡(jiǎn)單介紹文件</td><td><textarea rows="7" cols="50" name="fileintro" style="width:300px;"></textarea></td></tr>
<tr><td>請(qǐng)上傳你的文件</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
復(fù)制代碼 代碼如下:
<?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'])){
//得到上傳的文件 轉(zhuǎn)存到你希望的目錄
$upload_file=$_FILES['myfile']['tmp_name'];
//防止圖片覆蓋問(wèn)題,為每個(gè)用戶建立一個(gè)文件夾
$user_path=$_SERVER['DOCUMENT_ROOT']."/file/up/".$username;
if(!file_exists($user_path)){
mkdir ($user_path);
}
//$move_to_file=$user_path."/".$_FILES['myfile']['name'];
//防止用戶上傳用戶名相同的問(wèn)題
$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;
//中文要轉(zhuǎn)碼
if(move_uploaded_file($upload_file,iconv("utf-8","gb2312","$move_to_file"))){
echo $_FILES['myfile']['name']."上傳成功";
}else{
echo "上傳失敗";
}
}else{
echo "上傳失敗";
}
?>
3、封裝:
復(fù)制代碼 代碼如下:
<?php
class Upload{
public $upload_name; //上傳文件名
public $upload_tmp_path; //上傳文件保存到服務(wù)器的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();
}
*/ //獲取文件的擴(kuò)展名
$file_type=$this->getFileExt($this->upload_name);
if(!in_array($file_type,$this->allow_file_type)){
echo "上傳文件類型格式錯(cuò)誤";
exit();
}
//判斷上傳是否OK
if(is_uploaded_file($this->upload_tmp_path)){
//防止圖片覆蓋問(wèn)題,為每個(gè)用戶建立一個(gè)文件夾
$user_path=$this->file_save_path.$username;
if(!file_exists($user_path)){
mkdir ($user_path);
}
//$move_to_file=$user_path."/".$_FILES['myfile']['name'];
//防止用戶上傳用戶名相同的問(wèn)題
//$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;
//中文要轉(zhuǎn)碼
if(move_uploaded_file($this->upload_tmp_path,iconv("utf-8","gb2312","$move_to_file"))){
echo $this->upload_name."上傳成功";
}else{
echo "上傳失敗";
}
}else{
echo "上傳失敗";
}
}
//獲取文件的擴(kuò)展名
public function getFileExt($filename){
$fileExt=pathinfo($filename);
return $fileExt["extension"];
}
}
?>
您可能感興趣的文章:
- php多文件上傳實(shí)現(xiàn)代碼
- php jquery 多文件上傳簡(jiǎn)單實(shí)例
- php文件上傳的例子及參數(shù)詳解
- 簡(jiǎn)單的php文件上傳(實(shí)例)
- php文件上傳的簡(jiǎn)單實(shí)例
- PHP設(shè)置圖片文件上傳大小的具體實(shí)現(xiàn)方法
- PHP文件上傳主要代碼講解
- 與文件上傳有關(guān)的php配置參數(shù)總結(jié)
- php多文件上傳功能實(shí)現(xiàn)原理及代碼
- php 文件上傳實(shí)例代碼
- php利用iframe實(shí)現(xiàn)無(wú)刷新文件上傳功能的代碼
- php 文件上傳類代碼
- PHP文件上傳后綴名與文件類型對(duì)照表整理
- PHP文件上傳原理簡(jiǎn)單分析
- php中通過(guò)Ajax如何實(shí)現(xiàn)異步文件上傳的代碼實(shí)例
- File, FileReader 和 Ajax 文件上傳實(shí)例分析(php)
- php中關(guān)于普通表單多文件上傳的處理方法
- php多文件上傳下載示例分享
相關(guān)文章
PHP中全局變量global和$GLOBALS[]的區(qū)別分析
$GLOBALS['var']是外部的全局變量本身,global $var是外部$var的同名引用或者指針2012-08-08
php中替換字符串函數(shù)strtr()和str_repalce()的用法與區(qū)別
在php中替換函數(shù)主要有strtr(),str_repalce()這兩個(gè)函數(shù),下面這篇文中主要給大家介紹下這兩者之間的區(qū)別和用法,文中通過(guò)示例代碼介紹的很詳細(xì),有需要的朋友們可以參考借鑒,下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2016-11-11
php自動(dòng)加載的兩種實(shí)現(xiàn)方法
php自動(dòng)加載的兩種實(shí)現(xiàn)方法,需要的朋友可以參考下。2010-06-06
php self,$this,const,static,->的使用
用php這么久了,慚愧的是,原來(lái)自己還一直沒(méi)分清楚這幾個(gè)關(guān)鍵字使用方法。2009-10-10
wamp服務(wù)器訪問(wèn)php非常緩慢的解決過(guò)程
這篇文章主要介紹了wamp服務(wù)器訪問(wèn)php非常緩慢的解決過(guò)程,十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-07-07

