1

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

2 Answers2

1

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 );
}
Patrick Moore
  • 13,251
  • 5
  • 38
  • 63
  • thanks but didnt work, blank page, i guess the function is not being called =/, any ideas? – Paulo José Oliveira Rosa May 02 '12 at 22:47
  • Are you certain the `$dir` variable is set with the correct path? Can you turn on verbose error reporting, or look at your access logs, to see where the error is? At the very top, add this line: `error_reporting(E_ALL); ini_set('error_reporting', E_ALL);` – Patrick Moore May 03 '12 at 00:24
0
<? //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);
}
?>
Cory Carson
  • 276
  • 1
  • 6