Home  >  Article  >  Backend Development  >  PHP photo album function implementation (including PHP image upload, background management, browsing and deletion) tutorial examples, _PHP tutorial

PHP photo album function implementation (including PHP image upload, background management, browsing and deletion) tutorial examples, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:44:291612browse

PHP photo album function implementation (including PHP picture upload, background management, browsing and deletion) tutorial examples,

PHP album function implementation (including PHP picture upload, background management, browsing and deletion) ) Tutorial example consists of five parts:

1. Album homepage

<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>$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>

2. Backend management

<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>

3. Image upload

<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>

4. Picture browsing

<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>$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>

5. Delete pictures

<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>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1048764.htmlTechArticlePHP photo album function implementation (including php image upload, background management, browsing and deletion) tutorial examples, photo album function implementation ( Including PHP image upload, background management, browsing and deletion) tutorial examples include five...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn