2015-10-18 79 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

你得到了什么错误? –

相关问题