PHP伺服器檔案管理工具開發小結(六):使用jQueryUI實現建立、查看和編輯檔案

來源:互聯網
上載者:User

標籤:php   lamp   檔案管理工具   jqueryui   

前面幾節所做的操作都是對伺服器內容的讀取,下面開始探討若干會牽扯到修改伺服器的操作。


首先是在當前檔案夾下建立檔案,對應著檔案夾上方的New File功能表項目。

<li><a href="#" title="new file" onClick="onNewFile()"><img src="images/newfile48.png" class="topmenu" /></a></li>

其響應的onNewFile函數,如果在C/S程式中,可以彈出一個對話方塊,使使用者輸入建立檔案的檔案名稱,然後將請求發送給伺服器端。然而,如何在B/S程式中實現這個對話方塊呢?幸好有jQueryUI。

通過以下代碼引入線上CDN的jQueryUI,也可以考慮儲存到伺服器上後引用,本文的方法可以節省伺服器的資源。

<link rel="stylesheet" href="http://cdn.bootcss.com/jqueryui/1.11.2/jquery-ui.min.css"><link rel="stylesheet" href="http://cdn.bootcss.com/jqueryui/1.11.2/jquery-ui.theme.min.css"><script type="text/javascript" src="http://cdn.bootcss.com/jqueryui/1.11.2/jquery-ui.min.js"></script>

jQueryUI可以通過簡單調用.dialog函數實現對話方塊的彈出。具體到響應onNewFile函數中:

