Hi all ,
The following code first checks if the user is authorised, then it checks for a button press and executes the code if the button if pressed. It then checks for a result and if there is a result (in the update query) it shows the confirmation page.
My problem is that the code within the button press seems to run whenever the page is called as PHP is report a lot of undefined variables. When i do press the button it all executes as it should, handling the images as expected however the final 'result' page shows as an entirely new page below the current one in the browser and not on its own. I have tried putting a couple of 'else' statments in with the 'if's and some else statments on their own but i must be doing it wrong because i constantly get an 'unexpected else' error.
Can anyone point me in the right direction with this please?
<?PHP
session_start();
include ($_SERVER['DOCUMENT_ROOT']. '/includes/functions.php');
$GLOBALS['pubid'] = $_POST['pubid'];
if ( !is_adminauthed() and !is_authed() )
{
require ($_SERVER['DOCUMENT_ROOT']. '/includes/header.php'); ?>
<tr>
<td width="680" colspan="4" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
require ($_SERVER['DOCUMENT_ROOT']. '/includes/notpermit.inc.php');
</td>
</tr> <?php
require ($_SERVER['DOCUMENT_ROOT']. '/includes/footer.php');
exit;
}
else //user is authed
{
if (!isset($_POST['editimagebtn']));
{
require ($_SERVER['DOCUMENT_ROOT']. '/includes/header.php'); ?>
<tr>
<td width="790" colspan="4" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<br>
<h2>Update Pub Image:</h2>
<br>
<form method="post" action="editpubimage.php" enctype="multipart/form-data">
<b>Select Photo:</b> <input type="file" name="file" /><br />
<input type="submit" name="editimagebtn" value="Change Image">
<input type="hidden" name="pubid" value="<?php print $_POST['pubid']; ?>">
</form>
<br><br>
</tr>
<?PHP require ($_SERVER['DOCUMENT_ROOT']. '/includes/footer.php');
}
if (isset($_POST['editimagebtn'])); //button pressed
{
// if ( $_FILES['file'] ['name'] != "")
// {
$_FILES['file'] ['name'] = str_replace(" ","_", $_FILES['file'] ['name']);
$uploadedFile = $_FILES['file']['tmp_name'];
$newFile = $GLOBALS['imagepath'].$_FILES['file']['name'];
echo '<br>this is the file without spaces filled ';
echo $newFile;
$newFile = str_replace(" ","_",$newFile);
move_uploaded_file($uploadedFile, $newFile); //moves full size image to images folder
echo '<br>this is the file with spaces filled';
echo $newFile;
$tempimg = imagecreatefromjpeg($newFile); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($tempimg); // Current Image Width
$currheight = imagesy($tempimg); // Current Image Height
if ($currheight > $currwidth)
{ // If Height Is Greater Than Width
$zoom = $GLOBALS['thumbw'] / $currheight; // Length Ratio For Width
$newheight = $GLOBALS['thumbh']; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $GLOBALS['thumbw'] / $currwidth; // Length Ratio For Height
$newwidth = $GLOBALS['thumbw']; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$Thumb = imagecreatetruecolor($newwidth, $newheight); // Make New Image For Thumbnail
imagecopyresampled($Thumb, $tempimg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($Thumb, $GLOBALS['thumbpath'] . $_FILES['file']['name']); // Saving The Image
$thumbpath = $GLOBALS['thumbpath'] . $_FILES['file']['name'];
imagedestroy($tempimg); // Destroying The Temporary Image
imagedestroy($Thumb); // Destroying The Other Temporary Image
echo '<br> this should be the path to the thumb image';
echo $thumbpath;
// the below is used to generate a larger thumb but not a full size
$tempimg = imagecreatefromjpeg($newFile); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($tempimg); // Current Image Width
$currheight = imagesy($tempimg); // Current Image Height
if ($currheight > $currwidth)
{ // If Height Is Greater Than Width
$zoom = '250' / $currheight; // Length Ratio For Width
$newheight = '250'; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
}
else
{ // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = '250' / $currwidth; // Length Ratio For Height
$newwidth = '250'; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$LThumb = imagecreatetruecolor($newwidth, $newheight); // Make New Image For Thumbnail
imagecopyresampled($LThumb, $tempimg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($LThumb, $GLOBALS['imagepath'] . $_FILES['file']['name']); // Saving The Image
$imagepath = $GLOBALS['imagepath'] . $_FILES['file']['name'];
imagedestroy($tempimg); // Destroying The Temporary Image
imagedestroy($LThumb); // Destroying The Other Temporary Image
echo '<br>this should be the path to the full image';
echo $imagepath;
$pubid = $GLOBALS['pubid'];
$query = "UPDATE pubs SET filepath = '$imagepath', thumbpath = '$thumbpath' WHERE pubid = '$pubid' ";
$result = mysql_query($query) or die ('update of pub image failed');
// } //ends if there is a picture loop
} // ends if button pressed
}
if ($result != "")
{
require ($_SERVER['DOCUMENT_ROOT']. '/includes/header.php');
?>
<tr>
<td width="780" colspan="4" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<br><br><br><br><br><br><br><br><br>
<?PHP
echo 'Your new image has been submitted ';
echo '<br><a href="index.php">click here</a> to go back.';
?>
<br><br><br><br><br><br><br><br><br>
</td>
</tr>
<?php require ($_SERVER['DOCUMENT_ROOT']. '/includes/footer.php');
} //ends run for if $result is set
?>