php - Invalid argument supplied for foreach() in -


i keep getting error code though works should when run in browser, when called include_once doesnt work due error

    foreach(($hostlist->uploaded) $uploaded) {     if (strcmp($uploaded->url,"http://someurl.com/")==0) {     $host = simplexml_load_file($config['hostlist']);     unset($host->uploaded->url);     unset($host->uploaded->pass);     $host->uploaded->addchild('url',"http://anotherurl.com/");     $host->uploaded->addchild('pass',"anotherpass");     $host->uploaded->asxml($config['hostlist']);     $host->asxml($config['hostlist']);`     echo "url changed  http://anotherurl.com/";     }   } 

knowing variables follows:

$config['hostlist'] = 'xml/host.xml';  $hostlist = simplexml_load_file($config['hostlist']); 

and sample of xml file:

<host>   <uploaded>   <work>yes</work>   <url>http://someurl.com/</url>   <pass>pass</pass>  </uploaded> </host> 

as had mentioned via comments, issue maybe file using relative path , being accessed via include_once script on different folder, causes file path of xml invalid.

here simple example of may happening you.

i have following folder structure:

root   - include_folder/   - include_folder/read_host.php   - include_folder/xml   - include_folder/xml/host.xml   - test_xml.php 

when accessing include_folder/read_host.php reads file fine.

this read_host.php:

<?php  $config['hostlist'] = 'xml/host.xml'; $hostlist = simplexml_load_file($config['hostlist']); foreach($hostlist->uploaded $uploaded) {     echo $uploaded->work, "\n";     echo $uploaded->url, "\n";     echo $uploaded->pass, "\n"; } 

the output:

yes http://someurl.com/ pass 

however if access test_xml.php have following content:

<?php include_once('include_folder/read_host.php'); echo "what happens"; 

it fails error:

php warning: simplexml_load_file(): i/o warning : failed load external entity "xml/host.xml" in /home/admin/include_folder/read_host.php on line 4 php notice: trying property of non-object in /home/admin/include_folder/read_host.php on line 5 php warning: invalid argument supplied foreach() in /home/admin/include_folder/read_host.php on line 5

what happens

however if change $config['hostlist'] = 'xml/host.xml'; absolute path xml file works fine , output:

yes http://someurl.com/ pass happens 

so in case absolute folder was:

$config['hostlist'] = '/home/admin/include_folder/xml/host.xml'; 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -