2012-02-16 2 views
0

XML 캐시 파일에서 통화 코드를 확인하는 스크립트를 실행하고 있습니다. xml 캐시에 통화 코드가 포함되어 있지 않으면 PHP가 통화 코드를 추가합니다. 그래서 같이 : 캐시가 완전히 비어 있고 내가 처음으로 스크립트를 실행하면 위 그림과 같이, 그것은 잘 작동xml 파일에 새 노드를 추가하면 XML 구조가 손상됩니다.

<currencies> 
    <currency id="GBP"> 
    <title>Pound Sterling</title> 
    <loc>United Kingdom, Crown Dependencies (the Isle of Man and the Channel Islands), certain British Overseas Territories(South Georgia and the South Sandwich Islands, British Antarctic Territory and British Indian Ocean Territory)</loc> 
    <rate>0.6383</rate> 
    <timestamp>1329398333</timestamp> 
    </currency> 
</currency> 

. 두 번째로 다른 통화로 실행하면 캐시 파일을 업데이트하지만 데이터가 없거나 구조체가 손상됩니다. 그래서 같이 : 나는 세 번째 통화에 대한 스크립트를 다시 실행하면 마지막으로

<?xml version="1.0"?> 
<currencies> 
    <currency id="GBP"> 
    <title>Pound Sterling</title> 
    <loc>United Kingdom, Crown Dependencies (the Isle of Man and the Channel Islands), certain British Overseas Territories(South Georgia and the South Sandwich Islands, British Antarctic Territory and British Indian Ocean Territory)</loc> 
    <rate>0.6383</rate> 
    <timestamp>1329398333</timestamp> 
    </currency> 
    <currency id 

그리고, 그것은 완전히 구조를 해결하고 전 놓쳤다 된 모든 올바른 데이터를 추가!

가 여기에 업데이트를하고있는 코드입니다 :

if(empty($ratesTimeStamp)) { 

     $newXML = simplexml_load_file('cache/rates.xml'); 

     $child = $newXML->addChild('currency'); 
     $child->addAttribute('id', ''.$to.''); 
     $child->addChild('title', $toTitle); 
     $child->addChild('loc', $toLocation); 
     $child->addChild('rate', $finalRate); 
     $child->addChild('timestamp', time()); 

     $dom = new DOMDocument('1.0'); 
     $dom->preserveWhiteSpace = false; 
     $dom->formatOutput = true; 
     $dom->loadXML($newXML->asXML()); 

     file_put_contents('cache/rates.xml', $dom->saveXML()); 
    } 
+0

여러 사람이 동시에 페이지를 방문하거나 여러 스레드가 파일에 액세스하는 경우 파일이 여러 번 액세스되는 것으로 의심됩니다. PHP에서 "잠금"기능을 사용하여 파일 잠금을 수행 한 후에 사용하십시오. http://php.net/manual/en/function.flock.php –

+0

실제로 동일한 파일의 여러 simplexml_load_file 인스턴스가 있으므로 실제로 맞을 수도 있습니다. 나는 실제로 내 코드에 flock()을 통합하는 방법을 조금 혼란 스럽다. ..? – tctc91

+0

XML 예제에는 두 개의 닫는'currency' 태그가 있습니다. 나는 마지막 것이''이라고 생각한다. 이 문제가 해결됩니까? –

답변

0

좋아, 이것은 아무것도 문제입니다.

텍스트 편집기에서 파일을 열었 기 때문에 파일을 열었을 때 파일이 업데이트 될 때마다 텍스트 편집기에서 열린 파일을 업데이트할지 묻는 메시지가 나타났습니다. 몇몇 이유.

텍스트 편집기에서 해당 파일을 닫은 다음 스크립트를 실행하면 대개 정상적으로 작동합니다.

관련 문제