Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Feb 2007
    Posts
    217
    Thanks
    25
    Thanked 1 Time in 1 Post

    Question is there an easier way to do this?

    'I want to check if certain images are stored in three different directories, if they are I want to unlink them. And add some stuff to variable $commando.'

    Like this:

    PHP Code:
    if (isset($_POST['Pic1'])) {
     if (
    file_exists("../".$Countryrow["Imagelocation"]."/thumbs/".$Countryrow['Pic1']."t.jpg")) {unlink  ("../".$Countryrow["Imagelocation"]."/thumbs/".$Countryrow['Pic1']."t.jpg");}
      if (
    file_exists("../".$Countryrow["Imagelocation"]."/thumbseffects/".$Countryrow['Pic1']."t.jpg")) {unlink ("../".$Countryrow["Imagelocation"]."/thumbseffects/".$Countryrow['Pic1']."t.jpg");}
        if (
    file_exists("../".$Countryrow["Imagelocation"]."/thumbsoriginal/".$Countryrow['Pic1']."t.jpg")) {unlink ("../".$Countryrow["Imagelocation"]."/original/".$Countryrow['Pic1']."t.jpg");} 
     
    $commando=$commando"Pic1='',";
    }

    if (isset(
    $_POST['Pic2'])) {
    if (
    file_exists("../".$Countryrow["Imagelocation"]."/thumbs/".$Countryrow['Pic2']."t.jpg")) {unlink  ("../".$Countryrow["Imagelocation"]."/thumbs/".$Countryrow['Pic2']."t.jpg");}
      if (
    file_exists("../".$Countryrow["Imagelocation"]."/thumbseffects/".$Countryrow['Pic2']."t.jpg")) {unlink  ("../".$Countryrow["Imagelocation"]."/thumbseffects/".$Countryrow['Pic2']."t.jpg");}
        if (
    file_exists("../".$Countryrow["Imagelocation"]."/thumbsoriginal/".$Countryrow['Pic2']."t.jpg")) {unlink  ("../".$Countryrow["Imagelocation"]."/original/".$Countryrow['Pic2']."t.jpg");} 
    $commando=$commando"Pic2='',";
    }

    if (isset(
    $_POST['Pic3'])) {
    if (
    file_exists("../".$Countryrow["Imagelocation"]."/thumbs/".$Countryrow['Pic3']."t.jpg")) {unlink  ("../".$Countryrow["Imagelocation"]."/thumbs/".$Countryrow['Pic3']."t.jpg");}
      if (
    file_exists("../".$Countryrow["Imagelocation"]."/thumbseffects/".$Countryrow['Pic3']."t.jpg")) {unlink  ("../".$Countryrow["Imagelocation"]."/thumbseffects/".$Countryrow['Pic3']."t.jpg");}
        if (
    file_exists("../".$Countryrow["Imagelocation"]."/thumbsoriginal/".$Countryrow['Pic3']."t.jpg")) {unlink  ("../".$Countryrow["Imagelocation"]."/original/".$Countryrow['Pic3']."t.jpg");} 
    $commando=$commando"Pic3='',";

    // repeat this for all the other pictures 
    Now I need to do this for about 30 pictures. so that's a lot of coding. Is there an easier way to do this? for example with the foreach function?

  • #2
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts
    Yes, your on the right track just continue:

    PHP Code:
    $i=0;
    foreach (
    $_POST as $pic) {
    $i++;
      
    stuff

    And keep a numbering sequence to keep track with the i variable.

    PHP.net is your friend!
    http://gt.php.net/manual/en/control-...es.foreach.php
    "FORTRAN is not a language. It's a way of turning a multi-million dollar mainframe, into a $50 programmable scientific calculator."
    http://www.microfastcat.com -- FastCat Software, the fastest software on the NET!
    http://www.microthosting.com -- Free reseller web hosting, Hosting, VPS, FREE SMALL HOSTING!!!
    http://www.microtronix-tech.com -- Web design and programming


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •