$10 Paypal for some PHP code help...

Discussion in 'PHP' started by cyclotron, Jun 22, 2012.

  1. #1
    
    
    
    
    $array = Array( 
       0 => '-1.jpg',
       1 => '-2.jpg',
       2 => '-3.jpg',
       3 => '-4.jpg',
       4 => '-5.jpg',
       5 => '-6.jpg',
       6 => '-7.jpg',
       7 => '-8.jpg',
       8 => '-9.jpg',
       9 => '-10.jpg',
       10 => '-11.jpg',
       11 => '-12.jpg',
       12 => '-13.jpg',
       13 => '-14.jpg',
       14 => '-15.jpg',
       15 => '-16.jpg',
       16 => '-17.jpg',
       17 => '-18.jpg',
       18 => '-19.jpg',
       19 => '-20.jpg',
       20 => '-30.jpg'
    );
    
    if ($handle = opendir('/skin-batcher/omgz')) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
    $filename = '/skin-batcher/omgz/$file';
    $id = time( );    // filename
    
    
    
    
       foreach ( $array as $needle ) {
    if (strpos($file,$needle) !== false) {
    	echo $file;
    }
    
    }
     
            }
        }
        closedir($handle);
    }
    ?>
    PHP:

    I have over 5000 files in a directory. and if they contain "-XXX.jpg" I want to do something with them.
    The above code.. kind of works but not quite accurately.

    is there any easier way .. a way that will definitely work.
     
    cyclotron, Jun 22, 2012 IP
  2. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #2
    What do you want to do with them? Noone here owns a crystal ball.

    In what way does it "kind of" work?

    In what way does it not work "quite accurately"?

    An easier way to accomplish what?

    A way that would work doing what?

    Again...noone owns a crystal ball. Your post is so vague it's absolutely pointless to TRY and see what you are doing with your code.
     
    NetStar, Jun 22, 2012 IP
  3. wptheme

    wptheme Well-Known Member

    Messages:
    4,051
    Likes Received:
    346
    Best Answers:
    0
    Trophy Points:
    180
    #3
    @Netstar, I think what the OP meant is that he wants all of his files that contains "-XXX.jpg" to be referred to so that I can do something with it.
     
    wptheme, Jun 22, 2012 IP
  4. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #4
    Not up to anyone here to guess what he wants... or to assume. He needs to clarify what he's trying to do, why his code doesn't work for him, and what he would like to accomplish.
     
    NetStar, Jun 22, 2012 IP
  5. Praful.Kamble29

    Praful.Kamble29 Member

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    Be clear!!

    What you want to do? Purpose? What the code is for?
     
    Praful.Kamble29, Jun 23, 2012 IP
  6. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #6
    Hey, this could do it
    
    if ($handle = opendir('/skin-batcher/omgz')) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
    $filename = '/skin-batcher/omgz/$file';
    $id = time( );    // filename
    
    
    
    
     if (preg_match('/-([0-9]+)\.jpg$/', $file)) {
    echo $file;
    }
     
            }
        }
        closedir($handle);
    }
    ?>
    
    PHP:
     
    gapz101, Jun 23, 2012 IP
  7. lampChamp

    lampChamp Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    A simple regular expression can match the end of the filename, cheers!
    <?php
        $dir = 'dir/';
        if ($handle = opendir($dir)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    if (preg_match('#-[0-9]+\.jpg$#', $file)) {
                        // do something with $file
                    }
                }
            }
            closedir($handle);
        }
    ?>
    PHP:
     
    lampChamp, Jun 23, 2012 IP