|
|||
|
Hi All,
I have created a page that shows thumbnail images of all images in a given directory. Here is the function that searches the directory and builds an array of qualified files: > function getImages($dir) { > $directory = new DirectoryIterator($dir); > $fileTypes = '#\.jpg$|\.jpeg$|\.gif$|\.png$|\.JPG$|\.GIF$|\.PNG $#i'; > $filtered = new RegexIterator($directory, $fileTypes); > $array = array(); > foreach ($filtered as $item) { > $array[] = $item->getFilename(); > } > return new ArrayIterator($array); > } > > // get an ArrayIterator of filenames and the total number: > $images = getImages('../images/images_news/'); > $totalImages = $images->count(); Here is the HTML and PHP used to display the images (I'm excluding code that is specific to the paging function): > // Implement offset and files per page > foreach (new LimitIterator($images, $offset, $filesperpage) as > $image) { ?> > <div class="imgcont"> > <div class="imgholder"> > <img src="../images/images_news/<?php echo $image; ?>" > alt="<?php echo $image; ?>" height="55" class="center" /> > </div> > <p class="smallname"><?php echo $image; ?></p><br /> > </div> > <?php } ?> > <div class="clear"><br /> My question is this. I need to provide a way to delete images that will no longer be needed. Actually, I need to be able to delete the main image and the thumbnail image, each of which is in a separate directory. What I don't know is how this type of thing is normally done (I don't want the FTP route because I don't want a client messing about in the directory structure). And what I don't understand is how to select one or more files to delete when they were produced by an array. For ease of use, it would be nice if I could add a checkbox that the user could check to indicate that the image(s) should be deleted. I just can't figure out how to select the file(s). I sure hope this is not one of those simple things that will make me hide my head in shame. Any help would be most appreciated. Brett |
| Sponsored Links |
|
|||
|
Brett wrote:
> My question is this. I need to provide a way to delete images that will > no longer be needed. Actually, I need to be able to delete the main > image and the thumbnail image, each of which is in a separate > directory. The principle is fairly straightforward. Add a checkbox to the loop that displays your images, and assign the filename to the value. <div class="imgcont"> <input type="checkbox" name="images[]" value="<?php echo $image; ?>" /> <div class="imgholder"> When the form is submitted, the $_POST array should contain a subarray called images. Loop through that to delete the images. $mainImgDir = '/path/to/main/image/folder/'; $thumbsDir = '/path/to/thumbs/folder/'; if (isset($_POST['images']) && is_array($_POST['images'])) { foreach (basename($_POST['images']) as $image) { unset($mainImgDir . $image); unset($thumbsDir . $image); } } -- David Powers, Adobe Community Expert Author, "The Essential Guide to Dreamweaver CS3" (friends of ED) Author, "PHP Solutions" (friends of ED) http://foundationphp.com/ |
|
|||
|
Hi David,
Thanks. I've got some of it working, however, after uploading with upload_both_new(), the thumbnail images now have _tmb inserted between the file name and the extension. So when deleting I keep getting warnings that the file mypic001.jpg cannot be found in the thumbs directory. How do I get the _tmb into the file name? By the way, you used unset() where I think you meant unlink(). I changed that and it does at least delete the main picture. Also, in the foreach you included basename, but that threw an error, so I took it out. I can't understand why it throws an error, but whenever I have tried using it in any situation, it objects. Finally, I have discovered that if I upload a file named Ti301.JPG, I get Ti301.JPG001_thb.jpg. It seems that the strtolower() function in getNextFilename5.php is not converting the uppercase extension, it is just adding the incremented number plus _tmb and the new extension. Any ideas about how to correct that? Many many thanks. Best, Brett |
|
|||
|
Brett wrote:
> the thumbnail images now have _tmb inserted between > the file name and the extension. So when deleting I keep getting > warnings that the file mypic001.jpg cannot be found in the thumbs > directory. How do I get the _tmb into the file name? foreach ($_POST['images'] as $image) { unlink($mainImgDir . $image); $pos = strrpos($image, '.'); $thumb = substr($image, 0, $pos) . '_tmb' . substr($image, $pos); unlink($thumbsDir . $thumb); } > By the way, you used unset() where I think you meant unlink(). Yes, I did. Sorry about that. > Finally, I have discovered that if I upload a file named Ti301.JPG, I > get Ti301.JPG001_thb.jpg. It seems that the strtolower() function in > getNextFilename5.php is not converting the uppercase extension, it is > just adding the incremented number plus _tmb and the new extension. Any > ideas about how to correct that? That shouldn't be happening, but I don't have time to look into that now. Sorry. -- David Powers, Adobe Community Expert Author, "The Essential Guide to Dreamweaver CS3" (friends of ED) Author, "PHP Solutions" (friends of ED) http://foundationphp.com/ |
|
|||
|
David,
Thanks very much for your help, it works nicely now. When I look at your code it all looks so obvious and easy, perhaps one day it will be like that for me too. Certainly having your examples helps my understanding enormously; I really appreciate it. I will continue to look at the upload_both, create_both, getNextFilename, to see if I can come up with a fix. I'm afraid it's a bit over my head, but you never know. As my grandfather used to say, even a blind squirrel stumbles upon an acorn now and then. Wish me luck. Best, Brett |
|
|||
|
David,
What and easy fix this turned out to be (smiling with acorn bits at corner of mouth). I added one line to upload_both.inc.php: > $file = strtolower($file); I added this right after line 15 which is: > $file = str_replace(' ', '_', $_FILES['image']['name']); Now it changes all file names and extensions to lowercase. Sometimes it's just too easy. Brett |
|
|||
|
Brett wrote:
> Now it changes all file names and extensions to lowercase. Sometimes > it's just too easy. :-) -- David Powers, Adobe Community Expert Author, "The Essential Guide to Dreamweaver CS3" (friends of ED) Author, "PHP Solutions" (friends of ED) http://foundationphp.com/ |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
- Contact Us
-|-
Adobe Dreamweaver Forums -|-
Archive -|-
Top -|-Rules/Disclaimer-|-Help/Support-|-Advertise