php 文件上傳實例代碼,本文章為你提供一款經典的php文件上傳類了,并且舉例驗證了這一款文件上傳代碼是可用的哦。
- <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
- <html xmlns="http://www.111cn.net/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=gb2312" />
- <title>無標題文檔</title>
- </head>
- <body>
- <form id="form1" name="form1" enctype="multipart/form-data" method="post" action="">
- <label for="filefield"></label>
- <input type="file" name="filefield" id="filefield" />
- <input type="submit" name="button" id="button" value="文件開始上傳" />
- </form>
- </body>
- </html>
- <?
- /*
- | @param: $dir -- 存放目錄,最后加"/" [字串]
- | @param: $file_var -- 表單變量 [字串]
- | @param: $max_size -- 設定最大上傳值,以k為單位. [整數/浮點數]
- | @param: $type -- 限定后輟名(小寫),多個用"/"隔開,不限定則留空 [字串]
- | @param: $name -- 上傳后命名,留空則為原名,true為系統隨機定名 [布林值]
- | return: 上傳后文件名
- */
- function _asupfiles($dir, $file_var, $max_size='', $type='', $name=false)
- {
- if (!file_exists($dir)) showmsg("上傳圖片失敗:上傳目錄 ".$dir." 不存在!",0);
- if (!is_writable($dir))
- {
- showmsg("上傳圖片失?。荷蟼髂夸?nbsp;".$dir." 無法寫入!",0);
- exit();
- }
- $upfile=& $_files["$file_var"];
- $upfilename = $upfile['name'];
- if (!($upfilename===''))
- {
- if (!is_uploaded_file($upfile['tmp_name']))
- {
- showmsg('上傳圖片失敗:你選擇的文件無法上傳',0);
- exit();
- }
- if ($max_size>0 && $upfile['size']/1024>$max_size)
- {
- showmsg("上傳圖片失?。何募笮〔荒艹^ ".$max_size."kb",0);
- exit();
- }
- $ext_name = strtolower(str_replace(".", "", strrchr($upfilename, ".")));
- if (!($type==='') && strpos($type, $ext_name)===false)
- {
- showmsg("上傳圖片失敗:只允許上傳 ".$type." 的文件!",0);
- exit();
- }
- ($name==true)?$uploadname=time().mt_rand(100,999).".".$ext_name :'';
- ($name==false)?$uploadname=$upfilename:'';
- !is_bool($name)?($uploadname=$name.".".$ext_name):'';
- //$uploadname = $name ? md5(uniqid(rand())).".".$ext_name : $upfilename;
- if (!move_uploaded_file($upfile['tmp_name'], $dir.$uploadname))
- {
- showmsg('上傳圖片失?。何募蟼鞒鲥e!',0);
- exit();
- }
- return $uploadname;
- }
- else
- {
- return '';
- }
- }
- ?>
新聞熱點
疑難解答