XML fail to load with error - php

I'd like to parse an xml file with PHP. I used this code:
$images = parsage("Rambod_catalog.xml", "Thumbnail");
$prices = parsage("Rambod_catalog.xml", "Retail_Price");
echo sizeof($images);
function getindex($element, $liste) {
for($i=0;$i<sizeof($liste);$i) {
if($liste[$i] == $element)return $i;
}
return 0;
}
function parsage($document, $noeud) {
$document_xml = new DomDocument;
$document_xml->load($document);
$elements = $document_xml->getElementsByTagName($noeud);
return $elements;
}
but i got this exception:
Warning: DOMDocument::load() [domdocument.load]: I/O warning :
failed to load external entity "/Rambod_catalog.xml"
So, what is the problem? How can i fix my code?

"/Rambod_catalog.xml"
The path you have given is invalid. What you have wrote there is, unfortunately, referring to filesystem root. either the file is stored in the current directory and you delete the leading slash or specify a full path. (include_path is at times unreliable).
you should refer to files like CONSTANT_TO_DOCUMENT_ROOT.PATH_SEPARATOR.$fileName in order to be flexible.
also see getcwd() and $_SERVER global variable and in need, print_r or var_dump the reserved globlas to find what you might use for the CONSTANT_TO_DOCUMENT_ROOT.

Related

ini_get_all('Zend OPcache') echos warning that the extension couldn't be found

I have an app where the user or rather the admin, can check the configuration of PHP. Cause it is an API the ini values are collected by calling ini_get_all with all the extension names.
This is the code I use
public function getLoadedExtensions(): array
{
$extensions = [];
$loadedExtensions = get_loaded_extensions();
foreach ($loadedExtensions as $extensionName) {
try {
$iniValues = #ini_get_all($extensionName);
} catch (Throwable) {
$iniValues = [];
}
$extension = new PhpExtension();
$extension->setExtensionName($extensionName);
$extension->setVersion(phpversion($extensionName));
$extensions[] = $extension;
}
return $extensions;
}
It works for nearly all extensions as expected, just for Zend OPcache and a few with no ini values I get the following warning:
<b>Warning</b>: ini_get_all(): Extension "Zend OPcache" cannot be found in <b>/var/www/html/src/Maintenance/PhpInfo/PhpInfoService.php</b> on line <b>39</b><br />
Is there a clean way to get the ini values used by OPcache? I currently run a ini_get_all() and filter if the key starts with opcache.
I found a way to get the correct values. This piece of code gets the correct infos:
$extension = new ReflectionExtension('Zend OPcache'); // or the name of any other extension
$iniValues = $extension->getINIEntries();
This way all extensions get the correct ini values.

Debugging PHP Error: Ajax Chat installation

Unable to run install script for Ajax chat. It appears that the function calls can find the appropriate $filename, so I am having trouble determining what the invalid argument is.
in file [ROOT]/phpbb/di/container_builder.php on line 291: file_put_contents(C:/inetpub/wwwroot/phpbb3/chat/../cache/container_C:/inetpub/wwwroot/phpbb3/chatslashdotdotslash.php): failed to open stream: Invalid argument
Here is line 284-291:
protected function dump_container($container_filename)
{
$dumper = new PhpDumper($this->container);
$cached_container_dump = $dumper->dump(array(
'class' => 'phpbb_cache_container',
'base_class' => 'Symfony\Component\DependencyInjection\ContainerBuilder',
));
file_put_contents($container_filename, $cached_container_dump);
}
The function call for $container_filename is
protected function get_container_filename()
{
$filename = str_replace(array('/', '.'), array('slash', 'dot'), $this->phpbb_root_path);
return $this->phpbb_root_path . 'cache/container_' . $filename . '.' . $this->php_ext;
}
The function variable for $cached_container_dump is what I think may be the issue, as shown above in line 2. I'm not sure base_class is being found because it is not in the namespace. Been trying to follow this for a few days... any help will be appreciated.
Results from var_dump($dumper) here: does this mean the array is returning an object?
object(Symfony\Component\DependencyInjection\Dumper\PhpDumper)#13 (7) { ["inlinedDefinitions":"Symfony\Component\DependencyInjection\Dumper\PhpDumper":private]=> object(SplObjectStorage)#3325 (1) { ["storage":"SplObjectStorage":private]=> array(198)
Replace your file_put_contents function with the one below:
file_put_contents($container_filename, $cached_container_dump, FILE_APPEND);
And make sure that $dumper->dump() function returns a string.
You have to replace the colon it the cache file name since it's not a valid file name character on windows.

