2013-09-27 53 views
0

我想從上傳文件夾中刪除照片,當我按下刪除所有記錄被刪除,除了上傳文件夾中的圖片這裏是我的刪除,如何編碼從上傳刪除的代碼段文件夾如何從上傳文件夾中刪除文件

//trigger 
<?php 
    echo '<td><a href="delete.php?staff_id=' . $row['staff_id'] . '"><input type="button" onclick="confirmDelete(event)" value="delete"> 

// check if the 'staff_id' variable is set in URL, and check that it is valid 
if (isset($_GET['staff_id']) && is_numeric($_GET['staff_id'])) 
{ 

// get staff_id value 

$staff_id = $_GET['staff_id']; 

// delete the entry 

$result = mysql_query("DELETE FROM development WHERE staff_id=$staff_id") 
or die(mysql_error()); 
+5

你可以使用[PHP取消鏈接功能](http://php.net/manual/en/function.unlink.php) – TheNytangel

+0

這是delete.php的鏈接,你需要顯示的處理delete.php當鏈接被點擊時執行。 (歡迎也) – Jonnny

+0

但是什麼是'(isset($ _ GET ['staff_id'])&& is_numeric($ _ GET ['staff_id']))' – Jonnny

回答

3

在你delete.php腳本,你需要這樣一行:

unlink("path_to_your_upload_directory/".$staff_id.".jpg"); 

如果你有不同的文件擴展名:實現它是先保存與擴展文件名以適當的方法之一當用戶/員工上傳數據庫表時ds文件。檢索相同和刪除文件時使用它:

$filename_with_extension = 'retrieve this from database table where it is stored'; 
unlink("path_to_your_upload_directory/".$filename_with_extension); 
+0

豎起大拇指Uours,它像魅力一樣工作 – user2821383

0
if(isset($_GET['staff_id'])&&is_numeric($_GET['staff_id'])) 
{ 
    $Staff_Id = mysql_real_escape_string($_GET['staff_id']); 
    if($Staff_Id != '.' || $Staff_Id != '..') 
    { 
     $extension = '.jpg'; 
     if(unlink('uploads/'.$Staff_Id.$extension)) 
     { 
      $result = mysql_query("DELETE FROM development WHERE staff_id=$staff_id")or die(mysql_error()); 
     } 
    } 
} 
+0

這可能會起作用,但是通過'$ staff_id'要求一個討厭的SQL注入攻擊。 – chrylis

+0

的確如此,但這只是他如何取消鏈接文件的一個例子,但我仍然會修改我的答案。 – user2801966

+0

實際上,即使那樣,您仍然需要保護'unlink'調用(我之前專注於查詢) ;這並不能消除路徑中的'..'等技巧。 – chrylis

0

使用unlink功能。這將刪除指定目錄中的文件

0

unlink()函數在PHP中。您必須在參數中提供該文件的完整路徑。

注意:不要給http://路徑。

相關問題