PHP 文件編程綜合案例-文件上傳的實(shí)現(xiàn)
更新時(shí)間:2013年07月03日 09:30:50 作者:
本篇文章是對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>請?zhí)顚懹脩裘?lt;/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'])){
//得到上傳的文件 轉(zhuǎn)存到你希望的目錄
$upload_file=$_FILES['myfile']['tmp_name'];
//防止圖片覆蓋問題,為每個(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'];
//防止用戶上傳用戶名相同的問題
$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)){
//防止圖片覆蓋問題,為每個(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'];
//防止用戶上傳用戶名相同的問題
//$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>請?zhí)顚懹脩裘?lt;/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
復(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'];
//防止圖片覆蓋問題,為每個(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'];
//防止用戶上傳用戶名相同的問題
$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)){
//防止圖片覆蓋問題,為每個(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'];
//防止用戶上傳用戶名相同的問題
//$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 多文件上傳簡單實(shí)例
- php文件上傳的例子及參數(shù)詳解
- 簡單的php文件上傳(實(shí)例)
- php文件上傳的簡單實(shí)例
- PHP設(shè)置圖片文件上傳大小的具體實(shí)現(xiàn)方法
- PHP文件上傳主要代碼講解
- 與文件上傳有關(guān)的php配置參數(shù)總結(jié)
- php多文件上傳功能實(shí)現(xiàn)原理及代碼
- php 文件上傳實(shí)例代碼
- php利用iframe實(shí)現(xiàn)無刷新文件上傳功能的代碼
- php 文件上傳類代碼
- PHP文件上傳后綴名與文件類型對照表整理
- PHP文件上傳原理簡單分析
- php中通過Ajax如何實(shí)現(xiàn)異步文件上傳的代碼實(shí)例
- File, FileReader 和 Ajax 文件上傳實(shí)例分析(php)
- php中關(guān)于普通表單多文件上傳的處理方法
- php多文件上傳下載示例分享
相關(guān)文章
php合并數(shù)組array_merge函數(shù)運(yùn)算符加號(hào)與的區(qū)別
“+”運(yùn)算符和array_merge():array array_merge ( array array1, array array2 [, array ...] ) 都可以合并多個(gè)數(shù)組,但使用過程中有一點(diǎn)小區(qū)別。2008-10-10WordPress中用于獲取文章信息以及分類鏈接的函數(shù)用法
這篇文章主要介紹了WordPress中用于獲取文章信息以及分類鏈接的函數(shù)用法,分別是get_post()和get_category_link()的使用,需要的朋友可以參考下2015-12-12國外PHP程序員的13個(gè)好習(xí)慣小結(jié)
我是一個(gè)PHP新手,只有6個(gè)月的PHP編程經(jīng)歷,并且是在一位經(jīng)過認(rèn)證的zend工程師的指導(dǎo)下完成工作的,每當(dāng)我編寫腳本時(shí),我會(huì)注意一些能讓我做得更好的細(xì)節(jié)2012-02-02php從memcache讀取數(shù)據(jù)再批量寫入mysql的方法
這篇文章主要介紹了php從memcache讀取數(shù)據(jù)再批量寫入mysql的方法,可利用memcache緩解服務(wù)器讀寫壓力,并實(shí)現(xiàn)數(shù)據(jù)庫數(shù)據(jù)的寫入操作,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12PHP實(shí)現(xiàn)視頻文件上傳完整實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)視頻文件上傳的技巧,包含了PHP配置信息的設(shè)計(jì)及大文件的處理,需要的朋友可以參考下2014-08-08