PHP 簡易檔案 上傳/分享系統[有在自己的虛擬主機器上實現]
功能說明:只允許有帳號的人上傳,要分享只能在上船後複製連結貼給有需要的人
upload/index.html
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<title>登錄頁面</title>
<style type=”text/css”>
html{font-size:20px;}
fieldset{width:300px; margin: 0 auto;}
legend{font-weight:bold; font-size:24px;}
.label{float:left; width:70px; margin-left:20px;}
.left{margin-left:180px;font-size:18px;}
.input{width:150px;}
span{color: #666666;}
</style>
<script language=JavaScript>
<!–
function InputCheck(LoginForm)
{
if (LoginForm.username.value == “”)
{
alert(“請輸入帳號!”);
LoginForm.username.focus();
return (false);
}
if (LoginForm.password.value == “”)
{
alert(“請輸入密碼!”);
LoginForm.password.focus();
return (false);
}
}
//–>
</script>
</head>
<body>
<div>
<fieldset>
<legend>登錄頁面</legend>
<form name=”LoginForm” method=”post” action=”login.php” onSubmit=”return InputCheck(this)”>
<p>
<label for=”username” class=”label”>帳 號:</label>
<input id=”username” name=”username” type=”text” class=”input” />
<p/>
<p>
<label for=”password” class=”label”>密 碼:</label>
<input id=”password” name=”password” type=”password” class=”input” />
<p/>
<p>
<input type=”submit” name=”submit” value=” 確 定 ” class=”left” />
</p>
</form>
</fieldset>
</div>
</body>
</html>
|
upload/login.php
<?php
header(‘content-type:text/html;charset=utf-8’);
session_start();
echo “<font size=’24’ face=’Arial’>”;//PHP放大字體
//註銷登錄
if(isset($_GET[‘action’] ))
{
if( $_GET[‘action’] == “logout”)
{
unset($_SESSION[‘userid’]);
unset($_SESSION[‘username’]);
header(“Location:index.html”);
exit;
}
}
//登錄
if(!isset($_POST[‘submit’])){
header(“Location:index.html”);
}
$username = htmlspecialchars($_POST[‘username’]);
$password = $_POST[‘password’];
$result=$username.’,’.$password;
//echo $result.'<br>’;
$fp=fopen(“u_p.txt”,”r”);
$check=0;
while (!feof($fp))
{
$contents = fgets($fp, 300);//一次一行
//echo $contents.'<br>’;
if (false !== ($rst = strpos($contents, $result))) {
//登錄成功
$_SESSION[‘username’] = $username;
$check=1;
break;
}
}
fclose($fp);
if($check==1)
{
header(“Location:up_down.php”);
}
else
{
exit(‘登錄失敗!點擊此處 <a href=”javascript:history.back(-1);”>返回</a> 重試’);
}
?>
|
upload/u_p.txt
liaojash,XXXXXOOOOO
jashliao,OOOOOXXXX
|
upload/up_down.php
<?php
session_start();
//檢測是否登錄,若沒登錄則轉向登錄介面
if(!isset($_SESSION[‘username’])){
header(“Location:index.html”);
exit();
}
?>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<title>檔案上傳</title>
</head>
<body>
<form action=”upload.php” method=”post” enctype=”multipart/form-data”>
只限英文檔名:<input type=”file” name=”file”><br>
<input type=”submit” value=”上傳”>
</form>
<?php
echo ‘可下載檔案清單:<br>’;
$Vhost_Dir=opendir(“./upload/”);
while($file=readdir($Vhost_Dir)){
if ($file != “.” && $file != “..”)
{
echo ‘<a href=./upload/’.$file.’>’.$file.'</a><br>’;
}
}
closedir($Vhost_Dir);
?>
</body>
</html>
|
upload/upload.php
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<?php
header(‘content-type:text/html;charset=utf-8’);
if($_FILES[‘file’][‘error’]>0){
echo “Error: ” . $_FILES[‘file’][‘error’];
exit(“檔案上傳失敗!”);//如果出現錯誤則停止程式
}
else{
echo “檔案名稱: ” . $_FILES[‘file’][‘name’].”<br/>”;
echo “檔案類型: ” . $_FILES[‘file’][‘type’].”<br/>”;
echo “檔案大小: ” . ($_FILES[‘file’][‘size’] / 1024).” Kb<br />”;
echo “暫存名稱: ” . $_FILES[‘file’][‘tmp_name’].”<br/>”;
$name=iconv(“UTF-8”, “big5//TRANSLIT//IGNORE”,”upload/”.$_FILES[‘file’][‘name’]);
echo “path: ” .$name.”<br/>”;
move_uploaded_file($_FILES[‘file’][‘tmp_name’], $name);
exit(‘上傳完成!點擊此處 <a href=”javascript:history.back(-1);”>返回</a> 繼續上傳’);
}
?>
|
實作之後分享網址:http://jashliao.eu/upload/upload/CloudLinux_info.pdf
One thought on “PHP 簡易檔案 上傳/分享系統[有在自己的虛擬主機器上實現]”
PHP 特定目錄下的檔案列表 (檔案 列表)