note 103183 rejected from function.unlink by danbrown
| From: | danbrown@php.net | Date: | Wed, 30 Mar 2011 18:07:51 +0000 |
| Subject: | note 103183 rejected from function.unlink by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178351@lists.php.net to get a copy of this message | ||
Note Submitter: qakbar at hotmail dot co dot uk
----
I'm new to pho and need some help.
I have a php script that deletes thumbnail images in a given folder using the unlink function.
But I wanted to know if their was a way to delete the large images in the main images folder and
also to put in messages to state which image has been unlinked or if no image has been slected to
delete.
my code is as below.
<?php
// directory separator
defined("DS")
|| define("DS", DIRECTORY_SEPARATOR);
// root path
defined("ROOT_PATH")
|| define("ROOT_PATH", realpath(dirname(__FILE__)));
// upload folder directory
defined("UPLOAD_DIR")
|| define("UPLOAD_DIR", "../imagefolder/thumbfolder");
// path to the upload folder
defined("UPLOAD_PATH")
|| define("UPLOAD_PATH", ROOT_PATH.DS.UPLOAD_DIR);
function getAllFiles($folder = null) {
if(!empty($folder) && is_dir($folder)) {
if($handle = opendir($folder)) {
$out = array();
while($file = readdir($handle)) {
if(is_file($folder.DS.$file)) {
$out[] = $file;
}
}
closedir($handle);
return $out;
}
return false;
}
return false;
}
$files = getAllFiles(UPLOAD_PATH);
if (!empty($_POST['file'])) {
foreach($_POST['file'] as $file) {
unlink(UPLOAD_PATH.DS.$file) or die("Failed to <strong
class='highlight'>delete</strong> file");
}
header("location: " . $_SERVER['REQUEST_URI']);
}
?>
<?php if (!empty($files)) { ?>
<form name="form1" method="post">
<?php foreach($files as $key => $file) { ?>
<label for="file_<?php echo $key; ?>">
<input type="checkbox" name="file[]" id="file_<?php echo $key;
?>" value="<?php echo $file; ?>" />
<img src=<?php echo UPLOAD_DIR.DS.$file; ?> />
</label>
<?php } ?>
<input type="submit" name="delete" value="Delete" />
</form>
<?php } ?>
Thank you