Maison >développement back-end >tutoriel php >php相册功能实现包含php图片上传,后台管理,浏览和删除教程例子
相册功能实现(包含php图片上传,后台管理,浏览和删除)教程例子包括五个部分:
一、相册首页
<meta charset="utf-8"> <title>相册首页</title> <style><span> body{ width:<span>800px; margin:0<span> auto; test-align:<span>center; } </style> <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>ceil(<span>$current_size</span>/4);<span>$i</span>++<span>){ </span><span>for</span>(<span>$j</span>=0;<span>$j</span>$j++<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)$z<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=%22.<span">$temp[0]."><img src="/test/images/%22.<span" alt="php相册功能实现包含php图片上传,后台管理,浏览和删除教程例子" >$temp[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> ""<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> ){ <span>echo</span> "第一页 | "<span>; }</span><span>else</span><span>{ </span><span>echo</span> "<a href="<span>%24PATH_INFO</span>?page=1">第一页</a> | "<span>; } </span><span>if</span>(<span>$prev_page</span> ){ <span>echo</span> "上一页 | "<span>; }</span><span>else</span><span>{ </span><span>echo</span> "<a href="<span>%24PATH_INFO</span>?page=<span>%24prev_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>%24PATH_INFO</span>?page=<span>%24next_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>%24PATH_INFO</span>?page=<span>%24p_count</span>">最后一页</a>\n"<span>; } </span>?> </center> <a href="upfile.php">Upload Files</a>
二、后台管理
<meta charset="utf-8"> <title>后台处理页面</title> <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>?>
三、图片上传
<meta charset="utf-8"> <title>相册上传页面</title> <script language="javascript"> <span>function<span> Juge(theForm){ <span>if(theForm.upfile.value==""<span>){ alert("Please choice file!"<span>); theForm.upfile.<span>focus(); <span>return (<span>false<span>); } <span>if(theForm.content.value==""<span>){ alert("Please input image information!"<span>); theForm.content.<span>focus(); <span>return (<span>false<span>); } <span>if(theForm.content.value.length>60<span>){ alert("The information must less than 60 chars."<span>); theForm.content.<span>focus(); <span>return (<span>false<span>); } } </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" type="file"> <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>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%20echo%20<span>%24_GET</span>%5B'id'%5D%20?>">删除图片</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/%22.<span" alt="php相册功能实现包含php图片上传,后台管理,浏览和删除教程例子" >$temp[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>
五、删除图片
<meta charset="utf-8"> <title>删除图片</title> <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>$z-<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>$z;<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>?>
以上就介绍了php相册功能实现包含php图片上传,后台管理,浏览和删除教程例子,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。