2013-12-20 71 views
-1

得到這個下面的代碼:jQuery和PHP刪除文件

HTML:

<a href="#" class="deleteFile" id="'.$entry.'">Delete</a> 

JS:

$('.deleteFile').click(function(){ 
    if (confirm("Delete file ?")){ 
     $imagefile = $(this).attr('id'); 
     $.ajax({ 
      type: 'POST', 
      data: { 
       action: 'deleteFile', 
       imagefile: $imagefile, 
      }, 
      url: 'assets/php/deleteFile.php', 
      success: function(msg) { 
       alert($imagefile); 
      } 
     }) 
    } 
}) 

PHP:

if($_POST["action"]=="deleteFile") { 
    $imagefile = $_POST['imagefile']; 
    unlink("files/1/".$imagefile); 
} 

我不知道爲什麼這是行不通的。我的文件仍然在這裏...

你能幫我嗎?

謝謝。

+0

您是否測試過確認'unlink()'正常工作?你有沒有測試過正確的值傳遞給'$ _POST ['imagefile']'? –

+0

仔細檢查您發送到服務器的值。如上所述,您的ID值看起來不正確。 – Robbert

+0

這只是一個權限問題。非常感謝您的答覆。 – user3123641

回答

0

我認爲你需要echo $entry可變這裏

<a href="#" class="deleteFile" id="<?php echo $entry; ?>">Delete</a> 
0

HTML:

<a href="#" class="deleteFile" id="<?=$entry?>">Delete</a> 

PHP

unlink($SERVER['DOCUMENT_ROOT']."/files/1/".$imagefile); 
0

你需要寫一個PHP變量之前打開PHP聲明:

<?php echo $entry ?> 

HTML代碼必須看起來像:

<a href="#" class="deleteFile" id="<?php echo $entry ?>">Delete</a> 

您需要將文件保存爲PHP。