Button to Delete file (unlink)

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
Posts: 128
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

$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>
			<div class="caption">
			<p>Code for Notice<br><input type="text" class="form-control" value="<img src='. str_replace("..","","$num").' class=img-responsive></img> "></p>


How can i make an button to delete pictures i cant find out ?

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

$re ='No $_POST data';

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

  //if file from 'delf' exists, delete it; else output message
    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;

thanks for the support it is working

Similar Topics