validating uploaded XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jbatty
Forum Commoner
Posts: 35
Joined: Tue Sep 23, 2003 2:42 pm

validating uploaded XML

Post by jbatty »

I am using the upload script to below to upload an XML file. Before processing, I would like to check that the XML document is well formed by validating it against a XML schema.

How can I acheive this please?

Code: Select all

<?php
	if ($HTTP_POST_FILES['userfile']['name']=="none") {
		echo "Problem: no file uploaded";
		exit;
	}
	if ($HTTP_POST_FILES['userfile']['size']==0){
		echo "Problem: uploaded file is zero length";
		exit;
	}
	if ($HTTP_POST_FILES['userfile']['type'] != "text/xml") {
		echo "Problem: file is not an xml file";
		exit;
	}
	if (!is_uploaded_file($_FILES['userfile']['tmp_name'])) {
		echo "Problem: possible file upload attack";
		exit;
	}

    
	$uploaddir =  "uploads/";
    $upfile = $uploaddir. $_FILES['userfile']['name'];

	if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile)) {
    	echo "Problem: Could not move file into directory";
		exit;
	}
?>
User avatar
EvilWalrus
Site Admin
Posts: 209
Joined: Thu Apr 18, 2002 3:21 pm
Location: Springmont, PA USA

Post by EvilWalrus »

I'd sure like to know why you're fidging two different types of globals together... use $_FILES consistently.
jbatty
Forum Commoner
Posts: 35
Joined: Tue Sep 23, 2003 2:42 pm

Post by jbatty »

thanks I will make the recommended changes. But how can i check that the XML document is well formed by validating it against a XML schema.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

I haven't done this myself, but from what I know, you do need SAX for that. There is tutorial here on SAX and PHP and a brief thread on validating XML with SAX here.
Post Reply