以下程式碼基於PHP 研習中的實作練習下的檔案上傳程式碼。
1.
於config.php設定存放檔案的資料夾及檔案限制大小。(須先自行於同目錄下建立"uploads"資料夾哦~)
<?php //存放檔案的資料夾,這裡我是開一個「uploads」的資料夾來存放上傳的檔案。// $cfg_path = "uploads/"; //上傳檔案大小限制,可根據自己的需求來作修改// $cfg_max_size = 2*1024*1024; ?>
2.
work.php以_FILES函數處裡文件上傳,並用move_uploaded_file()函數將資料上傳至指定資料夾中,其中還包含了上傳後的檔案命名。<?php
include ("config.php"); //插入config.php
//如果檔案上傳有錯誤,可以顯示錯誤代碼。
if ($_FILES['upfile']['error'] > 0)
{
echo "Error: " . $_FILES['upfile']['error'] . "<br />";
}
else
{
//上傳檔案的原始名稱。
$upfile_name = $_FILES['upfile']['name'];
//上傳的檔案原始大小。
$upfile_size = $_FILES['upfile']['size'];
//上傳的檔案類型。
$upfile_type = $_FILES['upfile']['type'];
//上傳檔案後的暫存資料夾位置。
$upfile_tmp_name = $_FILES['upfile']['tmp_name'];
}
//==== 檢查是否確實有上傳檔案 ====//
if($upfile_size==0){
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
</head>
<body>
<script language=javascript>
alert("請確實選擇要上傳的檔案喔!");
history.go(-1); //返回
</script>
</body>
</html>
<?php
}
//檢查上傳的檔案是否有超過限制的大小
if($upfile_size>$cfg_max_size){
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
</head>
<body>
<script language="javascript">
alert("上傳檔案的大小不得超過 <? echo $cfg_max_size /1024/1024 ?> MB,\n請您重新上傳!");
history.go(-1);
</script>
</body>
</html>
<?php
exit;
}
$yearDir = date("Y");
$monthDir = date("m");
$dayDir = date("d");
$full_path = "$cfg_path" . "$yearDir/$monthDir/$dayDir/";
$year_path = "$cfg_path" . "$yearDir/";
$month_path = "$cfg_path" . "$yearDir/$monthDir/";
//判定是否有建立"年","月","日" 資料夾,若沒有就建立資料夾
if(!is_dir($year_path))
mkdir($year_path);
if(!is_dir($month_path))
mkdir($month_path);
if(!is_dir($full_path))
mkdir($full_path);
/*
explode(separator,string,limit)
separator 必需。規定在哪里分割字串。
string 必需。要分割的字串。
limit 可選。規定所返回的數組元素的最大數目。
"a.b.c.d.png" => $file_name = abcd
=> $file_subname = png
array[] {"a", "b", "c", "d", "png"} count = 5
*/
$file_arr = explode(".", $_FILES['upfile']['name']);
$file_subname = $file_arr[count($file_arr) - 1]; //副檔名
$file_name = "";
for ($i = 0; $i < count($file_arr) - 1; $i ++ )
$file_name .= $file_arr[$i];
//上傳後檔案命名
$upfile_name = $file_name . '-' . date("His") . '.' . $file_subname;
//====== 檔案上傳至資料夾 =====//
$cp = move_uploaded_file($upfile_tmp_name, "$full_path/$upfile_name");
//判斷是否上傳成功
if($cp)
{
echo "Upload Success!<br>";
echo "檔案大小:".$upfile_size."<br>";
echo "檔案名稱:".$upfile_name;
}
else
{
echo "Fail!";
}
?>
3.
可至Apache資料夾下的bin資料夾中找到php.ini檔,將其date.timezone = UTC改成date.timezone = "Asia/Taipei",此動作是將時間改成台灣的標準時間,修改後將伺服器重新啟動即可。4.
upload.php為使用介面程式碼,其中我為了美化整體畫面套用了Boostrap,你可至bootstrap 網站下載相關.css及.js檔。 ( 使用過程可參考:Bootstrap 於 Asp.net 的基本使用)<?php
include ("config.php"); //插入config.php
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<link rel="stylesheet" type="text/css" href="bootstrap.css" />
<script language="javascript">
function checkform()
{
if(document.thisform.upfile.value=='')
{
alert('您還沒選擇要上傳的檔案喔!')
return false;
}
return true;
}
</script>
</head>
<body>
<br><br>
<center>
<form action="work.php" method="post" enctype="multipart/form-data" name="thisform">
※上傳檔案大小以 <font color=red><?php echo $cfg_max_size/1024/1024; ?></font> MB 為限<br>
<table ><tr><td>
<table class="table table-striped table-bordered table-condensed">
<tr>
<td> 選擇檔案 </td>
<td> <input type="file" name="upfile" class="input-file" size="20" /></td>
</tr>
<tr>
<td>
<input type="hidden" name="work" value="upload" />
<input type="submit" value="確定送出" onclick="return checkform()" class="btn btn-primary" />
</td>
</tr>
</table>
</td></tr></table>
</form>
</body>
</html>
完成圖
回 PHP 目錄
回首頁
沒有留言 :
張貼留言