php 檔案上傳執行個體代碼

來源:互聯網
上載者:User
 代碼如下 複製代碼

<!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("上傳圖片失敗:上傳目錄 ".$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('上傳圖片失敗:檔案上傳出錯!',0);
 exit();
}
return $uploadname;
}
else
{
return '';
}
}
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.