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