溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

php相冊功能該如何實現(xiàn)

發(fā)布時間:2022-01-19 10:33:11 來源:億速云 閱讀:202 作者:kk 欄目:編程語言

php相冊功能該如何實現(xiàn),針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

php相冊功能的實現(xiàn)方法:1、創(chuàng)建相冊首頁并實現(xiàn)分頁效果;2、創(chuàng)建后臺處理頁面;3、實現(xiàn)相冊上傳頁面;4、定義圖片瀏覽功能;5、通過“unlink($imgfile);”等語句實現(xiàn)刪除圖片功能即可。

本文操作環(huán)境:Windows7系統(tǒng)、PHP7.1版、DELL G3電腦

php相冊功能怎么實現(xiàn)?

相冊功能實現(xiàn)(包含php圖片上傳,后臺管理,瀏覽和刪除)教程例子包括五個部分:

一、相冊首頁

<html>
<head>
    <meta charset="utf-8">
    <title>相冊首頁</title>
    <style>
        body{
            width:800px;
            margin:0 auto;
            test-align:center;
        }
    </style>
</head>
<body>
    <h2>The simple photo album.</h2>
    <center>
    <?php
    error_reporting(0);
    if(!$_GET["page"]){
        $page = 1;
    }else
        $page = $_GET["page"];
        $filename = "data.dat";
        $myfile = file($filename);
        $z = $myfile[0];
        if($z == NULL){
            echo "目前記錄條數(shù)為:0";
        }else{
            $temp = explode("||",$myfile[0]);
            echo "共有".$temp[0]."條內(nèi)容";
            echo "    ";
            $p_count = ceil($temp[0]/8);
            echo "分".$_count."頁顯示";
            echo "    ";
            echo "當前顯示第".$page."頁";
            echo "    ";
            echo "<br>";
            if($page != ceil($temp[0]/8)){
                $current_size = 8;
            }else{
                $current_size = $temp[0]%8;
            }
            if($current_size == 0){
                $current_size = 8;
            }
            for($i=0;$i<ceil($current_size/4);$i++){
                for($j=0;$j<4;$j++){
                    $temp = explode("||", $myfile[$i*4+$j+($page-1)*8]);
                    if(($i*4+$j+($page-1)*8)<$z){
                        $imgfile = "./images/".$temp[1];
                        $flag = getimagesize($imgfile);
                        echo "<a href=viewimage.php?id=".$temp[0]."><img src=/test/images/".$temp[1];
                        if($flag[0]>180||$flag[1]>100){
                            echo " width=180 height=".ceil($flag[1]*180/$flag[0]);
                        }
                        echo " border=\"0\"></a>";
                    }else{
                        echo "";

                    }
                }

            }
            echo "</table>";
        }
        echo "<p>";
        $prev_page = $page - 1;
        $next_page = $page + 1;
        if($page <= 1){
            echo "第一頁 | ";
        }else{
            echo "<a href='$PATH_INFO?page=1'>第一頁</a> | ";
        }
        if($prev_page < 1){
            echo "上一頁 | ";
        }else{
            echo "<a href='$PATH_INFO?page=$prev_page'>上一頁</a> | ";
        }
        if($next_page > $p_count){
            echo "下一頁 | ";
        }else{
            echo "<a href='$PATH_INFO?page=$next_page'>下一頁</a> | ";
        }
        if($page >= $p_count){
            echo "最后一頁</p>\n";
        }else{
            echo "<a href='$PATH_INFO?page=$p_count'>最后一頁</a></p>\n";
        }
    ?>
</center>
    <a href="upfile.php">Upload Files</a>
</body>
</html>

二、后臺管理

<html>
<head>
    <meta charset="utf-8">
    <title>后臺處理頁面</title>
