PHP文件上傳小程序 適合初學(xué)者學(xué)習(xí)!
本文實(shí)例為大家分享了PHP文件上傳小程序的具體代碼,供大家參考,具體內(nèi)容如下
廢話略過(guò),直接上代碼:
首先前端代碼:index.html
<html> <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>
接下來(lái)是重點(diǎn):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; //上傳臨時(shí)文件名
public $upload_final_name; //上傳文件的最終文件名
public $upload_target_dir; //文件被上傳到的目標(biāo)目錄
public $upload_target_path; //文件被上傳到的最終路徑
public $upload_filetype ; //上傳文件類型
public $allow_uploadedfile_type;//允許的上傳文件類型
public $upload_file_size; //上傳文件的大小
public $allow_uploaded_maxsize=10000000;//允許上傳文件的最大值
//構(gòu)造函數(shù)
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);//獲取文件擴(kuò)展名
if(in_array($upload_filetype,$this->allow_uploadedfile_type))//判斷文件類型是否符合要求
{
if($this->upload_file_size < $this->allow_uploaded_maxsize)//判斷文件大小是否超過(guò)允許的最大值
{
if(!is_dir($this->upload_target_dir))//如果文件上傳目錄不存在
{
mkdir($this->upload_target_dir);//創(chuàng)建文件上傳目錄
chmod($this->upload_target_dir,0777);//改權(quán)限
}
$this->upload_final_name = date("YmdHis").rand(0,100).'.'.$upload_filetype;//生成隨機(jī)文件名
$this->upload_target_path = $this->upload_target_dir."/".$this->upload_final_name;//文件上傳目標(biāo)目錄
if(!move_uploaded_file($this->upload_tmp_name,$this->upload_target_path))//文件移動(dòng)失敗
{
echo "<font color=red>文件上傳失?。?lt;/font>";
}
else
{
echo "<font color=green>文件上傳成功!</font>";
}
}
else
{
echo("<font color=red>文件太大,上傳失敗!</font>");
}
}
else
{
echo("<font color=red>僅支持一下文件類型,請(qǐng)重新選擇:<br>".implode(',',$this->allow_uploadedfile_type)."</font>");
}
}
/**
*獲取文件擴(kuò)展名
*@param String $filename 要獲取文件名的文件
*/
public function getFileExt($filename){
$info = pathinfo($filename);
return @$info["extension"];
}
}
?>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
開(kāi)源php中文分詞系統(tǒng)SCWS安裝和使用實(shí)例
這篇文章主要介紹了開(kāi)源php中文分詞系統(tǒng)SCWS安裝和使用實(shí)例,需要的朋友可以參考下2014-04-04
ThinkPHP3.2利用QQ郵箱/163郵箱通過(guò)PHPMailer發(fā)送郵件的方法
最近因?yàn)楣ぷ鞯男枰?,要?shí)現(xiàn)給我們的網(wǎng)站用戶發(fā)送郵件的需求,所以下面這篇文章主要給大家介紹了關(guān)于ThinkPHP3.2利用QQ郵箱/163郵箱通過(guò)PHPMailer發(fā)送郵件的方法,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
php二維數(shù)組排序與默認(rèn)自然排序的方法介紹
本篇文章介紹了,在php中二維數(shù)組排序與默認(rèn)自然排序的方法。需要的朋友參考下2013-04-04
使用php的HTTP請(qǐng)求的庫(kù)Requests實(shí)現(xiàn)美女圖片墻
這篇文章主要介紹了使用php的HTTP請(qǐng)求的庫(kù)Requests實(shí)現(xiàn)美女圖片墻的方法,十分簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-02-02
php使用phpoffice/phpspreadsheet導(dǎo)出圖片實(shí)例
這篇文章主要為大家介紹了php使用phpoffice/phpspreadsheet導(dǎo)出圖片實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
YII Framework框架教程之使用YIIC快速創(chuàng)建YII應(yīng)用詳解
這篇文章主要介紹了YII Framework框架教程之使用YIIC快速創(chuàng)建YII應(yīng)用的方法,詳細(xì)分析說(shuō)明了YII Framework框架使用YIIC命令行創(chuàng)建應(yīng)用的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下2016-03-03
簡(jiǎn)單解決新浪SAE無(wú)法上傳文件的問(wèn)題
這篇文章主要介紹了簡(jiǎn)單解決新浪SAE無(wú)法上傳文件的問(wèn)題,不知道到底是新浪的BUG還是我個(gè)人的問(wèn)題,分享給大家吧。2015-05-05

