Jump to content

simplexml_load_file not working with date as file name


Go to solution Solved by mac_gyver,

Recommended Posts

hey guys, having issues trying to get xml to load a file that has date as it's file name.  I have a script that creates an xml.

<?php

$companyName = ($_GET['VcomName']);
$visitDate        = ($_GET['Vdate']);
$visitors    = ($_GET['Vname']);

//$VcomName = 'test';
$vdoc = new DOMDocument('1.0');
$vdoc->formatOutPut = true;

$root = $vdoc->createElement('VisitorList');
$root = $vdoc->appendChild($root);

$company = $vdoc->createElement('CompanyName');
$company = $root->appendChild($company);

$ctext = $vdoc->createTextNode($companyName);
$ctext = $company->appendChild($ctext);

$visitor = $vdoc->createElement('Visitors');
$visitor = $root->appendChild($visitor);

$vtext = $vdoc->createTextNode($visitors);
$vtext = $visitor->appendChild($vtext);

$ddate = $vdoc->createElement('VisitDate');
$ddate = $root->appendChild($ddate);

$dtext = $vdoc->createTextNode($visitDate);
$dtext = $ddate->appendChild($dtext);

echo $vdoc->save($visitDate. ".xml") . "\n";

?>

It creates the file with the date submited in a form for a visitor board.

 

Im trying to open the file so that i can print out the visitor names and the company that is visiting.

<?php
libxml_use_internal_errors(true);

 $fileD=date('Y-m-d');
 
 echo "$fileD \n";
 
 $file = "/inc/$fileD.xml";

 include "/inc/2013-08-11.xml";
 
 
 $file = preg_replace('/[\s]+/',' ',$file);
 
    if(file_exists($file)) {
        $fxml = simplexml_load_file($file);
    
	        print_r($fxml);
    } else {
	    exit("The file $file Does not exist \n");
	}
?>

In this i have done some testing ... the include will work but the simplexml fails to find the file. All it gives me is the exit printout on the screen.

 

Im not sure why this is not working.  there are no errors besides unable to find the file.

It actually comes from an HTML5 from that i have.

<form action="inc/xml_create.php" autocomplete="off">
    <label for="date" tabindex="1">Date of Visit</label>
        <input type="date" name="Vdate" id="date" required>
    <label for="company" tabindex="2">Company Name</label>
	    <input type="text" name="VcomName" id="company">
	<label for="visitor" tabindex="3">Visitor name(s)</label>
	    <textarea name="Vname" id="visitor"></textarea>
	    <input type="submit" value="Submit Form">
</form>

Im using the HTML5 input date type.  So im not sure.. i tried to elemenate any spaces with the preg_replace in the code above.  Not sure about new line charecters.

  • Solution

the reason for this is the path you are specifying. the leading / is forming an absolute file system path, which is not where your file is actually at.

 

however, if the include() statement doesn't find the file where you specify, it will "finally check in the calling script's own directory and the current working directory before failing.", apparently even for absolute paths, which the documentation hints it won't do. so, the include() statement is finding the file, but your other statements won't.

 

since it appears that your form and your second piece of code are both in the parent folder that contains the inc/ folder, just remove the leading / from the file path, both in the $file variable and in the include statement, to form the correct path to where the file is at.

 

also, your preg_replace() is trying to convert multiple spaces to a single space, but for at least the posted code, there are no spaces at all since the date is being directly built. if the date was from external data and it could contain spaces..., you would want to remove all of them, not convert multiple ones to a single one.

the problem isn't that smiplexml needs a relative path. an absolute file system path would work, but the absolute path you supplied is not correct. the leading / refers to the root of the current disk. that's not where your path/file is at. it just turns out that include() went ahead and found the file relative to the calling script's own directory and the current working directory when it did not find it at that absolute path.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.