我学习PHP做的第一个程序是文件管理,现在拿出来给大家,有什么缺点请指出来,这样有利于大家的学习和进步。也希望能对新手有所帮助。
index.php
<?php
if(isset($_GET['path']) && strlen(trim($_GET['path']))!=0){
$dir = $_GET['path'];
}else{
$dir = "/";
}
$pageSize = 20; //
$pageNum = 1; //
$pageTitle = 1; //
$fileTitle = 0; //
$folderTitle = 0; //
$currFileNum = 0; //
$currFolerNum = 0; //
if(isset($_GET['pagenum']))$pageNum = $_GET['pagenum'];
if($pageNum<1)$pageNum=1;
//echo $pageNum;
?>
<html>
<head>
<title>File Administrator System</title>
<script language="javascript">
<!--
function ReName(dir,filename){
var newName;
//return false;
newName = prompt("Input the new name( include the suffix, such as\".Htm\".)", filename);
if(newName == null)return false;
if(newName.length<4){
alert("The document is illegal, pleasing input afresh!!!");
return false;
}
var cmd = "cmd.php?cmd=rename&old="+dir+filename+"&new="+dir+newName;
var WinId=window.open(cmd, 'popUpWin', "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=no,width=200,height=200");
if (WinId && WinId.open && !WinId.close){
return false;
}else{
//WinId.close();
location.reload();
}
}
function Del(cmd,dir){
var cmd = "cmd.php?cmd="+cmd+"&dir="+dir;
//alert(cmd);
var WinId=window.open(cmd, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=no,width=200,height=200');
if (WinId && WinId.open && !WinId.close){
return false;
}else{
//WinId.close();
location.reload();
}
}
function checkform(){
var strPath = new String(document.upfileform.upfile.value);
if(strPath.length < 5){
alert("First, choose the top that want the document spread, then click the top spreads!!!");
document.upfileform.upfile.focus();
return false;
}else{
var fileName;
fileName = prompt("Top that input document after spreading, include the suffix, such as'.Htm', if the document is by name empty, then will ascend to spread by the source document."," ");
document.upfileform.up.value = fileName;
document.upfileform.action = "up.php";
document.upfileform.submit();
}//end if
}
function newFolder(dir)
{
var newName;
newName = prompt("Input the document that to establish clip the name!!!","newFolder");
if(newName == null)return false;
var cmd = "cmd.php?cmd=mkdir&dir=" + dir + newName;
var WinId = window.open(cmd, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=no,width=200,height=200');
if (WinId && WinId.open && !WinId.close){
pa = "<div>Builder folder defeated.</div>"
WinId.document.write("");
WinId.document.close();
WinId.document.write(pa);
}else{
//WinId.close();
location.reload();
}
}
function downFile(filePath)
{
WinId = window.open("downFile.php?path="+filePath);
}
//-->
</script>
<style type="text/css">
body {
margin-left: 0px;
margin-top: 5px;
margin-right: 0px;
margin-bottom: 0px;
}
A:link {
TEXT-DECORATION: none;
color: #000000;
}
A:visited {
COLOR: #999999;
TEXT-DECORATION: none
}
A:active {
COLOR: #CCCCCC;
TEXT-DECORATION: none
}
A:hover {
COLOR: #696969;
TEXT-DECORATION: none
}
.tab {
border-top-width: 1px;
border-right-width: 1px;
border-bottom-width: 1px;
border-left-width: 1px;
border-top-style: dashed;
border-right-style: dashed;
border-bottom-style: dashed;
border-left-style: dashed;
border-top-color: #CCCCCC;
border-right-color: #666666;
border-bottom-color: #666666;
border-left-color: #CCCCCC;
font-size: 12px;
font-weight: normal;
color: #000000;
text-decoration: none;
padding-top: 1px;
padding-right: 1px;
padding-bottom: 1px;
padding-left: 1px;
}
.txt {
font-size: 12px;
font-weight: normal;
color: #000000;
text-decoration: none;
}
.style1 {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-weight: bold;
font-size: 18px;
}
</style>
</head>
<body class="txt">
<center>
<span class="style1">File Administrator System
</span>
<table width="770" border="0" cellpadding="3" cellspacing="1" bgcolor="#0099CC">
<tr>
<td><b><font color="#FFFFFF"><?php echo dirname($dir);?></font></td>
</tr>
<tr>
<td height="78" align="center" bgcolor="#eeffff" class="txt">
<table width="740" border="0" cellpadding="1" cellspacing="1" background="ba.gif">
<tr align="center" bgcolor="#FFFFdd" class="txt">
<td width="15"></td>
<td><b>File Name</b></td>
<td width="30"><b>Type</b></td>
<td width="60"><b>Size</b></td>
<td width="160"><b>Edit Time</b></td>
<td colspan="3"><b>Option</b></td>
</tr>
<?php
$rowcolor=1;
//echo "http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']). "/" ;
if ($handle = @opendir($dir)) {
while (false !== ($file = @readdir($handle))) {
if(@filetype($dir.$file)=="file"){
$currfileNum++;
}else{
$currfolderNum++;
}
if (($currfileNum+$currfolderNum)<=$pageNum*$pageSize && ($currfileNum+$currfolderNum)>($pageNum-1)*$pageSize) { //
$rowcolor+=1; //
if($rowcolor%2)
$color="#FFE4E1";
else
$color="#ffffff";
?>
<tr bgcolor=<?php echo $color;?> class="txt">
<td align="right"><?php echo $currfileNum + $currfolderNum;?><img src="RedArrow.gif" width="4" height="7"></td>
<td><?php
if(substr($dir,-1)!="/" && substr($dir,-2)!=".." && substr($dir,-1)!="." && substr($dir,-1)!="\")$dir.="/";
if($file=="."){
if(substr($dir,-1)=="/")$dirTemp = substr($dir,0,strlen($dir)-1);
$dirTemp = substr($dirTemp,0,strrpos($dirTemp,"/"));
if($dirTemp=="")$dirTemp="/";
echo "<a href=index.php?path=".urlencode($dirTemp)." title='Click Up Folder'><font color=#696933>";
}elseif($file==".."){
echo "<a href=index.php?path=".urlencode($dir)." title='Click Reload'><font color=#696933>";
}elseif(filetype($dir.$file)=="file"){
?><div onclick='downFile("<?php echo $dir.$file ;?>")' style="cursor:hand" title="Click Down File"><?php
}else{
$fire.="/";
echo "<a href=index.php?path=".urlencode($dir).urlencode($file)." title='Click Into'><font color=#000000>";
}echo $file."</font></a>";
//echo " ".filetype($dir.$file)."</td>";
echo "</td><td align=right>";
echo @filetype($dir.$file);
echo "</td>";
$temp = @filesize($dir.$file);
$Fs ="b";
if($temp<800) {
$Fs ="b";
//echo $temp."b<br>";
}else{
$temp = $temp/1024;
if($temp<600) {
$Fs ="Kb";
//echo $temp."Kb<br>";
}else{
$temp = $temp/1024;
if($temp<600) {
$Fs ="Mb";
//echo $temp."Mb<br>";
}else{
$temp = $temp/1024;
if($temp<600) {
$Fs ="Gb";
//echo $temp."Gb<br>";
}//end if
}//end if
}//end if
}//end if
if(filetype($dir.$file)=="file")$ot = sprintf("%0.2f",$temp).$Fs;
?>
<td align="right"><?php echo $ot;$ot=Null;?></td>
<td align="center"><?php echo date ("Y-m-d H:i:s",@filectime($dir.$file));?></td>
<td align=center width="35"><div onclick='Del("<?php echo filetype($dir.$file)=="file"?"del":"rmdir" ;?>","<?php echo $dir.$file ;?>")' style="cursor:hand">Kill</div></td>
<td align=center width="35"><div onclick='ReName("<?php echo $dir ;?>","<?php echo $file ;?>")' style="cursor:hand">ReName</div></td>
<td align=center width="35"><?php if(filetype($dir.$file)=="file"){?><div onclick='downFile("<?php echo $dir.$file ;?>")' style="cursor:hand" title="Click Down File">Load</div><?php }?></td>
</tr>
<?php
}//end if
}//end while
$pageTitle = ceil(($currfileNum + $currfolderNum)/$pageSize);
}else{
echo "<center><font color=red>error: not the folder.</font></center>";
exit();
}//end if (opendir)
?>
<form name="upfileform" enctype="multipart/form-data" method="post" target="_blank">
<tr bgcolor=#ffffdd class="txt">
<td align="right"></td>
<td align="center">
<input name="upfile" type="file" class="tab" id="upfile" style="background-color:#fffafa ;font-size:12px ;height:18" size="46">
<input type="hidden" name="up" id="up" value="up">
<input type="hidden" name="dir" id="dir" value="<?php echo $dir ;?>">
<td align="center">
<input type="button" class="tab" style="background-color:#fffafa ;cursor:hand;font-size:12px ;height:18" onClick="checkform()" value="Up File">
</td>
<td align="center">
<div class="tab" style="background-color:#fffafa ;font-size:12px ;height:18;width:48;text-align:center;cursor:hand" onClick="location.reload()">Reload</div>
</td>
<td align="center"><div style="background-color:fffafa ;cursor:hand;text-align:center;height:18;width:120" class="tab"><?php echo date("Y-m-d H:i:s");?></div></td>
<td colspan="3" align=center>
<div onClick='newFolder("<?php echo $dir ;?>")' style="background-color:#fffafa ;cursor:hand;font-size:12px ;height:18;width:80" class="tab">New Folder</div>
</td>
</tr>
</form>
</table>
<?php echo $currfileNum;?>File<b> /\ </b>
<?php echo $currfolderNum;?>Folder<b> /\ </b>
<?php echo $pageTitle;?>Page<b> /\ </b>
<?php echo $pageSize;?>/page<b> /\ </b>
<?php if($pageNum>1){?>
<a href="index.php?path=<?php echo $dir;?>&pagenum=1">First</a><b>/\</b>
<a href="index.php?path=<?php echo $dir;?>&pagenum=<?php echo $pageNum-1;?>">Prev</a><b> /\ </b>
<?php }else{?>
First<b> /\ </b>
Prev<b> /\ </b>
<?php }if($pageNum<$pageTitle){?>
<a href="index.php?path=<?php echo $dir;?>&pagenum=<?php echo $pageNum+1;?>">Next</a><b> /\ </b>
<a href="index.php?path=<?php echo $dir;?>&pagenum=<?php echo $pageTitle;?>">End</a>
<?php }else{?>
Next<b> /\ </b>
End
<?php }?>
</td>
</tr>
</table>
No Copyright; Spreed freedom; Cherish the labor; People Long Live
</center>
</body>
</html>
cmd.php
<?php
if(isset($_GET['cmd'])){
if($_GET['cmd'] == "copy"){
$source = $_GET['from'];
$dest = $_GET['to'];
if(copy($source, $dest)){
echo "Copy file scceed!!!";
}else{
echo "Copy file defeated!!!";
}
}
if($_GET['cmd'] == "del"){
$dir = $_GET['dir'];
if(unlink($dir)){
echo "Delete scceed.file:\"".$dir."\"";
}else{
echo "Delete file defeated!!!";
}
}
if($_GET['cmd'] == "stat"){
$dir = $_GET['dir'];
if($arr=stat($dir)){
echo "<pre>";
echo print_r($arr);
echo "</pre>";
}else{
echo " No the Folder!!!";
}
}
if($_GET['cmd'] == "mkdir"){
$dir = $_GET['dir'];
$mode = $_GET['mode'];
if(@mkdir($dir)){
echo "Scceed builded folder:\"".$dir."\"";
}else{
echo "Defeated builded folder";
}
}
if($_GET['cmd'] == "file"){
$dir = $_GET['dir'];
if(arr=@file($dir]$arr=@file($dir)){
echo "<pre>";
echo print_r($arr);
echo "</pre>";
}else{
echo "No the File!!!";
}
}
if($_GET['cmd'] == "rename"){
$old = $_GET['old'];
$new = $_GET['new'];
if(rename($old,$new)){
echo "Scceed Run,old name:\"".$old."\",rename:\"".$new."\"";
}else{
echo "Defeated!!!";
}
}
if($_GET['cmd'] == "rmdir"){
$dir = $_GET['dir'];
if(rmdir($dir)){
echo "Scceed Run,\"".$dir."\"deleted.";
}else{
echo "Defeated";
}
}
}
?>
downFile.php
<?php
if(isset($_GET['path'])){
$path = ($_GET['path']);
if(file_exists(temp)){
//echo "Here is a folder:temp.<br>";
}else{
echo "Here is not foler:temp<br>";
mkdir(temp);
echo (file_exists(temp))?"Scceed builder new folder \"temp\".<br>":"Defeated builder new folder<br>";
}//end if
if ($handle = opendir("temp")) {
echo "hand<br>";
while (false !== ($file = @readdir($handle))) {//Scan folder "temp"
if(file_exists("temp/".$file) && trim($file)!="." && trim($file)!=".."){ //Judge whether document exsits or not
if(date("d",@filectime("temp/".$file))!=date("d") || date("H",@filectime("temp/".$file))!=date("H") || date("i",@filectime("temp/".$file))-date("i")>10){ //
unlink("temp/".$file);
echo $file."<br>Date";
echo date("d",@filectime("temp/".$file))."/";
echo date("d")."<br>Hour";
echo date("H",@filectime("temp/".$file))."/";
echo date("H")."<br>Minute";
echo date("i",@filectime("temp/".$file))."/";
echo date("i")."<br>";
}//end if
}//end if
}//end while
}//end if
$fileName = (substr($path,strrpos($path,"/")+1,strlen($path)));
$dot = substr($fileName,-4);
if($dot!=".zip" && $dot!=".rar" && $dot!=".exe" && $dot!=".chm" && $dot!=".com")$fileName .=".zip";
$copyOK = copy($path,"temp/".$fileName);
if($copyOK){
$url = "http://" . $_SERVER['HTTP_HOST'] .dirname($_SERVER['PHP_SELF'])."/temp/".$fileName;
echo ($url);
header("location:".$url);
}else{
echo ("Server headles data mistake.");
}//end if
}
//echo "<br>http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']). "/" ;
?>
up.php
<?php
if(isset($_POST['up'])){
$name = trim($_POST['up']);
$uploaddir = $_POST['dir'];
if(substr($uploaddir,-1) != "/") $uploaddir.="/";
if($name == "") $name= $_FILES['upfile']['name'];
$uploadfile = $uploaddir.$name;
echo $uploadfile;
echo "<br>".$_FILES['upfile']['tmp_name'];
print "<pre>";
if (@move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile)) {
print "Up file scceed!!!\n";
print_r($_FILES);
print "<script language=javascript>alert('Up file succeed.Breaking the page document would show.');</script>";
echo $uploadfile;
print "<script language=javascript>window.close();</script>";
} else {
print "<br>Up file defeated!!\n";
print_r($_FILES);
}
print "</pre>";
}
?>
main.css
.tab {
border-top-width: 1px;
border-right-width: 1px;
border-bottom-width: 1px;
border-left-width: 1px;
border-top-style: dashed;
border-right-style: dashed;
border-bottom-style: dashed;
border-left-style: dashed;
border-top-color: #CCCCCC;
border-right-color: #666666;
border-bottom-color: #666666;
border-left-color: #CCCCCC;
font-size: 12px;
font-weight: normal;
color: #000000;
text-decoration: none;
padding-top: 1px;
padding-right: 1px;
padding-bottom: 1px;
padding-left: 1px;
}
.txt {
font-size: 12px;
font-weight: normal;
color: #000000;
text-decoration: none;
}
A:link {
FONT-WEIGHT: COLOR: #000000; TEXT-DECORATION: none
}
A:visited {
FONT-WEIGHT: COLOR: #ff00ff; TEXT-DECORATION: none
}
A:active {
FONT-WEIGHT: COLOR: #00ccff; TEXT-DECORATION: none
}
A:hover {
FONT-WEIGHT: COLOR: #696969; TEXT-DECORATION: none
}