douke3007 2012-05-02 21:00
浏览 20
已采纳

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

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条回答 默认 最新

  • dongpu9481 2012-05-02 21:24
    关注

    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 );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥15 看一下OPENMV原理图有没有错误
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置
  • ¥15 Java计划序号重编制功能,此功能会对所有序号重新排序,排序后不改变前后置关系。
  • ¥15 关于哈夫曼树应用得到一些问题