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.

Deleting Images from a Directory Script

Featured Replies

Hey guys,

 

I am using the following script to delete files in different directories. The location of the directory is set by the posted value from the form before. However...the script isn't working. Can you please help me as I'm a beginner to PHP.

 

Cheers!

 

<?php

$path = $_POST['list'];

if(isset($_POST['file']) && is_array($_POST['file']))
{
foreach($_POST['file'] as $file)
{	
	unlink($_POST['list'] . $file) or die("Failed to delete file");
}
}
?>

<form name="form1" method="post">

<?php
$path = $_POST['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>

what is $_POST['list']? Whats the value? This seems a bit overly complicated for my liking Josh. :S. Do you have the right permissions on said directory?

  • 4 months later...

You should never give the user the ability to entirely wipe from the server, you should create a temp or "moved" files folder and place them there.

 

It's for your protection in the long run.

 

bool copy ( string $source , string $dest [, resource $context ] )

 

That is the short explanation of the copy function from PHP.net

 

You could integrate this to your script like this possibly.

 

$fileUserSelectedToDelete == $_POST['value'];
$moveToThisDirectory == $_SERVER['DOCUMENT_ROOT'].'path/to/movedFilesFolder';

// Run the function
copy($fileUserSelectedToDelete, $moveToThisDirectory, $output);

if($output)
return true;
else
return false;

 

If you then want to "delete" multiple files at once you probably want to loop whilst a passed value remains true a while loop should see you well.

 

Good luck :)

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.