2012-05-02 81 views
1
每個文件

你好,我是靠什麼堅持在這裏的功能,PHP運行在目錄

好吧,我已經有一個工作的功能來更新我的數據庫之一指定的文件中,在我的目錄, 現在我需要一個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');   
       } 
      } 
    } 
} 

真正需要幫助的就這一個

+0

這是否幫助:http://stackoverflow.com/questions/2014474/php-read-sub-directories-and-loop-through-files-how-to? –

+0

我需要做同樣的事情。我喜歡它,當我發現這樣的問題。 +1爲樂趣。 –

回答

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); 
} 
+0

謝謝,但沒有工作,空白頁面,我猜這個函數沒有被調用= /,任何想法? –

+0

你確定'$ dir'變量是否設置了正確的路徑?你能打開詳細的錯誤報告,或看看你的訪問日誌,看看錯誤在哪裏?在最上面,添加這一行:'error_reporting(E_ALL); ini_set('error_reporting',E_ALL);' –

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