2015-10-18 82 views
0

我有一個基於jQuery的Linux服務器上運行的文件管理器。該代碼包含在script.js文件中。 還有一個小小的index.php,其中包含腳本。使用Ajax從jQuery調用PHP腳本創建一個新文件夾

我在index.php中添加了一個按鈕,並將其偵聽器實現爲腳本。通過按下這個按鈕,我希望偵聽器調用一個php腳本(createfolder.php),它在當前路徑中創建一個新文件夾。

以下是不同文件的相關代碼。

的index.php

<body> 

<div class="filemanager"> 

    <div class="search"> 
     <input type="search" placeholder="Find a file.." /> 
    </div> 

    <button type="button" id="createFolder">create folder</button> 

    <div class="breadcrumbs"></div> 

    <ul class="data"></ul> 

    <div class="nothingfound"> 
     <div class="nofiles"></div> 
     <span>No files here.</span> 
    </div> 

</div> 

<!-- Include our script files --> 
<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script> 
<script src="assets/js/script.js"></script> 

的script.js

 $("button").click(function() { 

    //just a test to see if the listener and the variable works 
    alert('test1: currentPath= ' + currentPath); 


        function callCreateFolderPhp() { 

        //also just a test 
        alert('test2'); 

        $.ajax({ 

        url: "createfolder.php",       
        type: "GET", 
        data: "curPath=" + currentPath, 

        success: function(msg){ 
         //more testing 
         alert("test3"); 
        } 

        }); 

        //another one 
        alert('test4'); 

       } 

    callCreateFolderPhp(); 

    //final test 
    alert('test5'); 

    }); 

createfolder.php

<?php 
$currentPath = $_GET['curPath']; 
//different ways I have tested 
mkdir($currentPath+'/newfolder/', 0700); 
mkdir($currentPath+'/newfolder1', 0700); 
mkdir("newfolder2"); 
?> 

如果我點擊按鈕,我收到我的測試警報以及正確的路徑,但不會創建新的文件夾。

我在哪裏犯了一個錯誤?謝謝。

+1

將存放您正在創建的文件夾的文件夾是否具有適當的寫入權限? – Jesse

+0

如何檢查和更改權限? – Tzizel

回答

1

用744個寫入權限創建一個新目錄。

mkdir ($currentPath . '/newfolder/', 0744); 

我希望這有助於。

+0

我已經使用更新編輯了我的答案。 (。)句號運算符用於組合字符串,這正是您所需要的。對不起,php不是我的第一語言。 –

0

我添加了幾條語句。

<?php 
$currentPath = $_GET['curPath']; 
//different ways I have tested 
mkdir($currentPath+'/newfolder/', 0744); 
mkdir($currentPath+'/newfolder1', 0744); 
mkdir("newfolder3", 0744); 
mkdir($currentPath+'/newfolder/newfolder/', 0744); 
mkdir($currentPath+'/newfolder1/newfolder1', 0744); 
mkdir($currentPath+'newfolder/', 0744); 
mkdir($currentPath+'newfolder2', 0744); 
mkdir($currentPath+'/newfolder/newfolder4/', 0744); 
mkdir($currentPath+'/newfolder1/newfolder4', 0744); 
?> 

唯一可行的線路

mkdir("newfolder3", 0744); 

但我想與currentPath變量使用它。有沒有辦法來檢查什麼是$ currentPath分配?在script.js currentPath看起來像這樣的文件/地點

+0

您是否嘗試過echo $ currentPath?您的問題可能是您沒有在GET表單方法中傳遞$ _GET ['curPath']。 –

+0

它說http://domain/createfolder.php?curPath = files 路徑現在是文件。不應該是/文件嗎?這一個也不工作: mkdir('/'+ $ currentPath +'/ newfolder /',0744); – Tzizel

+0

你得到了什麼錯誤? –

相關問題