PHP output to a .XML (Works fine, but the filename is odd)

Soldato
Joined
12 Sep 2003
Posts
11,233
Location
Newcastle, UK
Hi all,

I'm outputing some data from a Web Form to a .XML file. This works fine, I used some code from the net :p , however, it won't append the variable filename which a user enters with <filename>".xml". It just calls the file ".xml" and missus the name bit out. :confused:

Code:
if (isset($_POST['create_itinerary']))
	{	
		$xmlfileName = $_POST['xmlFileName'];
        }

...

		$default_dir = "C:/<folder1>/<folder2>/";
		$default_dir .= $xmlFileName .".xml";
		
		// Store the File
		$fp = fopen($default_dir,'w');
		fwrite($fp,$xml_doc);
		fclose($fp);

...

It gets the data via the POST method from another .php file, which has the Form on it. I don't undersand why it's not showing the $xmlFileName part before the .xml is added. The form works as it's getting other variables which a user enters and adding them into the .xml file, so why not the file name?

Thanks for any help. It's driving me crazy!
 
Scoping. Your variable is only accessible from inside the `if` block because that's where it was created.

Code:
$xmlFileName = '';

if ( !empty($_POST['whatever']) ) {
    $xmlFileName = 'foo';
}

// $xmlFileName is now accessible here.

Incidentally, that indentation style is the most horrible I've seen for a while—is it not reeeeally annoying to have *two* tabs for every single block? Isn't all your code off to the right of the screen somewhere? Ugh.
 
He has snipped out extra code, hence the '...' above and below the block.

btw - scoping isn't the issue, unless the filename stuff is within a different scope of course, and judging by the indentation.. that is likely.

Code:
<?php

if (true)
{
    $a = 'b';
}

var_dump($a);

?>
 
Thanks for the replies. :)

I will try changing the scope and post back, I never thought of that.

Oh, with regards to the indentation, no all of my code is to the left hand side but I just cut and paste that into here and tried to make it as read-able as possible by placing it in the centre. So don't worry, my code isn't all down to the right hand side. ;)

I wasn't really going for neatness, I just want to get the darn thing working first. lol.
 
Hmm, I always thought that PHP was sensible about scoping like that. Weird.

Another addition to "Things Wrong With PHP Vol. 482" then!
 
The variable name "$xmlfileName" is inconsistent - PHP is case-sensitive.

ie: You've initialised the variable $xmlfileName but referred to it as $xmlFileName everywhere else.
 
robmiller said:
Hmm, I always thought that PHP was sensible about scoping like that. Weird.

Another addition to "Things Wrong With PHP Vol. 482" then!
Java is the same, as is C if memory serves.
 
I just wanted to say thanks for the help.

I tried the suggestions and it works. :D Yey!

One other question, which I hope people won't mind answering, I'm trying to now load the created .xml file and use a .xsl style sheet to output the contents onto the screen.

I have coded the .xsl fine, however, I'm stuck trying to load the .xml file because with the name being random (a variable depending on what the user enters) the name can't be set. :confused:

So it's not as if I can go,

Code:
$xml = new DOMDocument;
$xml->load('/<folder1>/<folder2>/bob.xml');

...

because that may work once, but then it may be called 'bob2' by someone else.

Can I do it so that it just looks in the folder and loads any .xml files which are in there, regardless of a name? i.e. perhaps something like,

Code:
$xml = new DOMDocument;
$xml->load('/<folder1>/<folder2>/"".xml');

...

Thanks for any help. :)
 
Back
Top Bottom