質問

の問題


したい削除ファイル AJAX/PHP.

そのphpというファイルのファイル名は何を送りたいとAJAXではないファイルにしてしまっているんですけどへのリンクで削除します。チェックマーむけにPHP、思って、IF/ELSE文をチェックの場合は文字列は、ファイル: is_file, その結果 false.

なし is_file ということ:

Warning: unlink("image.jpg") [function.unlink]: Invalid argument in C:\wamp\www\images\users\delete.php on line 8

ファイルか電話、ajaxのフォルダのファイルもしたいと思います。

PHPの


<?php
    // I save the file sources from the URL what was sent by AJAX to these variables.
    $photo_id = $_GET['photo_id'];
    $thumbnail_id = $_GET['thumbnail_id'];

    function deletePhotos($id){
        // If is a file then delete the file.
        if(is_file($id)){
            return unlink($id);
        // Else show error.
        } else {
            echo $id . " is not a file, or there is a problem with it.<br />" ; 
        }
    }

    if(isset($photo_id)){
        deletePhotos($photo_id);
    }
    if(isset($thumbnail_id)){
        deletePhotos($thumbnail_id);
    }

 ?>

のAJAX


function deletePhoto(photo, thumbnail){

        var photos = encodeURIComponent(photo);
        var thumbnails = encodeURIComponent(thumbnail);

        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
        } else {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                document.getElementById("media").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "http://192.168.2.104/images/users/delete.php?photo_id=\""+photos+"\"&thumbnail_id=\""+thumbnails+"\"", true);
        xmlhttp.send();
    }
役に立ちましたか?

解決

あなたのAJAX要求が引用符で囲まれたデータを持っています。

//Bad
delete.php?photo_id="1234"

//Good
delete.php?photo_id=1234

//So use this:
xmlhttp.open("GET", "http://192.168.2.104/images/users/delete.php?photo_id="+photos+"&thumbnail_id="+thumbnails, true);

他のヒント

  • らかじめ与えておく必要がありまフルパスis_file.一部のようなパスimage.jpg ないがそのファイルがあります。いずのキュメントルートからの相対する必要がありま誤動作防止)。

  • これは最も危険なスクリプトをたのしくやっていました.また他のファイルをphoto_idるためのバッファーとして、webサーバの権限で削除します。少なくともしていることの確認を制限でのみ削除ファイル内にあるをクリックします。

あなたがたとえばパスを指定する必要があります。

file_exists( realpath('.') . '/' . $id );
代わりに他のセキュリティがない限り、

他の人が言ったこと同上(あなたのファイルはスクリプトと同じフォルダにあると仮定すると)、これは危険なスクリプトです!

あなたのポストにトリミング使用したり、変数を取得しようと、例:

$photo_id=trim($_GET['blah..blah']);
私は$photo_idを追加し、その今の私のために働いていたので、それはファイル名」である必要がある場合、そのリターンのこの「\ nfilename」のようなもの。

- 私の場合は

問題がtrimでは、ファイル名を返しません。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top