Maison >développement back-end >tutoriel php >php相本功能实现(包含php图片上传,后台管理,浏览和删除)教程例子
php相册功能实现(包含php图片上传,后台管理,浏览和删除)教程例子
相册功能实现(包含php图片上传,后台管理,浏览和删除)教程例子包括五个部分:
一、相册首页
<meta charset="utf-8"> <title>相册首页</title> <style><span style="color: #000000;"> body{ width:<span style="color: #000000;">800px; margin:0<span style="color: #000000;"> auto; test-align:<span style="color: #000000;">center; } </style> <h1>The simple photo album.</h1> <center> <span style="color: #000000;">php </span><span style="color: #008080;">error_reporting</span>(0<span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$_GET</span>["page"<span style="color: #000000;">]){ </span><span style="color: #800080;">$page</span> = 1<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span> <span style="color: #800080;">$page</span> = <span style="color: #800080;">$_GET</span>["page"<span style="color: #000000;">]; </span><span style="color: #800080;">$filename</span> = "data.dat"<span style="color: #000000;">; </span><span style="color: #800080;">$myfile</span> = <span style="color: #008080;">file</span>(<span style="color: #800080;">$filename</span><span style="color: #000000;">); </span><span style="color: #800080;">$z</span> = <span style="color: #800080;">$myfile</span>[0<span style="color: #000000;">]; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$z</span> == <span style="color: #0000ff;">NULL</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> "目前记录条数为:0"<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$temp</span> = <span style="color: #008080;">explode</span>("||",<span style="color: #800080;">$myfile</span>[0<span style="color: #000000;">]); </span><span style="color: #0000ff;">echo</span> "共有".<span style="color: #800080;">$temp</span>[0]."条内容"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> " "<span style="color: #000000;">; </span><span style="color: #800080;">$p_count</span> = <span style="color: #008080;">ceil</span>(<span style="color: #800080;">$temp</span>[0]/8<span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> "分".<span style="color: #800080;">$_count</span>."页显示"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> " "<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "当前显示第".<span style="color: #800080;">$page</span>."页"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> " "<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$page</span> != <span style="color: #008080;">ceil</span>(<span style="color: #800080;">$temp</span>[0]/8<span style="color: #000000;">)){ </span><span style="color: #800080;">$current_size</span> = 8<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$current_size</span> = <span style="color: #800080;">$temp</span>[0]%8<span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$current_size</span> == 0<span style="color: #000000;">){ </span><span style="color: #800080;">$current_size</span> = 8<span style="color: #000000;">; } </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>ceil(<span style="color: #800080;">$current_size</span>/4);<span style="color: #800080;">$i</span>++<span style="color: #000000;">){ </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$j</span>=0;<span style="color: #800080;">$j</span>$j++<span style="color: #000000;">){ </span><span style="color: #800080;">$temp</span> = <span style="color: #008080;">explode</span>("||", <span style="color: #800080;">$myfile</span>[<span style="color: #800080;">$i</span>*4+<span style="color: #800080;">$j</span>+(<span style="color: #800080;">$page</span>-1)*8<span style="color: #000000;">]); </span><span style="color: #0000ff;">if</span>((<span style="color: #800080;">$i</span>*4+<span style="color: #800080;">$j</span>+(<span style="color: #800080;">$page</span>-1)*8)$z<span style="color: #000000;">){ </span><span style="color: #800080;">$imgfile</span> = "./images/".<span style="color: #800080;">$temp</span>[1<span style="color: #000000;">]; </span><span style="color: #800080;">$flag</span> = <span style="color: #008080;">getimagesize</span>(<span style="color: #800080;">$imgfile</span><span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> "<a href="viewimage.php?id=%22.<span" style="color: #800080;">$temp[0]."><img src="/test/images/%22.<span" style="max-width:90%" alt="php相本功能实现(包含php图片上传,后台管理,浏览和删除)教程例子" >$temp[1<span style="color: #000000;">]; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$flag</span>[0]>180||<span style="color: #800080;">$flag</span>[1]>100<span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> " width=180 height=".<span style="color: #008080;">ceil</span>(<span style="color: #800080;">$flag</span>[1]*180/<span style="color: #800080;">$flag</span>[0<span style="color: #000000;">]); } </span><span style="color: #0000ff;">echo</span> " border=\"0\"></a>"<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">echo</span> ""<span style="color: #000000;">; } } } </span><span style="color: #0000ff;">echo</span> ""<span style="color: #000000;">; } </span><span style="color: #0000ff;">echo</span> "<p>"<span style="color: #000000;">; </span><span style="color: #800080;">$prev_page</span> = <span style="color: #800080;">$page</span> - 1<span style="color: #000000;">; </span><span style="color: #800080;">$next_page</span> = <span style="color: #800080;">$page</span> + 1<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$page</span> ){ <span style="color: #0000ff;">echo</span> "第一页 | "<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">echo</span> "<a href="<span%20style=%22color:%20#800080;%22>%24PATH_INFO</span>?page=1">第一页</a> | "<span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$prev_page</span> ){ <span style="color: #0000ff;">echo</span> "上一页 | "<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">echo</span> "<a href="<span%20style=%22color:%20#800080;%22>%24PATH_INFO</span>?page=<span%20style=%22color:%20#800080;%22>%24prev_page</span>">上一页</a> | "<span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$next_page</span> > <span style="color: #800080;">$p_count</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> "下一页 | "<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">echo</span> "<a href="<span%20style=%22color:%20#800080;%22>%24PATH_INFO</span>?page=<span%20style=%22color:%20#800080;%22>%24next_page</span>">下一页</a> | "<span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$page</span> >= <span style="color: #800080;">$p_count</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> "最后一页</p>\n"<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">echo</span> "<a href="<span%20style=%22color:%20#800080;%22>%24PATH_INFO</span>?page=<span%20style=%22color:%20#800080;%22>%24p_count</span>">最后一页</a>\n"<span style="color: #000000;">; } </span>?></center> <a href="upfile.php">Upload Files</a>
二、后台管理
<meta charset="utf-8"> <title>后台处理页面</title><span style="color: #000000;">php </span><span style="color: #008080;">error_reporting</span>(1<span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$_FILES</span>['upfile']['name'] == <span style="color: #0000ff;">NULL</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> "No file choice."<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<a href="upfile.php">返回</a>"<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$filepath</span> = "/Library/WebServer/Documents/test/images/"<span style="color: #000000;">; </span><span style="color: #800080;">$tmp_name</span> = <span style="color: #800080;">$_FILES</span>['upfile']['tmp_name'<span style="color: #000000;">]; </span><span style="color: #800080;">$filename</span> = <span style="color: #800080;">$filepath</span>.<span style="color: #800080;">$_FILES</span>['upfile']['name'<span style="color: #000000;">];</span><span style="color: #008000;">//</span><span style="color: #008000;"> echo $filename;</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$tmp_name</span>,<span style="color: #800080;">$filename</span><span style="color: #000000;">)){ </span><span style="color: #800080;">$dataname</span> = "data.dat"<span style="color: #000000;">; </span><span style="color: #800080;">$myfile</span> = <span style="color: #008080;">file</span>(<span style="color: #800080;">$dataname</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$myfile</span>[0] == ""<span style="color: #000000;">){ </span><span style="color: #800080;">$fp</span> = <span style="color: #008080;">fopen</span>(<span style="color: #800080;">$dataname</span>, "a+"<span style="color: #000000;">); </span><span style="color: #008080;">fwrite</span>(<span style="color: #800080;">$fp</span>,"1||".<span style="color: #800080;">$_FILES</span>['upfile']['name']."||".<span style="color: #800080;">$_POST</span>["content"]."||".<span style="color: #008080;">date</span>(Y年m月d日)."\n"<span style="color: #000000;">); </span><span style="color: #008080;">fclose</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">); }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$temp</span> = <span style="color: #008080;">explode</span>("||", <span style="color: #800080;">$myfile</span>[0<span style="color: #000000;">]); </span><span style="color: #800080;">$temp</span>[0]++<span style="color: #000000;">; </span><span style="color: #800080;">$fp</span> = <span style="color: #008080;">fopen</span>(<span style="color: #800080;">$dataname</span>, "r"<span style="color: #000000;">); </span><span style="color: #800080;">$line_has</span> = <span style="color: #008080;">fread</span>(<span style="color: #800080;">$fp</span>,<span style="color: #008080;">filesize</span>("<span style="color: #800080;">$dataname</span>"<span style="color: #000000;">)); </span><span style="color: #008080;">fclose</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">); </span><span style="color: #800080;">$fp</span> = <span style="color: #008080;">fopen</span>(<span style="color: #800080;">$dataname</span>, "w"<span style="color: #000000;">); </span><span style="color: #008080;">fwrite</span>(<span style="color: #800080;">$fp</span>,<span style="color: #800080;">$temp</span>[0]."||".<span style="color: #800080;">$_FILES</span>['upfile']['name']."||".<span style="color: #800080;">$_POST</span>["content"]."||".<span style="color: #008080;">date</span>("Y年m月d日")."\n"<span style="color: #000000;">); </span><span style="color: #008080;">fwrite</span>(<span style="color: #800080;">$fp</span>,"<span style="color: #800080;">$line_has</span>"<span style="color: #000000;">); </span><span style="color: #008080;">fclose</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">echo</span> "<p></p>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "指定文件已经上传成功!"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<p></p>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "点<a href="index.php">返回</a>"<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">echo</span> "文件上传失败!"<span style="color: #000000;">; } }</span>?>
三、图片上传
<meta charset="utf-8"> <title>相册上传页面</title> <script language="javascript"> <span style="color: #0000ff;">function<span style="color: #000000;"> Juge(theForm){ <span style="color: #0000ff;">if(theForm.upfile.value==""<span style="color: #000000;">){ alert("Please choice file!"<span style="color: #000000;">); theForm.upfile.<span style="color: #000000;">focus(); <span style="color: #0000ff;">return (<span style="color: #0000ff;">false<span style="color: #000000;">); } <span style="color: #0000ff;">if(theForm.content.value==""<span style="color: #000000;">){ alert("Please input image information!"<span style="color: #000000;">); theForm.content.<span style="color: #000000;">focus(); <span style="color: #0000ff;">return (<span style="color: #0000ff;">false<span style="color: #000000;">); } <span style="color: #0000ff;">if(theForm.content.value.length>60<span style="color: #000000;">){ alert("The information must less than 60 chars."<span style="color: #000000;">); theForm.content.<span style="color: #000000;">focus(); <span style="color: #0000ff;">return (<span style="color: #0000ff;">false<span style="color: #000000;">); } } </script><center> <h1>Image album upload page</h1> <p> <a href="index.php">返回首页</a> </p> <table border="1"> <form enctype="multipart/form-data" action="system.php" method="post" onsubmit="return Juge(this)"> <tr> <td>选择图片:</td> <td><input name="upfile" type="file"></td> </tr> <tr> <td>输入说明:</td> <td> <input name="content" type="text">(*限30字)</td> </tr> <tr> <td colspan="2"> <center> <input type="submit" value="提交"> <input type="reset" value="重置"> </center> </td> </tr> </form> </table> </center>
四、图片浏览
<meta charset="utf-8"> <title>查看图片</title><center> <h1>View images.</h1> <span style="color: #000000;">php </span><span style="color: #008080;">error_reporting</span>(0<span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$_GET</span>["id"<span style="color: #000000;">]){ </span><span style="color: #0000ff;">echo</span> "No assign id."<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<a href="index.php">首页</a>"<span style="color: #000000;">; </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">(); }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span>?> <a href="index.php">返回首页</a> &<span style="color: #000000;">nbsp; </span><a href="delimage.php?id=<?php%20echo%20<span%20style=" color:>$_GET['id'] ?>">删除图片</a><br> <span style="color: #000000;">php </span><span style="color: #800080;">$id</span> = <span style="color: #800080;">$_GET</span>["id"<span style="color: #000000;">]; </span><span style="color: #800080;">$filename</span> = "data.dat"<span style="color: #000000;">; </span><span style="color: #800080;">$myfile</span> = <span style="color: #008080;">file</span>(<span style="color: #800080;">$filename</span><span style="color: #000000;">); </span><span style="color: #800080;">$z</span> = <span style="color: #800080;">$myfile</span>[0<span style="color: #000000;">]; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$z</span> == ""<span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> "目前记录条数为0"<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$temp</span> = <span style="color: #008080;">explode</span>("||", <span style="color: #800080;">$myfile</span>[<span style="color: #800080;">$z</span>-<span style="color: #800080;">$id</span><span style="color: #000000;">]); </span><span style="color: #0000ff;">echo</span> "<p>"."文件名:".<span style="color: #800080;">$temp</span>[1]."</p>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<p><img src="/test/images/%22.<span" style="max-width:90%" alt="php相本功能实现(包含php图片上传,后台管理,浏览和删除)教程例子" >$temp[1]."></p>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<p>图片简介:".<span style="color: #800080;">$temp</span>[2]."</p>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<p>上传日期:".<span style="color: #800080;">$temp</span>[3]."</p>"<span style="color: #000000;">; } } </span>?></center>
五、删除图片
<meta charset="utf-8"> <title>删除图片</title><span style="color: #000000;">php </span><span style="color: #008080;">error_reporting</span>(0<span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$_GET</span>["id"<span style="color: #000000;">]){ </span><span style="color: #0000ff;">echo</span> "No id assign."<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<a href="index.php">首页</a>"<span style="color: #000000;">; </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">(); }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$id</span> = <span style="color: #800080;">$_GET</span>["id"<span style="color: #000000;">]; </span><span style="color: #800080;">$filename</span> = "data.dat"<span style="color: #000000;">; </span><span style="color: #800080;">$myfile</span> = <span style="color: #008080;">file</span>(<span style="color: #800080;">$filename</span><span style="color: #000000;">); </span><span style="color: #800080;">$z</span> = <span style="color: #800080;">$myfile</span>[0<span style="color: #000000;">]; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$z</span> == <span style="color: #0000ff;">NULL</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> "目前记录条数为:0"<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$temp</span> = <span style="color: #008080;">explode</span>("||", <span style="color: #800080;">$myfile</span>[<span style="color: #800080;">$z</span>-<span style="color: #800080;">$id</span><span style="color: #000000;">]); </span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$temp</span><span style="color: #000000;">); </span><span style="color: #800080;">$filepath</span> = "/Library/WebServer/Documents/test/images/"<span style="color: #000000;">; </span><span style="color: #800080;">$imgfile</span> = <span style="color: #800080;">$filepath</span>.<span style="color: #800080;">$temp</span>[1<span style="color: #000000;">]; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$imgfile</span><span style="color: #000000;">; </span><span style="color: #008080;">unlink</span>(<span style="color: #800080;">$imgfile</span><span style="color: #000000;">); </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>$z-<span style="color: #800080;">$id</span>);<span style="color: #800080;">$i</span>++<span style="color: #000000;">){ </span><span style="color: #800080;">$temp2</span> = <span style="color: #008080;">explode</span>("||", <span style="color: #800080;">$myfile</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">]); </span><span style="color: #800080;">$temp2</span>[0]--<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$temp2</span>[0]>0<span style="color: #000000;">) </span><span style="color: #800080;">$text2</span> = <span style="color: #800080;">$text2</span>.<span style="color: #800080;">$temp2</span>[0]."||".<span style="color: #800080;">$temp2</span>[1]."||".<span style="color: #800080;">$temp2</span>[2]."||".<span style="color: #800080;">$temp2</span>[3<span style="color: #000000;">]; } </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=(<span style="color: #800080;">$z</span>-<span style="color: #800080;">$id</span>+1);<span style="color: #800080;">$i</span>$z;<span style="color: #800080;">$i</span>++<span style="color: #000000;">){ </span><span style="color: #800080;">$text1</span> = <span style="color: #800080;">$text1</span>.<span style="color: #800080;">$myfile</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">]; } </span><span style="color: #800080;">$fp</span> = <span style="color: #008080;">fopen</span>(<span style="color: #800080;">$filename</span>, "w"<span style="color: #000000;">); </span><span style="color: #008080;">fwrite</span>(<span style="color: #800080;">$fp</span>, <span style="color: #800080;">$text2</span><span style="color: #000000;">); </span><span style="color: #008080;">fwrite</span>(<span style="color: #800080;">$fp</span>, <span style="color: #800080;">$text1</span><span style="color: #000000;">); </span><span style="color: #008080;">fclose</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> "指定文件已经删除成功!"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<a href="index.php">首页</a>"<span style="color: #000000;">; } }</span>?>