</head>
<body>
<?php
    error_reporting(1);
    if($_FILES['upfile']['name'] == NULL){
        echo "No file choice.";
        echo "<a href='upfile.php'>返回</a>";
    }else{
        $filepath = "/Library/WebServer/Documents/test/images/";
        $tmp_name = $_FILES['upfile']['tmp_name'];
        $filename = $filepath.$_FILES['upfile']['name'];
//        echo $filename;
        if(move_uploaded_file($tmp_name,$filename)){
            $dataname = "data.dat";
            $myfile = file($dataname);

            if($myfile[0] == ""){

                $fp = fopen($dataname, "a+");
                fwrite($fp,"1||".$_FILES['upfile']['name']."||".$_POST["content"]."||".date(Y年m月d日)."\n");
                fclose($fp);
            }else{
                $temp = explode("||", $myfile[0]);
                $temp[0]++;
                $fp = fopen($dataname, "r");
                $line_has = fread($fp,filesize("$dataname"));
                fclose($fp);
                $fp = fopen($dataname, "w");
                fwrite($fp,$temp[0]."||".$_FILES['upfile']['name']."||".$_POST["content"]."||".date("Y年m月d日")."\n");
                fwrite($fp,"$line_has");
                fclose($fp);
            }

            echo "<p></p>";
            echo "指定文件已經(jīng)上傳成功!";
            echo "<p></p>";
            echo "點<a href='index.php'>返回</a>";
        }else{
            echo "文件上傳失敗!";
        }
    }
?>
</body>
</html>

三、圖片上傳

<html>
<head>
    <meta charset="utf-8">
    <title>相冊上傳頁面</title>
</head>
<body>
    <script language="javascript">
        function Juge(theForm){
            if(theForm.upfile.value==""){
                alert("Please choice file!");
                theForm.upfile.focus();
                return (false);
            }
            if(theForm.content.value==""){
                alert("Please input image information!");
                theForm.content.focus();
                return (false);
            }
            if(theForm.content.value.length>60){
                alert("The information must less than 60 chars.");
                theForm.content.focus();
                return (false);
            }
        }
    </script>
<center>
    <h2>Image album upload page</h2>
    <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>
    <h2>View images.</h2>
    <?php
    error_reporting(0);
    if(!$_GET["id"]){
        echo "No assign id.";
        echo "<a href='index.php'>首頁</a>";
        exit();
    }else{
        ?>
        <a href="index.php">返回首頁</a>    
        <a href="delimage.php?id=<?php echo $_GET['id'] ?>">刪除圖片</a><br>
    <?php
        $id = $_GET["id"];
        $filename = "data.dat";
        $myfile = file($filename);
        $z = $myfile[0];
        if($z == ""){
            echo "目前記錄條數(shù)為0";
        }else{
            $temp = explode("||", $myfile[$z-$id]);
            echo "<p>"."文件名:".$temp[1]."</p>";
            echo "<p><img src=/test/images/".$temp[1]."></p>";
            echo "<p>圖片簡介:".$temp[2]."</p>";
            echo "<p>上傳日期:".$temp[3]."</p>";
        }
    }
    ?>
</center>
</body>
</html>

五、刪除圖片

<html>
<head>
    <meta charset="utf-8">
    <title>刪除圖片</title>
</head>
<body>
<?php
    error_reporting(0);
    if(!$_GET["id"]){
        echo "No id assign.";
        echo "<br>";
        echo "<a href='index.php'>首頁</a>";
        exit();
    }else{
        $id = $_GET["id"];
        $filename = "data.dat";
        $myfile = file($filename);
        $z = $myfile[0];
        if($z == NULL){
            echo "目前記錄條數(shù)為:0";
        }else{
            $temp = explode("||", $myfile[$z-$id]);
            print_r($temp);
            $filepath = "/Library/WebServer/Documents/test/images/";
            $imgfile = $filepath.$temp[1];
            echo $imgfile;
            unlink($imgfile);
            for($i=0;$i<($z-$id);$i++){
                $temp2 = explode("||", $myfile[$i]);
                $temp2[0]--;
                if($temp2[0]>0)
                    $text2 = $text2.$temp2[0]."||".$temp2[1]."||".$temp2[2]."||".$temp2[3];
            }
            for($i=($z-$id+1);$i<$z;$i++){
                $text1 = $text1.$myfile[$i];
            }
            $fp = fopen($filename, "w");
            fwrite($fp, $text2);
            fwrite($fp, $text1);
            fclose($fp);
            echo "指定文件已經(jīng)刪除成功!";
            echo "<a href='index.php'>首頁</a>";
        }

    }
?>
</body>
</html>

php是什么語言

php,一個嵌套的縮寫名稱,是英文超級文本預處理語言(PHP:Hypertext Preprocessor)的縮寫。PHP 是一種 HTML 內(nèi)嵌式的語言,PHP與微軟的ASP頗有幾分相似,都是一種在服務器端執(zhí)行的嵌入HTML文檔的腳本語言,語言的風格有類似于C語言,現(xiàn)在被很多的網(wǎng)站編程人員廣泛的運用。

關(guān)于php相冊功能該如何實現(xiàn)問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI