Menghapus File Dengan PHP

Pada kesempatan sebelumnya kita sudah belajar tentang bagaimana menyalin file dengan php. Selanjutnya kita akan belajar menghapus file dengan php.

Untuk mengahapus file dengan php, perintah yang digunakan adalah unlik() diikuti dengan parameter path/file didalamnya. Dalam contoh kali ini kita akan menghapus file dengan php melalui sebuah form html. Berikut ini adalah contoh source code untuk menghapus file dengan php :


<?php 



$pesan_error = array();
$berhasil="";
if(isset($_POST['tampilkan_file']))
{
	if(empty($_POST['path_tujuan']))
	{
		//jika direktori tujuan kosong, maka gunakan default direktori aktif
		$path = getcwd();
	}
	else
	{
		//tetapi jika ada path tujuan dimana direktori baru akan diletakan maka 
		//rubah path direktorinya

		$path = $_POST['path_tujuan'];
		chdir($path);
	}

}
	

if(isset($_POST['hapus_file']))
{
	
	if(empty($_POST['file']))
	{
		array_push($pesan_error, "Harap pilih file yang ingin dihapus");
	}
	else
	{
		$file = $_POST['file'];
	}
	if(count($pesan_error)==0)
	{
		if(chdir($_POST['path_asal']))
		{
			if(unlink($_POST['file']))
			{
				$berhasil = "File  ".$_POST['file']." berhasil berhasil dihapus <br/>";
			}
			else
			{
				$berhasil = "Gagal menghapus file <br/>";
			}
		}
		

	}
	
}



?>
<!DOCTYPE html>
<html>
<head>
	<title>Operasi Pengelolaan File Sistem</title>
	<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container" align="center">
	<?php 
	if(count($pesan_error)>0)
	{
		foreach ($pesan_error as $error) {
			echo $error;
		}
	}
	?>
	<?=$berhasil?>
	<div class="container" align="center">
		<table class="items" width="60%" cellpadding="8" border="1">
			<thead>
				<th><h3>Menghapus File Dengan PHP  - ROOT93</h3></th>
			</thead>
			<tr>
				<td align="center">
					<form action="<?=$_SERVER['PHP_SELF']?>" method="post">	
						<table class="items2"  cellpadding="5" border="1">

							<tr>
								<td>Path</td>
								<td>
									
									<input type="text" size="35" name="path_tujuan" placeholder="Masukan penempatan direktori yang dituju" value="<?=getcwd()?>"  /> 
								</td>
								<td>
									<input type="submit" name="tampilkan_file" value="Pilih Direktori" />
								</td>
							</tr>					
						</table>
					</form>
				</td>
			</tr>
			<tr>
				<td align="center">
					<form action="<?=$_SERVER['PHP_SELF']?>" method="post">	
							<table class="items2"  cellpadding="5" border="1">
								<td>Pilih file</td>
								<td>
									<select name="file">
										<?php 
											$i=1;
											$path=getcwd();
											if($buka_dir=opendir($path))
											{
												
												while (($file=readdir($buka_dir))!==false) {
													if($i>=3)
													{
														echo "<option value=\"$file\">$file</option>";
														
													}
													$i+=1;
												}
												closedir($buka_dir);
											}

										?>

									</select>
									<input type="hidden" name="path_asal" value="<?=getcwd()?>" />
								</td>
								
								
								<td><input type="submit" name="hapus_file" value="Hapus" /></td>
							</table>
					</form>
				</td>
			</tr>
			
		</table>
	</div>
	</table>
	
	
</div>
</body>
</html>



bb

Menghapus file dengan php
output menghapus file dengan php

Cara kerja form diatas, adalah pertama Anda perlu memilih direktori, kemudian file pada pilihan file akan muncul, Anda bisa klik hapus untuk menghapus file yang tersedia.

Baca juga : Menuliskan Data Pada File Dengan PHP


0 Response to "Menghapus File Dengan PHP"

Post a Comment

Komentar yang Anda kirim akan terlebih dahulu di moderasi oleh Admin