Sorry for the massive code in advance, I have been going at this for a day and a bit and can’t seem to wrap my head around why I keep getting “Parse error: parse error in C:wampwwwFamily Tree Albumadd_news.php on line 97”. It works fine once you take out all the image content. So everything below // start of image information except the HTML submit button. I’m using WampServer Version 2.0, if anyone could help throw me in any direction except for this one, I would be greatly appreciated.
Oh, I am using PHP Version 5.3.0.
Thank you
[code=php]
<?php
include(“config.php”);
if(isset($_POST[‘submit’]))
{//begin of if($submit).
$title = mysql_escape_string($_POST[‘title’]);
$age = mysql_escape_string($_POST[‘age’]);
$testimonial = mysql_escape_string($_POST[‘testimonial’]);
//check if (title) field is empty then print error message.
if(!$title){
echo “Error: Entry title is a required field. Please fill it.”;
exit(); //exit the script and don’t do anything else.
}// end of if
$result = mysql_query(“INSERT INTO album (title, dtime, age, testimonial)
VALUES (‘$title’,NOW(),’$age’,’$testimonial’)”,$connect);
//print success message.
echo “<b>Thank you! News added Successfully!<br>You’ll be redirected to Home Page after (4) Seconds”;
echo “<meta http-equiv=Refresh content=4;url=index.php>”;
}//end of if($submit).
else
{//begin of else
?>
<br>
<h3>::Add News</h3>
<form method=”post” action=”<?php $_SERVER[‘PHP_SELF’] ?>”>
<p>Title:
<input name=”title” size=”40″ maxlength=”255″>
<br>
Age:
<textarea name=”age” rows=”7″ cols=”30″></textarea>
<br>
Testimonial:
<textarea name=”testimonial” rows=”7″ cols=”30″></textarea>
</p>
<p>
<?php
// start of image information
set_time_limit(0);
$link = mysql_connect($dbhost, $dbusername, $dbpassword) or die(“Could not connect to host.”); //put in your db connection details here
mysql_select_db(album) or die(“Could not find <strong class=”highlight”>database</strong>.”);
//define <strong class=”highlight”>a</strong> maxim size for the uploaded images
define (“MAX_SIZE”,”500″);
// define the width <strong class=”highlight”>and</strong> height for the thumbnail
<strong class=”highlight”>and</strong> are not fixed,
define (“WIDTH”,”150″);
define (“HEIGHT”,”120″);
// this is the function that will create the thumbnail <strong class=”highlight”>image</strong> from the uploaded <strong class=”highlight”>image</strong>
// the <strong class=”highlight”>resize</strong> will be done considering the width <strong class=”highlight”>and</strong> height defined, but without deforming the <strong class=”highlight”>image</strong>
function make_thumb($img_name,$filename,$new_w,$new_h)
{
//get <strong class=”highlight”>image</strong> extension.
$ext=getExtension($img_name);
//creates the new <strong class=”highlight”>image</strong> using the appropriate function from gd library
if(!strcmp(“jpg”,$ext) || !strcmp(“jpeg”,$ext))
$src_img=imagecreatefromjpeg($img_name);
if(!strcmp(“png”,$ext))
$src_img=imagecreatefrompng($img_name);
if(!strcmp(“gif”,$ext))
$src_img=imagecreatefromgif($img_name);
//gets the dimmensions of the <strong class=”highlight”>image</strong>
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
// next we will calculate the new dimmensions for the thumbnail <strong class=”highlight”>image</strong>
$ratio1=$old_x/$new_w;
$ratio2=$old_y/$new_h;
if($ratio1>$ratio2) {
$thumb_w=$new_w;
$thumb_h=$old_y/$ratio1;
}
else {
$thumb_h=$new_h;
$thumb_w=$old_x/$ratio2;
}
// we create <strong class=”highlight”>a</strong> new <strong class=”highlight”>image</strong> with the new dimmensions
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
// <strong class=”highlight”>resize</strong> the big <strong class=”highlight”>image</strong> to the new created one
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
// output the created <strong class=”highlight”>image</strong> to the file. Now we will have the thumbnail <strong class=”highlight”>into</strong> the file named by $filename
if(!strcmp(“png”,$ext))
imagepng($dst_img,$filename);
else
imagejpeg($dst_img,$filename);
if (!strcmp(“gif”,$ext))
imagegif($dst_img,$filename);
//destroys source <strong class=”highlight”>and</strong> destination images.
imagedestroy($dst_img);
imagedestroy($src_img);
}
// This function reads the extension of the file.
// It is used to determine if the file is an <strong class=”highlight”>image</strong> by checking the extension.
function getExtension($str) {
$i = strrpos($str,”.”);
if (!$i) { return “”; }
$l = strlen($str) – $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
// This variable is used as <strong class=”highlight”>a</strong> flag. The value is initialized with 0 (meaning no error found)
//and it will be changed to 1 if an error occures. If the error occures the file will not be uploaded.
$errors=0;
// checks if the form has been submitted
if(isset($_POST[‘Submit’]))
{
//reads the name of the file the user submitted for uploading
$image=$_FILES[‘cons_image’][‘name’];
// if it is not empty
if ($image)
{
// get the original name of the file from the clients machine
$filename = stripslashes($_FILES[‘cons_image’][‘name’]);
// get the extension of the file in <strong class=”highlight”>a</strong> lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
// if it is not <strong class=”highlight”>a</strong> known extension, we will suppose it is an error, print an error message
//and will not <strong class=”highlight”>upload</strong> the file, otherwise we continue
if (($extension != “jpg”) && ($extension != “jpeg”) && ($extension != “png”) && ($extension != “gif”))
{
echo ‘<h1>Unknown extension! Please use .gif, .jpg or .png files only.</h1>’;
$errors=1;
}
else
{
// get the size of the <strong class=”highlight”>image</strong> in bytes
// $_FILES[‘image’][‘tmp_name’] is the temporary filename of the file in which
//the uploaded file was stored on the server
$size=getimagesize($_FILES[‘cons_image’][‘tmp_name’]);
$sizekb=filesize($_FILES[‘cons_image’][‘tmp_name’]);
//compare the size with the maxim size we defined <strong class=”highlight”>and</strong> print error if bigger
if ($sizekb > MAX_SIZE*1024)
{
echo ‘<h1>You have exceeded the 1MB size limit!</h1>’;
$errors=1;
}
$rand= rand(0, 1000);
//we will give an unique name, for example <strong class=”highlight”>a</strong> random number
$image_name=$rand.’.’.$extension;
//the new name will be containing the full path where will be stored (images folder)
$consname=”image/”.$image_name; //change the image/ section to where you would like the original <strong class=”highlight”>image</strong> to be stored
$consname2=”image/thumb”.$image_name; //change the image/thumb to where you would like to store the new created thumb nail of the <strong class=”highlight”>image</strong>
$copied = copy($_FILES[‘cons_image’][‘tmp_name’], $consname);
$copied = copy($_FILES[‘cons_image’][‘tmp_name’], $consname2);
$sql=”UPDATE your table name SET <strong class=”highlight”>image</strong>= ‘$consname2’ WHERE id= ‘$lastid'”or die(mysql_error());
$query = mysql_query($sql)or die(mysql_error());
//we verify if the <strong class=”highlight”>image</strong> has been uploaded, <strong class=”highlight”>and</strong> print error instead
if (!$copied) {
echo ‘<h1>Copy unsuccessfull!</h1>’;
$errors=1;
}
else
{
// the new thumbnail <strong class=”highlight”>image</strong> will be placed in images/thumbs/ folder
$thumb_name=$consname2 ;
// call the function that will create the thumbnail. The function will get as parameters
//the <strong class=”highlight”>image</strong> name, the thumbnail name <strong class=”highlight”>and</strong> the width <strong class=”highlight”>and</strong> height desired for the thumbnail
$thumb=make_thumb($consname,$thumb_name,WIDTH,HEIGHT);
}
}
}
}
//If no errors registred, print the success message <strong class=”highlight”>and</strong> how the thumbnail <strong class=”highlight”>image</strong> created
if(isset($_POST[‘Submit’]) && !$errors)
{
echo “<h5>Thumbnail created Successfully!</h5>”;
echo ‘<img src=”‘.$thumb_name.'”>’;
echo $lastid;
}
echo “<form name=”newad” method=”post” enctype=”multipart/form-data” action=””>”;
echo “<input type=”file” name=”cons_image” >”;
echo “<input name=”Submit” type=”submit” id=”image1″ value=”<strong class=”highlight”>Upload</strong> image” />”;
echo “</form>”;
// end of image information
?>
</p>
<p><br>
<input type=”submit” name=”submit” value=”Add News”>
</p>
</form>
<?php
}//end of else
?>