Warning: require_once(includes/EliteScript.php) [function.require-once]: failed to open stream: No such file or directory in

This is a licence based script and it has a valid licence using SourceGuardian. The script was working fine on client's old server and he hired me to migrate it to the new server.
After migrating, everything was working fine but not the login area, when trying to login it gives the follow error
Warning: require_once(includes/EliteScript.php) [function.require-once]: failed to open stream: No such file or directory in /home/sitetalk/public_html/interface/index.php on line 3
Fatal error: require_once() [function.require]: Failed opening required 'includes/EliteScript.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/sitetalk/public_html/interface/index.php on line 3
Registration works fine and can see the filled data in database. Tried to take it to the script developer, but there's no response since 3months.
Not sure, what causing this error to happen. Pasting the part of code of the shown file names.
EliteScript.php
<?php
global $config;
global $country_array;
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
define("ES_VERSION", "7.0.4");
define("ES_SERVER", "http://primary.shadowscripts.com/master/server.php");
define("ES_FLASH", "/includes/graphs/src/lang/my.inc.php");
define("ES_VERIFYDEV", "eth0");
require_once("ShadowScripts.php");
require_once("interface/template.php");
spl_autoload_register(function($class)
{
if ($class == "EliteScriptBase")
{
return 0;
}
if ($class != "FusionCharts_Gen.php")
{
$class = preg_replace("/\\_/", "/", $class);
}
$class = preg_replace("/\\\\/", "/", $class);
?>
and
index.php
<?php
global $es;
global $ui;
global $member_id;
global $base_url;
global $member_url;
global $image_url;
global $admin_url;
require_once("includes/EliteScript.php");
$es = new EliteScript();
$ui = new UserInterface();
$es->RequireMember();
$member_id = $es->GetMemberId();
$base_url = $es->GetConfig("baseUrl");
$member_url = $es->GetConfig("memberUrl");
$image_url = $es->GetConfig("imageUrl");
$f_login_page = $es->GetLoginUIPage();
if( $f_login_page )
{
$ui->ChangePage($f_login_page);
}
$es->DisplayHeader($es->getText("home_window_title"), "member.php");
switch( $_REQUEST["do"] )
{
case "toggle_autorenew":
toggle_autorenew();
break;
default:
display_main();
}
$es->DisplayFooter();
echo "\r\n\r\n\r\n\r\n\r\n\r\n";
function toggle_autorenew()
{
global $es;
global $ui;
global $member_id;
global $base_url;
global $member_url;
global $image_url;
global $admin_url;
$way = $_REQUEST["way"];
$es->ToggleMembershipAutoRenew($member_id, $way);
$ui->SetMessage("msg", $es->getText("home_togglerenew_success"));
$ui->ChangePage($_SERVER["PHP_SELF"]);
}
function display_main()
?>
As it says on line 3, I find nothing to edit. Please help what I can do to fix this.
Thanks
Change this and check it is working or not
require_once("includes/EliteScript.php");
to
require_once("../includes/EliteScript.php");
Explanation
current code calling current directory/includes/EliteScript.php but your file in previous directory so you have to add ../ it will include file from previous directory.
The problem you encounter has to do with relative and absolute paths in the include and require functions of php. Your index file is requiring the EliteScript.php with a relative path.
Articles about the difference between relative and absolute:
php relative and absolute paths
PHP: Absolute vs. relative paths
And a comparison table: http://www.geeksengine.com/article/absolute-relative-path.html
In your comments you mention the files are encrypted, so the best way to handle this is to keep the directory structure in exact the same way as the files are given to you.
The specifically point out (as I've done in the comments) where the problem lies:
public_html/interface/index.php loads the relative path includes/EliteScript.php which therefor will require the path public_html/interface/includes/EliteScript.php however your EliteScript.php is located in public_html/includes/EliteScript.php.
The other answers maybe true, but they didn't fix my problem.
I did exactly as it was showing in this article
I edited the include_path in php.ini and it's working like charm.

code for "delete full directory" returns warning

I have folder, his name is "1", in this folder is 2 folders: "big_images" and "small_images"
I want delete folder "1" and his content, this is code:
function removeFullDir ($dir) {
$content = glob($dir."/*");
foreach ($content as $file) {
if (is_dir($file)) {
removeFullDir ($file);
}
else {
unlink($file);
}
}
rmdir($dir);
}
removeFullDir("1");
in localhost (XAMPP server, php version 5.4.4) this code works good. in remote server this code also works, but returns warning:
Warning: Invalid argument supplied for foreach()
in remote server, php version is 5.2.42
please tell me, why obtain I this warning?
This happens because glob does not return an array, which means that based on the documentation it must return false. This can happen when there is an error, but it can also happen when the directory happens to be empty:
Returns an array containing the matched files/directories, an empty
array if no file matched or FALSE on error.
Note: On some systems it is impossible to distinguish between empty match and an error.
To prevent the notice just guard against it:
function removeFullDir ($dir) {
$content = glob($dir."/*");
if(!$content) {
return;
}
// ...
}
One possible reason could be open_basedir restriction which means that glob() returns false and it will error at the foreach loop as $content is not an array, so you could check like:
//before foreach
if(is_array($content) && count($content) > 0) {

php simplexml object caching

Consider the following function prototype for caching object from cached RSS(XML) feed:
function cacheObject($xml,$name,$age = 3600)
{
// directory in which to store cached files
$cacheDir = "cache/";
// cache filename
$filename = $cacheDir.$name;
// default to fetch the file
$cache = true;
// but if the file exists, don't fetch if it is recent enough
if (file_exists($filename))
{
$cache = (filemtime($filename) < (time()-$age));
}
// fetch the file if required
if ($cache)
{
$item = $xml->channel->item;
file_put_contents($filename,serialize($item));
// update timestamp to now
touch($filename);
}
// return the cache filename
return unserialize(file_get_contents($filename));
}
The function calls are as follows:
$urlD = "http://somerss.php";
$xmlD = simplexml_load_file(cacheFetch($urlD,'cachedfeedD.xml',3600));
$itemD = '';
if($xmlD === FALSE)
{$itemD = '';}
else
{$itemD = cacheObject($xmlD,'cacheobjectD',3600);}
$urlM = "somerss2.php";
$xmlM = simplexml_load_file(cacheFetch($urlM,'cachedfeedM.xml',3600));
$itemM = '';
if($xmlM === FALSE)
{$itemM = '';}
else
{$itemM = cacheObject($xmlM,'cacheobjectM',3600);}
I get the following error:
Fatal error: Uncaught exception 'Exception'
with message 'Serialization of 'SimpleXMLElement' is not allowed' in C:\xampp\htdocs\sitefinal\cacheObject.php:20 Stack trace: #0 C:\xampp\htdocs\sitefinal\cacheObject.php(20): serialize(Object(SimpleXMLElement))
Any help making this program to work is greatly appreciated.
Probably, the SimpleXMLElement class, like many built-in PHP objects, cannot be serialized.
Instead, you could call the class method asXML (which returns a valid XML string if you pass no parameters) and serialize this. You can then recreate the SimpleXMLElement class by calling simplexml_load_string() on this string.
Magpierss (free open source) is supposed to cache external xml files. I used it some years ago. You set the software a time frame to pull the xml file again. It worked well. Only problem I saw was that it kept pulling the xml file whether there was a frontend request for it or not which was using up the server. I think there might be a fix for that however. Good luck.

Categories