时间:2021-07-01 10:21:17 帮助过:9人阅读
相册功能实现(包含php图片上传,后台管理,浏览和删除)教程例子包括五个部分:
一、相册首页
相册首页 The simple photo album.
php error_reporting(0); if(!$_GET["page"]){ $page = 1; }else $page = $_GET["page"]; $filename = "data.dat"; $myfile = file($filename); $z = $myfile[0]; if($z == NULL){ echo "目前记录条数为:0"; }else{ $temp = explode("||",$myfile[0]); echo "共有".$temp[0]."条内容"; echo " "; $p_count = ceil($temp[0]/8); echo "分".$_count."页显示"; echo " "; echo "当前显示第".$page."页"; echo " "; echo " Upload Files
"; if($page != ceil($temp[0]/8)){ $current_size = 8; }else{ $current_size = $temp[0]%8; } if($current_size == 0){ $current_size = 8; } for($i=0;$i<ceil($current_size/4);$i++){ for($j=0;$j<4;$j++){ $temp = explode("||", $myfile[$i*4+$j+($page-1)*8]); if(($i*4+$j+($page-1)*8)<$z){ $imgfile = "./images/".$temp[1]; $flag = getimagesize($imgfile); echo "$temp[0].">$temp[1]; if($flag[0]>180||$flag[1]>100){ echo " width=180 height=".ceil($flag[1]*180/$flag[0]); } echo " border=\"0\">"; }else{ echo ""; } } } echo ""; } echo ""; $prev_page = $page - 1; $next_page = $page + 1; if($page <= 1){ echo "第一页 | "; }else{ echo "$PATH_INFO?page=1'>第一页 | "; } if($prev_page < 1){ echo "上一页 | "; }else{ echo "$PATH_INFO?page=$prev_page'>上一页 | "; } if($next_page > $p_count){ echo "下一页 | "; }else{ echo "$PATH_INFO?page=$next_page'>下一页 | "; } if($page >= $p_count){ echo "最后一页
\n"; }else{ echo "$PATH_INFO?page=$p_count'>最后一页\n"; } ?>
二、后台管理
后台处理页面 php error_reporting(1); if($_FILES['upfile']['name'] == NULL){ echo "No file choice."; echo "返回"; }else{ $filepath = "/Library/WebServer/Documents/test/images/"; $tmp_name = $_FILES['upfile']['tmp_name']; $filename = $filepath.$_FILES['upfile']['name'];// echo $filename; if(move_uploaded_file($tmp_name,$filename)){ $dataname = "data.dat"; $myfile = file($dataname); if($myfile[0] == ""){ $fp = fopen($dataname, "a+"); fwrite($fp,"1||".$_FILES['upfile']['name']."||".$_POST["content"]."||".date(Y年m月d日)."\n"); fclose($fp); }else{ $temp = explode("||", $myfile[0]); $temp[0]++; $fp = fopen($dataname, "r"); $line_has = fread($fp,filesize("$dataname")); fclose($fp); $fp = fopen($dataname, "w"); fwrite($fp,$temp[0]."||".$_FILES['upfile']['name']."||".$_POST["content"]."||".date("Y年m月d日")."\n"); fwrite($fp,"$line_has"); fclose($fp); } echo ""; echo "指定文件已经上传成功!"; echo ""; echo "点返回"; }else{ echo "文件上传失败!"; } }?>
三、图片上传
相册上传页面 Image album upload page
返回首页
四、图片浏览
查看图片 View images.
php error_reporting(0); if(!$_GET["id"]){ echo "No assign id."; echo "首页"; exit(); }else{ ?> 返回首页 $_GET['id'] ?>">删除图片
php $id = $_GET["id"]; $filename = "data.dat"; $myfile = file($filename); $z = $myfile[0]; if($z == ""){ echo "目前记录条数为0"; }else{ $temp = explode("||", $myfile[$z-$id]); echo ""."文件名:".$temp[1]."
"; echo "$temp[1].">
"; echo "图片简介:".$temp[2]."
"; echo "上传日期:".$temp[3]."
"; } } ?>
五、删除图片
删除图片 php error_reporting(0); if(!$_GET["id"]){ echo "No id assign."; echo "
"; echo "首页"; exit(); }else{ $id = $_GET["id"]; $filename = "data.dat"; $myfile = file($filename); $z = $myfile[0]; if($z == NULL){ echo "目前记录条数为:0"; }else{ $temp = explode("||", $myfile[$z-$id]); print_r($temp); $filepath = "/Library/WebServer/Documents/test/images/"; $imgfile = $filepath.$temp[1]; echo $imgfile; unlink($imgfile); for($i=0;$i<($z-$id);$i++){ $temp2 = explode("||", $myfile[$i]); $temp2[0]--; if($temp2[0]>0) $text2 = $text2.$temp2[0]."||".$temp2[1]."||".$temp2[2]."||".$temp2[3]; } for($i=($z-$id+1);$i<$z;$i++){ $text1 = $text1.$myfile[$i]; } $fp = fopen($filename, "w"); fwrite($fp, $text2); fwrite($fp, $text1); fclose($fp); echo "指定文件已经删除成功!"; echo "首页"; } }?>