2010-07-29 16 views

답변

10

DOM으로, 스스로를 확인하는 것이하지 마십시오 사용 :

를가 존재하지 않기 때문에

<?php 
    header("Location: index.php"); 

    $xmldoc = new DOMDocument(); 
    $xmldoc->load('sample.xml'); 
    $newAct = $_POST['activity']; 

    $root = $xmldoc->firstChild; 
    $newElement = $xmldoc->createElement('activity'); 
    $root->appendChild($newElement); 
    $newText = $xmldoc->createTextNode($newAct); 
    $newElement->appendChild($newText); 
    $xmldoc->save('sample.xml'); 

?> 

지금, 그것은 나에게 오류를 제공합니다

if(file_exists('sample.xml')){ 
    $xmldoc->load('sample.xml'); 
} else { 
    $xmldoc->loadXML('<root/>');//or any other rootnode name which strikes your fancy. 
} 

파일 저장은 $xmldoc->save();과 함께 자동으로 수행됩니다.

+2

경로가 파일 ('is_file()')을 가리키고, 파일이 읽기 쉽고 (존재한다면'is_readable()'), 쓰기 가능 ('is_writable()')을 가리키는 지 확인할 수도 있습니다. – salathe

+1

@salathe : 실제로 오류 조건을 인식하고보고 할 수있는 적절한 코드를 작성할 때 다음 장애물이됩니다. – Wrikken

+0

대단히 감사합니다. – input

2

file_exists('sample.xml')

+1

나를 그냥 file_exists가()'기능이 항상 작동하지 않는 '것을 주장하자. 이 경우에는 다음을 사용할 수 있습니다 : 'if (@fopen ('http://www.somedomain.com/sample.xml', 'r')) // 무언가를하십시오, ' 이 함수는' file_exists()'하지만 모든 시나리오에서 매력처럼 작동합니다. 나는 지금이 문제에 부딪쳤다. 그리고 나의 발견을 공유하기를 원했다. – Dejv

0

로드 함수는 true 및 false를 반환합니다. 이 코드를 사용해보십시오 : (당신이 ** 원격 서버 **에 파일을 터치해야하는 경우, 즉)

... 
$res = $xmldoc->load('sample.xml'); 
if ($res === FALSE) 
{ 
    /// not exists 
} 

http://de2.php.net/manual/en/domdocument.load.php

관련 문제