<"/>
php フォト アルバム機能のチュートリアル実装例 (php 画像アップロード、バックグラウンド管理、閲覧、削除を含む)
フォト アルバム機能を実現するチュートリアル例 (php 画像アップロード、バックグラウンド管理、閲覧、削除を含む) には以下が含まれます5 つの部分:
1. アルバムのホームページ
<html><head> <meta charset="utf-8"> <title>相册首页</title> <style><span style="color: #000000;"> body{ width</span>:<span style="color: #000000;">800px; margin</span>:0<span style="color: #000000;"> auto; test</span>-align:<span style="color: #000000;">center; } </span></style></head><body> <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><<span style="color: #008080;">ceil</span>(<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><4;<span style="color: #800080;">$j</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;">$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)<<span style="color: #800080;">$z</span><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=".<span style="color: #800080;">$temp</span>[0]."><img src=/test/images/".<span style="color: #800080;">$temp</span>[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> "</table>"<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> <= 1<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 style="color: #800080;">$PATH_INFO</span>?page=1'>第一页</a> | "<span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$prev_page</span> < 1<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 style="color: #800080;">$PATH_INFO</span>?page=<span style="color: #800080;">$prev_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 style="color: #800080;">$PATH_INFO</span>?page=<span style="color: #800080;">$next_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 style="color: #800080;">$PATH_INFO</span>?page=<span style="color: #800080;">$p_count</span>'>最后一页</a></p>\n"<span style="color: #000000;">; } </span>?></center> <a href="upfile.php">Upload Files</a></body></html>
<html><head> <meta charset="utf-8"> <title>后台处理页面</title></head><body><?<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>?></body></html>
<html><head> <meta charset="utf-8"> <title>相册上传页面</title></head><body> <script language="javascript"> <span style="color: #0000ff;">function</span><span style="color: #000000;"> Juge(theForm){ </span><span style="color: #0000ff;">if</span>(theForm.upfile.value==""<span style="color: #000000;">){ alert(</span>"Please choice file!"<span style="color: #000000;">); theForm</span>.upfile.<span style="color: #000000;">focus(); </span><span style="color: #0000ff;">return</span> (<span style="color: #0000ff;">false</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">if</span>(theForm.content.value==""<span style="color: #000000;">){ alert(</span>"Please input image information!"<span style="color: #000000;">); theForm</span>.content.<span style="color: #000000;">focus(); </span><span style="color: #0000ff;">return</span> (<span style="color: #0000ff;">false</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">if</span>(theForm.content.value.length>60<span style="color: #000000;">){ alert(</span>"The information must less than 60 chars."<span style="color: #000000;">); theForm</span>.content.<span style="color: #000000;">focus(); </span><span style="color: #0000ff;">return</span> (<span style="color: #0000ff;">false</span><span style="color: #000000;">); } } </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 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 echo <span style="color: #800080;">$_GET</span>['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/".<span style="color: #800080;">$temp</span>[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></body></html>
<html><head> <meta charset="utf-8"> <title>删除图片</title></head><body><?<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><(<span style="color: #800080;">$z</span>-<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><<span style="color: #800080;">$z</span>;<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>?></body></html>