Hi guys this is my first ever class , So i am looking for ways to improve it and possibly even more advice on people who have tried the script or dont understand some of it 🙂
What the class does ,
It uses the photoFlow component in flash to display images via a XML file.
THE CLASS :
<?php
/**
* This script works alongside with photoFlow component for Flash from flashloaded.com.
* It will upload and create the table to use with the component.
* REQUIRED FILES TO BE CREATED.
*
* imagehandler.xml ( A blank XML FILE created at the root of your site );
*
*/
class Upload{
//Class Properties
// Connection Properties.
// The server we wish to connect to.
private $server = "localhost";
// The MYSQL Username
private $username = "root";
// The MYSQL Password
private $password = "";
// The database to find.
private $db = "demowork";
//Uploading Properties
// The WebAddress
public $Site = "FlashGallery";
// The directory we are uploading to...
public $Directory = "images/";
// This is the real file selected by the user.;
public $RealFile;
// This is the temporary file we create to store the "copied" file.
public $TempFile;
//The image name to insert into database;
public $ImageName;
// The imagelocation ( Where the image can be found once uploaded.
public $ImageLocation;
//XML Creation Properties;
// This will hold the "base" string for the XML insert.
public $StartXML;
//The named blank XML file to fill.
public $XMLFile = "imagehandler.xml";
/* GLOBAL PROPERTIES */
// This will return any error messages.
public $error;
// <<END PROPERTIES>>
function SetProperties($_FILES){
//$_FILES superglobal has it own columns named ['yourfieldname']['tmp_name']['error']
$this->TempFile = $_FILES['uploadFile']['tmp_name'];
$this->RealFile = $_FILES['uploadFile']['name'];
$this->error = $_FILES['uploadFile']['error'];
// Attempt to put the file to the uploads.
$this->PutFile();
}
function PutFile(){
// Send the file to the directory as stated above.
$upload = move_uploaded_file($this->TempFile,$this->Directory.$this->RealFile);
// Make the imagelink the value of therealfile. EX> MyImage.jpg
$this->ImageLink = $this->RealFile;
if (!$upload){
$this->error .= "<li>Unable to upload ".$this->RealFile." to directory in ".$this->Directory."</li>";
$this->ShowStatus();
}else{
$this->error .= "<li>File has been moved to ".$this->Directory." and is named ".$this->RealFile."</li>";
$this->Connect();
}
}
function Connect(){
$connect = mysql_connect($this->server,$this->username,$this->password);
if(!$connect){
$this->error .= "<li>Unable to connect to server</li>";
}else{
$selectdb = mysql_select_db($this->db,$connect);
if(!$selectdb){
$this->ConnectionError .= "<li>Unable to find server and DB</li>";
}else{
$this->MakeTable();
}
}
}
function MakeTable(){
$CheckTable = "SELECT * FROM `flashgallery";
$CheckQuery = mysql_query($CheckTable);
// If unable to query table then make one.
if(!$CheckQuery){
$sql = "CREATE TABLE `".$this->db."`.`flashgallery` (
`ImageID` INT NOT NULL AUTO_INCREMENT ,
`ImageName` VARCHAR( 100 ) NOT NULL ,
`ImageLocation` VARCHAR( 100 ) NOT NULL ,
PRIMARY KEY ( `ImageID` )
) ENGINE = MYISAM COMMENT = 'Images to display in FlashGallery'";
$query = mysql_query($sql);
if(!$query){
$this->error .= "<li>Unable to create table</li>";
$this->ShowStatus();
}else{
$this->error .= "<li>Table Created</li>";
$this->ShowStatus();
}
}
}
function InsertToDB($_POST){
$ImageName = mysql_real_escape_string(ucfirst($_POST['ImageName']));
if ($ImageName == ""){
$this->error = "<li> No Image Name Entered</li>";
$this->ShowStatus();
}else{
// Send Query
$sql = "INSERT INTO `flashgallery`(`ImageName`,`ImageLocation`) VALUES ('".$ImageName."','".$this->ImageLink."')";
$query = mysql_query($sql);
if(!$query){
$this->error .= "<li>Unable to insert Image to DB</li>";
}else{
$this->error = "<li>Database Updated</li>";
$this->UpdateXML();
}
}
}
function UpdateXML(){
$sql = "SELECT * FROM `flashgallery`";
$query = mysql_query($sql);
$StartXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
$StartXML .= "<photos path=\"images\">";
while ($result = mysql_fetch_array($query)){
$StartXML .= "<photo name=\"".$result['ImageName']."\" url=\"/".$result['ImageLocation']."\">This is photo1</photo>";
}
$StartXML .= "</photos>";
// Open the file..
$OpenFile = fopen($this->XMLFile,"r+");
// Write the XML
$Write = fwrite($OpenFile,$StartXML);
if(!$Write){
$this->error .= "<li>Unable to write to file </li>";
}else{
$this->error .= "<li>XML GENERATED</li>";
}
//Close the file
fclose($OpenFile);
}
function ShowStatus(){
return $this->error;
}
}
// END CLASS
The Page for inserting images.
<?php
if(isset($_POST['Submit'])){
$NewUpload = new Upload;
$NewUpload->SetProperties($_FILES);
$NewUpload->InsertToDB($_POST);
echo $NewUpload->ShowStatus();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FlashGallery Test</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" name="form1">
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="16%" height="26"><label>Image Name </label></td>
<td width="38%"><input type="text" name="ImageName" id="textfield" /></td>
<td width="38%"><input name="uploadFile" type="file" id="uploadFile" /></td>
<td><input type="submit" name="Submit" value="Upload My File" /></td>
</tr>
</table>
</form>
</body>
</html>
For all the honed experts out there please remember that this is my first ever class , So theres bound to be some hiccups along the way whilst designing it 🙂
If people try it out and find that it doesnt work too well or something is not quite right please feel free to let me know of what kind of features or things that should be implemented. 🙂
Or alternatively if it works fine please let me know always good to get a public testing opinion.
Kind Regards Pinky.