iT邦幫忙

0

php檔案刪除問題

最近在學習PHP做了一個檔案上傳網站
檔案上傳到資料庫跟下載檔案和查看部分都正常
但刪除部分有問題,可以刪除資料庫資料但沒辦法刪除資料夾檔案
想請教一下刪除部分如何刪除

資料夾路徑是Filepaper/
第一個上傳檔案變數設$file0
第二個上傳檔案變數設$file1
第三個上傳檔案變數設$file2

##刪除的表單部分程式

<?php
	include("connMysqlupload.php");

	if(isset($_POST["action"])&&($_POST["action"]=="delete")){
		$sql_query = "DELETE FROM uploadpaper WHERE id=?";
		$stmt = $db_link -> prepare($sql_query);
		$stmt -> bind_param("i", $_POST["id"]);
		$stmt -> execute();
		$stmt -> close();
		//$db_link -> close();
		//重新導向回到主畫面
		//header("Location: http://localhost/test/indexok.php");
	}

$sql_select = "SELECT id, file0,file1,file2 FROM uploadpaper WHERE id = ?";
	$stmt = $db_link -> prepare($sql_select);
	$stmt -> bind_param("i", $_GET["id"]);
	$stmt -> execute();
    $stmt -> bind_result($id, $file0, $file1, $file2);
	$stmt -> fetch();
?>
  <tr>
  <th>欄位</th><th>資料</th>
   </tr>
  </tr>
   <tr>
   <td></td><td><?php echo $file0;?></td>
   </tr>
   <tr>
   <td></td><td><?php echo $file1;?></td>
   </tr>
   <tr>
   <td></td><td><?php echo $file2;?></td>
   </tr>
  <tr>
  <td colspan="2" align="center">
  <input name="id" type="hidden" value="<?php echo $id;?>">
  <input name="action" type="hidden" value="delete">
  <input type="submit" name="button" id="button" value="確定刪除這筆資料嗎?">
  </td>
  </tr>

接下來點選button按下後刪除檔案這邊就有錯

<?php
if (isset($_POST['button'])) {
echo $file0;
unlink($file0);
}
?>

結果跑出這兩段錯誤(路徑的部分以文字代替)

Notice: Undefined index: in (路徑) on line 113

Warning: unlink(): Invalid argument in (路徑) on line 113

想請問一下按下按鈕後 刪除表格上上傳的檔案這部分該怎麼做?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
海綿寶寶
iT邦大神 1 級 ‧ 2021-09-27 11:29:22
最佳解答

試試看
加這段(第5到13列)

		$sql_select = "SELECT id, file0,file1,file2 FROM uploadpaper WHERE id = ?";
		$stmt = $db_link -> prepare($sql_select);
		$stmt -> bind_param("i", $_GET["id"]);
		$stmt -> execute();
	    	$stmt -> bind_result($id, $file0, $file1, $file2);
		$stmt -> fetch();
		unlink($file0);
		unlink($file1);
		unlink($file2);

結果變成

<?php
	include("connMysqlupload.php");

	if(isset($_POST["action"])&&($_POST["action"]=="delete")){
		$sql_select = "SELECT id, file0,file1,file2 FROM uploadpaper WHERE id = ?";
		$stmt = $db_link -> prepare($sql_select);
		$stmt -> bind_param("i", $_GET["id"]);
		$stmt -> execute();
	    	$stmt -> bind_result($id, $file0, $file1, $file2);
		$stmt -> fetch();
		unlink($file0);
		unlink($file1);
		unlink($file2);
		
		$sql_query = "DELETE FROM uploadpaper WHERE id=?";
		$stmt = $db_link -> prepare($sql_query);
		$stmt -> bind_param("i", $_POST["id"]);
		$stmt -> execute();
		$stmt -> close();
		//$db_link -> close();
		//重新導向回到主畫面
		//header("Location: http://localhost/test/indexok.php");
	}

$sql_select = "SELECT id, file0,file1,file2 FROM uploadpaper WHERE id = ?";
	$stmt = $db_link -> prepare($sql_select);
	$stmt -> bind_param("i", $_GET["id"]);
	$stmt -> execute();
    $stmt -> bind_result($id, $file0, $file1, $file2);
	$stmt -> fetch();