function onNewFile(){$(‘#dialogNewFile‘).dialog({height:‘auto‘,width:‘auto‘,position:{my:‘center‘,at:‘center‘,collision:‘fit‘},modal:false,draggable:true,resizable:true,title:"New File",show:‘slide‘,hide:‘explode‘,buttons:{OK: function() {strNewFileName = $(‘#textNewFileName‘).val();if (strNewFileName == "") return;$.post("query.php", {act:"newFile", file:strNewFileName}, function (data) {        $(‘#spanDirTable‘).html(data);        $( ‘#dialogNewFile‘ ).dialog( "close" );    });},Cancel: function() {        $( this ).dialog( "close" );        }}});}

注意到dialog後面提供了若干參數,這裡不做詳細解釋,感興趣的讀者可以去查閱相關的官方介紹。

同時,在dialog中提供了兩個按鈕,並綁定了點擊時觸發的處理流程,最關鍵的是對OK按鈕的處理:

        OK: function() {strNewFileName = $(‘#textNewFileName‘).val();if (strNewFileName == "") return;$.post("query.php", {act:"newFile", file:strNewFileName}, function (data) {        $(‘#spanDirTable‘).html(data);        $( ‘#dialogNewFile‘ ).dialog( "close" );    });}

其從輸入框textNewFileName中取出了建立檔案的名稱,並向query.php發送請求。接收到資訊後,填充網頁內容,並關閉對話方塊。

當然,只有這個函數並不能實現對話方塊功能,還需要在網頁中加入“原材料”:

<div id="dialogNewFile" style="display:none"><strong>Create File Under Folder:<br/></strong>Please input File Name:<input type="text" id="textNewFileName"/></div>

點擊效果如下:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/59/CA/wKiom1ThoX_x0OObAAEn6xSLLWg070.jpg" title="PHP檔案管理工具6.png" alt="wKiom1ThoX_x0OObAAEn6xSLLWg070.jpg" />

服務端接收該命令後,嘗試建立對應的檔案,並通過alert向使用者返回提示資訊。

PHP中,通過fileexists判斷檔案是否存在,通過fopen可以建立或開啟檔案。

        case "newFile":                $isDirContentView = true;                if (isset($_SESSION["currDir"]))                 {                    $strDirName = $_SESSION["currDir"];                }                else  $strDirName = "/home";                if (isset($_POST["file"]))                {                    $strFileName = $_POST["file"];                    $strFilePath = $strDirName."/".$strFileName;                    $fileNew = fopen($strFilePath, "w");                    if ($fileNew === false)                    {                        echo "<script>alert(‘Create New File \"$strFilePath\" Failed!‘)</script>";                    }                    else                     {                        fclose($fileNew);                        echo "<script>alert(‘Create New File \"$strFilePath\" Succeed!‘)</script>";                    }                }                break;

注意,這裡的$isDirContentView = true會導致觸發前文提到的擷取檔案夾內容列表的操作。

實現效果:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/59/C6/wKioL1Tho66wTuAwAAJNv0kMmIY157.jpg" title="PHP檔案管理工具7.png" alt="wKioL1Tho66wTuAwAAJNv0kMmIY157.jpg" />點擊alert的按鈕,dialog也會關閉。


建立檔案夾也可以用類似的方法解決。

圖片連結:

<li><a href="#" title="new folder" onClick="onNewFolder()"><img src="images/newfolder48.png" class="topmenu" /></a></li>

“原材料”:

<div id="dialogNewFolder" style="display:none"><strong>Create Sub-Folder Under Folder:<br/></strong>Please input Folder Name:<input type="text" id="textNewFolderName"/></div>

onNewFolder函數:

function onNewFolder(){$(‘#dialogNewFolder‘).dialog({height:‘auto‘,width:‘auto‘,position:{my:‘center‘,at:‘center‘,collision:‘fit‘},modal:false,draggable:true,resizable:true,title:"New File",show:‘slide‘,hide:‘explode‘,buttons:{OK: function() {strNewFolderName = $(‘#textNewFolderName‘).val();if (strNewFolderName == "") return;$.post("query.php", {act:"newFolder", file:strNewFolderName}, function (data) {        $(‘#spanDirTable‘).html(data);        $( ‘#dialogNewFolder‘ ).dialog( "close" );    });},Cancel: function() {        $( this ).dialog( "close" );        }}});}

PHP中通過mkdir來建立檔案夾,對應的響應:

        case "newFolder":                $isDirContentView = true;                if (isset($_SESSION["currDir"]))                 {                    $strDirName = $_SESSION["currDir"];                }                else  $strDirName = "/home";                if (isset($_POST["file"]))                {                    $strSubDirName = $_POST["file"];                    $strSubDirPath = $strDirName."/".$strSubDirName;                    if (mkdir($strSubDirPath) === false)                    {                        echo "<script>alert(‘Create New Folder \"$strSubDirPath\" Failed!‘)</script>";                    }                    else                     {                        echo "<script>alert(‘Create New Folder \"$strSubDirPath\" Succeed!‘)</script>";                    }                }                break;

實現效果:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/59/CA/wKiom1ThpCyhUROkAAKSDx0MSCA901.jpg" title="PHP檔案管理工具8.png" alt="wKiom1ThpCyhUROkAAKSDx0MSCA901.jpg" />


查看檔案屬於檔案操作的一部分,需要檔案可讀。下面的程式碼片段在操作欄中添加查看表徵圖:

$viewMode= "content";$encodedFilePath = urlencode($filePath);$info.= "<td><ul class=\"operations\">";    if (is_readable($filePath)) $info.= "<li><a href=\"#\" title=\"view\" onClick=\"onElemAct(‘$viewMode‘,‘$encodedFilePath‘)\"><img src=\"images/view32.png\" alt=\"\" class=\"tabmenu\"/></a></li>";

注意,實際響應按鈕事件為onElemAct(‘content‘,‘urlencode($filePath)‘)。

這裡通過onElemAct,在用戶端JS中對針對檔案夾下項目的操作統一起來。

下面是對查看內容的響應:

function onElemAct(strViewMode, strViewPath){switch (strViewMode){case "content":$.post("query.php", {act:"content", file:strViewPath}, function (data){$(‘#textContent‘).text(""+data);$(‘#dialogContent‘).dialog({height:‘auto‘,width:‘auto‘,position:{my:‘center‘,at:‘center‘,collision:‘fit‘},modal:false,draggable:true,resizable:true,title:‘View Content‘,show:‘slide‘,hide:‘explode‘});});break;

同建立檔案不同的是,這裡首先POST請求檔案內容,然後將檔案內容填入textContent地區中,再顯示dialogContent來彈出對話方塊。對應的“原材料”為:

<div id="dialogContent" style="display:none"><textarea class="text-dialog" id="textContent"></textarea></div>

服務端通過file_get_content可以擷取檔案的內容。這裡JS端使用text而不是html裝入內容可以有效避免觸發服務端檔案的html標記。

            case "content":                $isDirContentView = false;                if (isset($_POST["file"]))                {                    $strFileName = urldecode($_POST["file"]);                    echo file_get_contents($strFileName);                }                break;

這裡$isDirContentView = false;可以避免重新整理檔案夾內容,因為不需要返回該內容。

具體效果:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/59/CA/wKiom1Thp6ex3ZjQAAL4SHxHGuU562.jpg" title="PHP檔案管理工具9.png" alt="wKiom1Thp6ex3ZjQAAL4SHxHGuU562.jpg" />

編輯文檔需要結合建立文檔和查看文檔中的技術:首先請求擷取文檔內容,在編輯後通過按鈕事件將編輯後的內容發送到伺服器,並返回修改結果。

首先是按鈕表徵圖,接在查看按鈕後面,注意可編輯的檔案必須可寫:

if (is_writable($filePath)) $info.= "<li><a href=\"#\" title=\"edit\" onClick=\"onElemAct(‘edit‘,‘$encodedFilePath‘)\"><img src=\"images/edit32.png\" alt=\"\" class=\"tabmenu\"/></a></li>";

在onElemAct中響應該事件:

            case "edit":$.post("query.php", {act:"content", file:strViewPath}, function (data){$(‘#textEdit‘).text(""+data);$(‘#dialogEdit‘).dialog({height:‘auto‘,width:‘auto‘,position:{my:‘center‘,at:‘center‘,collision:‘fit‘},modal:false,draggable:true,resizable:true,title:‘Edit Content‘,show:‘slide‘,hide:‘explode‘,buttons:{OK: function() {strEditContent = $(‘#textEdit‘).val();$.post("query.php", {act:"editFile", file:strViewPath, content:strEditContent}, function (data) {        $(‘#spanDirTable‘).html(data);        $( ‘#dialogEdit‘ ).dialog( "close" );    });},Cancel: function() {        $( this ).dialog( "close" );        }}});});break;

對應的原材料:

<div id="dialogEdit" style="display:none"><textarea class="text-dialog" id="textEdit"></textarea></div>

在伺服器中,通過file_get_content擷取內容,通過file_put_content修改內容:

case "editFile":                $isDirContentView = true;                if (isset($_SESSION["currDir"]))                 {                    $strDirName = $_SESSION["currDir"];                }                else  $strDirName = "/home";                if (isset($_POST["file"]) && isset($_POST["content"]))                {                    $strFilePath = urldecode($_POST["file"]);                    if (file_put_contents($strFilePath, $_POST["content"]))                    {                        echo "<script>alert(‘File \"$strFilePath\" Edit Succeed!‘)</script>";                    }                    else                     {                                                echo "<script>alert(‘File \"$strFilePath\" Edit Failed!‘)</script>";                    }                }                break;

實現效果:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/59/C6/wKioL1ThqqfSFqpEAAKw-za8L7M299.jpg" title="PHP檔案管理工具10.png" alt="wKioL1ThqqfSFqpEAAKw-za8L7M299.jpg" />

本文出自 “Accplayer的小地方” 部落格,請務必保留此出處http://accplaystation.blog.51cto.com/9917407/1614674

PHP伺服器檔案管理工具開發小結(六):使用jQueryUI實現建立、查看和編輯檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.