Page 1 of 1

validating uploaded XML

Posted: Thu Feb 05, 2004 4:29 pm
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;
	}
?>

Posted: Thu Feb 05, 2004 9:55 pm
by EvilWalrus
I'd sure like to know why you're fidging two different types of globals together... use $_FILES consistently.

Posted: Sat Feb 07, 2004 7:21 am
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.

Posted: Sat Feb 07, 2004 10:39 am
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.