?>
  <tr>
  <th>欄位</th><th>資料</th>
   </tr>
  </tr>
   <tr>
   <td></td><td><?php echo $file0;?></td>
   </tr>
   <tr>
   <td></td><td><?php echo $file1;?></td>
   </tr>
   <tr>
   <td></td><td><?php echo $file2;?></td>
   </tr>
  <tr>
  <td colspan="2" align="center">
  <input name="id" type="hidden" value="<?php echo $id;?>">
  <input name="action" type="hidden" value="delete">
  <input type="submit" name="button" id="button" value="確定刪除這筆資料嗎?">
  </td>
  </tr>

感謝您!我發現我的問題了 我發現是我input value那邊沒宣告檔案名稱 導致後端那邊檔案變數沒有用

2

說真的,看不懂你寫的東西是啥鬼。
只好先告訴你一些基本的

1.不要把前端語言當後端用。
2.表單的資料發送只能接收表單值。
3.unlink跑的路徑是主機實體路徑。要小心有些是需要完整路徑才能刪除。

不好意思~剛學php講的不是很清楚,我想要的結果是按下按鈕後檔案可以刪除,上面$file0 $file1 $file2是我上傳檔案設的變數不是檔名,路徑部分應該是沒問題的我表格上讀的到檔案
然後if (isset($_POST['button'])) { }按鈕的這段不知道要怎麼打才可以讀取到上傳至表格的檔案,讓它進行刪除的動作

先了解一下,你知道 $_POST的用意嘛??
另外,你有懂的我說的,不要將前端當後端使用的意義在哪嘛?

1
小魚
iT邦大師 1 級 ‧ 2021-09-25 02:16:44

如果是Linux系統,
要看有沒有權限,
不過如果是網頁自己建自己刪,
應該不會有權限問題,
那就檢查一下你寫的路徑吧,
如同星空大大說的要用絕對路徑,
不要用相對路徑.

0
咖咖拉
iT邦好手 1 級 ‧ 2021-09-25 09:52:46
file_exists( ):判斷檔案或目錄是否存在
is_file( ):判斷檔案是否存在
is_dir( ):判斷目錄是否存在
unlink( ):將檔案刪除

是否有檢查路徑?

看更多先前的回應...收起先前的回應...

路徑部分應該是沒問題的我表格上讀的到檔案
我想問說if (isset($POST['button'])) { }按鈕的這段不知道要怎麼打才可以讀取到上傳至表格的檔案,讓它進行刪除的動作

咖咖拉 iT邦好手 1 級 ‧ 2021-09-25 20:43:38 檢舉

$file0 $file1 $file2是你上傳檔案設的變數 不是檔名

unlink() 必需傳檔名

<?php
echo $file0;
$filepath ='Filepaper/$file0';
if (isset($_POST['button'])) {						

if(file_exists($filepath)){
if (unlink($filepath)) {
echo 'file is removed';
}else {
echo 'file is not available';
}
}else {
echo 'file is not available';
}
}
?>

那再請教您一個問題就是$filepath ='Filepaper/$file0'; file0變數這邊如果我改成只打檔案名稱的話是可以刪除的,但如果給他變數一樣有讀到檔名但無法刪除那請問這邊要如何更改

$filepath ='Filepaper/$file0';應該是問說$file0這個變數要怎麼讓它變成檔名

咖咖拉 iT邦好手 1 級 ‧ 2021-09-27 10:12:58 檢舉
$filepath ='Filepaper/'.$file0;
0
達達
iT邦新手 5 級 ‧ 2021-09-27 10:21:42

雞婆一下,先講你問題的做法,對新手來說,你先理解成前端只做畫面的動作,跟DB有關的動作都用後端做,然後前後端資料傳送是用ajax表單的方式溝通,所以你這個問題的作法,我會上傳檔案到DB的時候,在table裡用一個欄位塞絕對位置,塞完後再把p key用js塞回畫面裡,再來刪除檔案的時候,將p key拿來search table取得絕對位置,用絕對位置刪除檔案
但你的問題有好幾個,
1.你是用什麼方式溝通前後端
2.前端傳資料到後端是用表單,不可能像你的code用畫面的純文字進行傳遞

我要發表回答

立即登入回答