PHP为目录中的每个文件运行一个函数

时间:2012-05-02 21:00:03

标签: php file function loops directory

您好我依赖卡在这里,

好的,我已经有了一个工作函数来更新我的目录中的一个指定文件的数据库, 现在我需要一个PHP代码为目录上的每个文件做同样的事情然后删除它。

$fileName = "IEDCBR361502201214659.RET";

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

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

函数linhaProcessada是

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');        
                }
          }
    }
}

真的需要这方面的帮助

2 个答案:

答案 0 :(得分:1)

PHP opendir()应该做的伎俩。更多信息: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 );
}

答案 1 :(得分: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);
}
?>