Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP Delete Images from a Directory

Featured Replies

I have a basic php delete from directory script but it will not let me delete images:

 

delete1.php - This allows the user to select the directory they want to delete from:

 

<form method="POST" action="delete2.php" enctype="multipart/form-data">
<label>Select a School that you would like to delete images from:
<select name="list" id="list">
					<option value="albert/thumbs">Albert Village</option>
					<option value="ashby_hilltop/thumbs">Ashby Hilltop</option>
					<option value="ashby_school/thumbs">Ashby School</option>
					<option value="ashby_willesley/thumbs">Ashby Willesley</option>
					<option value="ashbycofe/thumbs">Ashby C of E</option>
					<option value="blackfordby/thumbs">Blackfordby</option>
					<option value="donisthorpe/thumbs">Donisthorpe</option>
					<option value="griffydam/thumbs">Griffydam</option>
					<option value="ivanhoe/thumbs">Ivanhoe</option>
					<option value="moira/thumbs">Moira</option>
					<option value="newbold/thumbs">Newbold</option>
					<option value="oakthorpe/thumbs">Oakthorpe</option>
					<option value="packington/thumbs">Packington</option>
					<option value="viscount/thumbs">Viscount Beaumont</option>
					<option value="woodcote/thumbs">Woodcote</option>
					<option value="worthington/thumbs">Worthington</option>
				</select></label><br /><br />
				<input type="submit" value="Open Directory" />
	   </form>

 

delete2.php

<?php
$path = "$list";

if(isset($_POST['file']) && is_array($_POST['file']))
{
foreach($_POST['file'] as $file)
{	
	unlink($path . "/" . $file) or die("Failed to delete file");
}
}
?>
<form name="form1" method="post">
<?php
$path = "$list";
$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) {

if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;

echo "<input type='CHECKBOX' name='file[]' value='$file'>";
echo "<img src='$file' alt='$file'><br />";

}
closedir($dir_handle);
?>
<input type="submit" name="Delete" value="Delete">
</form>

 

Any help would be much appreciated! Cheers!

A bit more information would probably be helpful. Does it allow you to "open directory" and list the relevant files. How far does the script go before it stops working?

 

This will narrow down the search for errors in your script. I have already noticed a few things that may be causing you problems for example the initial declaration of $path in delete2.php. It should be $path = $_POST['list'] as you are accessing a POST variable from the previous form.

 

Dan

  • 4 months later...

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.