Adobe Dreamweaver Forums



Last 10 THreads :         spry menubars not lining up in IE (Last Post : coftscootourb - Replies : 148 - Views : 536 )           »          display strips leading 0 (Last Post : Azadi - Replies : 1 - Views : 2 )           »          Image enlarge on mouse-over (Last Post : accicalacrism - Replies : 5 - Views : 190 )           »          net links (Last Post : almanson - Replies : 2 - Views : 3 )           »          mx 04 versus cs3 pro (Last Post : richard - Replies : 0 - Views : 1 )           »          Locking resize handles in browser. (Last Post : Jasper Thayer - Replies : 3 - Views : 4 )           »          Flash buttons don't work (Last Post : Murray *ACE* - Replies : 2 - Views : 3 )           »          Does DW CS3 write better php than DWMX2004? (Last Post : Murray *ACE* - Replies : 1 - Views : 2 )           »          PagesThemselves Look OK After Removing Link, But WhenSecond Nav Bar is Used Formatting is Lost (Last Post : Murray *ACE* - Replies : 5 - Views : 6 )           »          Preview with CS4 or CS3 on OS10.5.4 gets wrong URL path (Last Post : Murray *ACE* - Replies : 7 - Views : 8 )           »         


Home Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
User Info Statistics
Go Back   Adobe Dreamweaver Forums > Dreamweaver: Main > Dreamweaver Application Development
 
Tags:

Reply
  #1 (permalink)  
Old 07-18-2008, 12:34 PM
Brett
 
Posts: n/a
Diggs:
Default Help with arrays in PHP

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
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-18-2008, 03:28 PM
David Powers
 
Posts: n/a
Diggs:
Default Re: Help with arrays in PHP

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/
Reply With Quote
  #3 (permalink)  
Old 07-18-2008, 06:37 PM
Brett
 
Posts: n/a
Diggs:
Default Re: Help with arrays in PHP

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
Reply With Quote
  #4 (permalink)  
Old 07-18-2008, 07:53 PM
David Powers
 
Posts: n/a
Diggs:
Default Re: Help with arrays in PHP

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/
Reply With Quote
  #5 (permalink)  
Old 07-19-2008, 03:03 PM
Brett
 
Posts: n/a
Diggs:
Default Re: Help with arrays in PHP

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
Reply With Quote
  #6 (permalink)  
Old 07-23-2008, 02:42 PM
Brett
 
Posts: n/a
Diggs:
Default Re: Help with arrays in PHP

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


Reply With Quote
  #7 (permalink)  
Old 07-23-2008, 03:44 PM
David Powers
 
Posts: n/a
Diggs:
Default Re: Help with arrays in PHP

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/
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



© Camley Interactive (camley.info) 2008 - all logos and images are copywrite their respective owners.
Proud member of the Camley Interactive Network
All times are GMT. The time now is 12:34 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
Inactive Reminders By Mished.co.uk