XML and PHP help!

Right, I need some help with XML and PHP. I’m using a flash gallery that calls an XML file to import the images and description. I need to create a form that uploads an image and thumbnail and then sends the links for those images into the XML file.

I’ll be honest I’m no pro when it comes to PHP I did a lot of it last year, but I can hardly remember it. I seem to have an upload form that uploads an image, which I’ll post the code for below. I’m just not too sure about the process of XML I’ve read various tutorials on here and the web. But no luck. I think I have found various bits, but if some one could help me put them together that would be great. Any way here is the code:

Image and thumbnail upload for images PHP (with some MySQL) :



<?
session_start();
$name = $_SESSION['MM_Username'];

//BEGIN FORM SUBMISSION CODE
//Retrieve information about file
$imgfilename=$HTTP_POST_FILES['imgfile']['name'];
$imgfiletmp=$HTTP_POST_FILES['imgfile']['tmp_name'];
$imgfilesize=$HTTP_POST_FILES['imgfile']['size'];
$imgfiletype=$HTTP_POST_FILES['imgfile']['type'];
//Specify a filepath 
$filepath="uploads/".$imgfilename;
//As an alternative, you could name each file with a unique name.
//The time() function returns the number of seconds since 1970!
//Since this will always be a unique number, you can create 
//unique file names. 
//First you would need to get the extension from the file. explode()
//does this:

$fileparts=explode(".",$imgfilename);

//The explode function breaks up strings according to the separator 
//specified in the first argument. The results are put into an array,
//or list. 
//To put it all together, you can use:

$filepath="uploads/".time().".".$fileparts[1];
$filepath2=time().".".$fileparts[1];

//The [1] in $fileparts specifies the element of the array.

//The following code checks that the file is OK to upload. 
//$message is used both to contain error messages 
//and a final check that all requirements have been met.
$message="";
$maxfilesize=900000;
if($imgfilesize>$maxfilesize){
	$message="You have exceeded the limit of $maxfilesize bytes<br>";
}
if($imgfiletype == "image/jpeg" ||$imgfiletype == "image/gif"){
	//file ok
	}else{
	$message.="Your file is not a gif or jpeg<br>";
}
if(file_exists($filepath)){
	$message.="A file of this name already exists<br>";
}
if($message==""){
	if(move_uploaded_file($imgfiletmp,$filepath)){
		//file upload ok
	}else{
	$message="Sorry, there was an error";
	}
}
//END FORM SUMISSION CODE

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

?>

<?
$sourcePath = "uploads/"; // Path of original image
$sourceUrl = '//';
$sourceName = $filepath2; // Name of original image
$thumbPath = $sourcePath . "thumbs/"; // Writeable thumb path
$thumbUrl = $sourceUrl . "thumbs/";
$thumbName = "tn_".$sourceName; // Tip: Name dynamically

// Set a maximum height and width
$width = 300;
$height = 300;

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($sourcePath.$sourceName);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Beyond this point is simply code. 
$sourceImage = imagecreatefromjpeg("$sourcePath/$sourceName");
$sourceWidth = imagesx($sourceImage);
$sourceHeight = imagesy($sourceImage);

$targetImage = imagecreate($width,$height);
imagecopyresized($targetImage,$sourceImage,0,0,0,0,$width,
$height,imagesx($sourceImage),imagesy($sourceImage));
imagejpeg($targetImage, "$thumbPath/$thumbName");

// By now, the thumbnail is copied into the $thumbpath
// as the file name specified in $thumbName, so display


?>
<?php
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO img (imgfile, des, title, `create`, `date`, thumbs, username) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($filepath, "text"),
					   //GetSQLValueString($_POST['imgfile'], "text"),
                       GetSQLValueString($_POST['des'], "text"),
                       GetSQLValueString($_POST['title'], "text"),
                       GetSQLValueString($_POST['create'], "text"),
                       GetSQLValueString($_POST['date'], "text"),
					   GetSQLValueString($thumbPath.$thumbName, "text"),
                       GetSQLValueString($_POST['username'], "text"));

  mysql_select_db($database_myDB, $myDB);
  $Result1 = mysql_query($insertSQL, $myDB) or die(mysql_error());

  $insertGoTo = "gallery.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

$colname_Recordset1 = "-1";
if (isset($_GET['id'])) {
  $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_GET['id'] : addslashes($_GET['id']);
}
mysql_select_db($database_myDB, $myDB);
$query_Recordset1 = sprintf("SELECT * FROM images WHERE id = %s", $colname_Recordset1);
$Recordset1 = mysql_query($query_Recordset1, $myDB) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>



Some of the code in there was used from a standard PHP Gallery I created here:

http://impserver.bournemouth.ac.uk/~lchapman/index.php

So a lot of the code will be irrelevant.

The XML/PHP code that I found on here was this:



<?php 
$file = fopen("yourfile.xml", "w+"); //the w+ mode truncates the file to 0 length for us :) 
$xmlString = $_GLOBALS["HTTP_RAW_POST_DATA"]; //you need to use this with xml.send() 
print "XML:".$xmlString."
"; 
if(!fwrite($file, $xmlString)){ 
    print "Error :("; 
} 
fclose($file); 
?>


(from here: http://www.kirupa.com/forum/showthread.php?s=&threadid=49418&highlight=xml)

I could be completely wrong so any advice would be much appreciated.

Cheers!!

Patch^ (Liam).

EDIT: just noticed I also have to resize the original image as well any info on that would be good to :slight_smile: