PHP Run a function for each file in directory

Hello i'm rely stuck here,

Ok i already have a working function to update my database for ONE specified file, in my directory, now i need a php code to do the same thing for each file on directory and then delete it.

$fileName = "IEDCBR361502201214659.RET";

$cnab240 = RetornoFactory::getRetorno($fileName, "linhaProcessada");

$retorno = new RetornoBanco($cnab240);
$retorno->processar();

the function linhaProcessada is

function linhaProcessada2($self, $numLn, $vlinha) {
if($vlinha["registro"] == $self::DETALHE ) 
    {
        if($vlinha["registro"] == $self::DETALHE && $vlinha["segmento"] == "T" ) {
            //define a variavel do nosso numero como outra usavel
            $query ="SELECT * FROM jos_cobra_boletos WHERE nosso_numero = ".$vlinha['nosso_numero']."";
            echo "Boleto de numero: ".$vlinha['nosso_numero']." Atualizado com sucesso!<hr>";
            $testResult = mysql_query($query) or die('Error, query failed');
                if(mysql_fetch_array($testResult) == NULL){
                }else{
                $query = "UPDATE jos_cobra_boletos
                  SET status_pagamento='Pago'
                  WHERE nosso_numero=".$vlinha['nosso_numero']."";
                  $result = mysql_query($query) or die('Erro T');        
                }
          }
    }
}

Really need help on this one

Upvotes: 1

Views: 544

Answers (2)

Removed
Removed

Reputation: 1

<? //PHP 5.4+
foreach(
    new \GlobIterator(
        __DIR__ . '/*.RET', //Or other directory where these files are
        \FilesystemIterator::SKIP_DOTS |
        \FilesystemIterator::CURRENT_AS_PATHNAME
    )

    as $pathname
){
    (new RetornoBanca(
        RetornoFactory::getRetorno($pathname, 'linhaProcessada')
    ))
    ->processar();

    \unlink($pathname);
}
?>

Upvotes: 0

Patrick Moore
Patrick Moore

Reputation: 13354

PHP's opendir() ought to do the trick. More info here: http://php.net/manual/en/function.opendir.php

<?php
// Set Directory
$dir = '/abs/path/with/trailing/slash/';
if ($handle = opendir( $dir )) { // Scan directory
    while (false !== ($file = readdir($handle))) { // Loop each file

        $fileName = $dir . $file;

        // Run code on file 
        $cnab240 = RetornoFactory::getRetorno($fileName, "linhaProcessada");

        $retorno = new RetornoBanco($cnab240);
        $retorno->processar();

        // Delete file
        unlink( $fileName );
    }
    closedir( $handle );
}

Upvotes: 1

Related Questions