Ajax到php - 缺少目录

时间:2013-06-20 16:56:25

标签: php ajax

我可能只需要一双新鲜的眼睛,这是愚蠢的,但我遇到了这个问题:

我已经实现了内联文件夹重命名,如:

$(function () {
        $(".name").dblclick(function (e) {
            e.stopPropagation();
            var currentEle = $(this).children("a");
            var value = currentEle.html();
            updateVal(currentEle, value);
        });
    });

    function updateVal(currentEle, value) {
        var is_file = 0;
        if(currentEle.hasClass('file')){
            is_file = 1;
        }
        var rnm_dir = currentEle.attr('href');
            rnm_dir = rnm_dir.replace('?dir=','');

        $(currentEle).html('<input class="thVal" type="text" value="' + value + '" />');
        $(".thVal").focus();
        $(".thVal").keyup(function (event) {
            if (event.keyCode == 13) {
                var n_name = $(".thVal").val();
                $('.thVal').remove();
                $(currentEle).html(n_name);

                $.ajax({
                    cache: false,
                    type: 'GET',
                    url: 'addfunctions.php',
                    data: {'rnm_dir':rnm_dir, 'is_file':is_file, 'new_name':n_name, 'function':'rename'},
                    dataType: "json",
                    beforeSend: function(){
                        console.log('ok');
                    },
                    success: function(response){
                        alert(response.name);
                    },
                    error: function (request, status, error) {
                        alert(request.responseText);
                    }
                });
            }
        });

        $(document).click(function () {
                $(currentEle).html($(".thVal").val());
        });
    }
在控制台中我看到:

?rnm_dir=%3Fdir%3Dpress/Blackfield/Blackfield%25202/joijuoi&is_file=0&new_name=dcxfgdfxg&function=rename 

但是我收到原始目录丢失的错误rename(,/dcxfgdfxg)

但是,如果我直接将控制台输出运行到文件:

addfunctions.php?rnm_dir=%3Fdir%3Dpress/Blackfield/Blackfield%25202/New%2520Folder&is_file=0&new_name=joijuoi&function=rename

它实际上重命名了它

我的php是:

$is_file = $_GET['is_file'];
$rnm_dir = urldecode($_GET['rnm_dir']);
$new_name = $_GET['new_name'];

$rnm_dir = str_replace('?dir=','',$rnm_dir);

if($is_file){
    $inf = pathinfo($rnm_dir);
    $ext = $inf['extension'];
    $dir = $inf['dirname'];
    $n_file = $dir.'/'.$new_name.'.'.$ext;
    rename($rnm_dir, $n_file);
    $out['name'] = $n_file;
    echo json_encode($out);
} else {
    $dir = dirname($rnm_dir);
    $n_dir = $dir.'/'.$new_name;
    rename($rnm_dir, $n_dir);
    $out['name'] = $n_dir;
    echo json_encode($out);
}

修改为工作代码

1 个答案:

答案 0 :(得分:2)

我认为问题是由?中的dat引起的。这将使GET键?rnm_dir而不是rnm_dir。以这种方式传递数据时,不需要?

将来发现这一点的一种快速方法是使用PHP函数print_r($_GET);来查看服务器端的实际接收数据。