Button to Delete file (unlink)

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts: 282
Location: Holland Rotterdam

Button to Delete file (unlink)

I`m back again this time with an question for unlink
i lookt for 6 hours for an solution to delete pictures placed into this code

Code: Select all

<?php
$files = glob("../images/$users[email]/*.*");

for ($i=0; $i<count($files); $i++)
{
	$num = $files[$i];

	echo '<div class="col-xs-6 col-md-3">
	<div class="thumbnail">
			<a href="'.$num.'" rel="shadowbox"><img src="'.$num.'" class="img-responsive" style="width:200px;height:150px;"> </a>
			<br>
			<div class="caption">
			<p>Code for Notice<br><input type="text" class="form-control" value="<img src='. str_replace("..","http://145.53.93.209/Comments/","$num").' class=img-responsive></img> "></p>
			

  </div></div>

			</div>
			</div>';
}
?>
How can i make an button to delete pictures i cant find out ?

Admin Posts: 805
Hello,
Try adapt this code to your script:

Code: Select all

//get the files into an array
$files = glob('../images/'. $users['email'] .'/*.*');
$nrf = count($files);
$reout ='';  //store data to output

//traverse the array, add form with button to delete it
for($i=0; $i<$nrf; $i++){
  $reout .='<form action="delfile.php" method="post"><input type="hidden" name="delf" value="'. urlencode($files[$i]) .'" /><input type="submit" value="Delete" /> - <span>'. $files[$i] .'</span></form>';
}

echo $reout;
- In "delfile.php" file:

Code: Select all

<?php
$re ='No $_POST data';

//if form data with 'delf' name
if(isset($_POST['delf'])){
  $_POST['delf'] = urldecode($_POST['delf']);

  //if file from 'delf' exists, delete it; else output message
  if(file_exists($_POST['delf'])){
    if(unlink($_POST['delf'])) $re ='File deleted: '. $_POST['delf'];
    else $re ='Unable to delete: '. $_POST['delf'];
  }
  else $re ='The file not exists: '. $_POST['delf'];
}
echo $re;

JanMolendijk Posts: 282
thanks for the support it is working

Similar Topics