hi, i have a form with a few fields including 3 image fields:
the problem that i am having is that on the 3rd images upload it just does nothing. i can upload using any 2 of the image inputs and it will work fine but as soon as i upload using all three image inputs it just directs me back to the form.
i new to php and this is my first project so any advise will be appreciated.
<?php
ob_start();
//start the session
include("connect.php");
session_start();
//check to make sure the session variable is registered
if(!isset($_SESSION['username']))
{
header("location:index.php");
}
else
{
echo ("Cars admin Panel <a href='logout.php'>logout</a><hr/>");
// function getExtension checks file extension so the it compared to our allowed file types for images
function getExtention($checkext)
{
$position = strpos($checkext,".");
if(!$position){ return "";}
$short = strlen($checkext) - $position;
$finalext = substr($checkext,$position+1,$short);
return $finalext;
}
$errors = 0;
if (empty($_POST['fmake'])) { header("location:loginsuccess.php?error=error"); die("");}
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$make = mysql_real_escape_string($_POST['fmake']);
$desc = mysql_real_escape_string($_POST['fdesc']);
$model = mysql_real_escape_string($_POST['fmodel']);
$yearmodel = mysql_real_escape_string($_POST['fyearmodel']);
$colour = mysql_real_escape_string($_POST['fcolour']);
$price = mysql_real_escape_string($_POST['fprice']);
$image1 = mysql_real_escape_string($_FILES['userfile']['name']['0']);
$image2 = mysql_real_escape_string($_FILES['userfile']['name']['1']);
$image3 = mysql_real_escape_string($_FILES['userfile']['name']['2']);
//$priceNimage = $image1.$image2.$image3.$price;
$allimages = $image1.$image2.$image3;
//echo $image1,'<br>'.$image2,'<br>'.$image3,'<br>'; die();
if (!empty($allimages))
{
$image1 = strtolower($image1);
$image2 = strtolower($image2);
$image3 = strtolower($image3);
$theextention1 = getExtention($image1);
$theextention2 = getExtention($image2);
$theextention3 = getExtention($image3);
if (($theextention1 != "jpg") && ($theextention1 != "jpeg") &&
($theextention2 != "jpg") && ($theextention2 != "jpeg") &&
($theextention3 != "jpg") && ($theextention3 != "jpeg"))
{
header("location: loginsuccess.php?badimage=badimage");
die();
}
else
{
if(!empty($image1))
{
$image_name1 = md5(rand() * time()).'.'.$theextention1;
}
else{$image_name1 = "";}
if(!empty($image2))
{
$image_name2 = md5(rand() * time()).'.'.$theextention2;
}
else{$image_name2 = "";}
if(!empty($image3))
{
$image_name3 = md5(rand() * time()).'.'.$theextention3;
}
else{$image_name3 = "";}
$locate1 = $_FILES['userfile']['tmp_name']['0'];
$locate2 = $_FILES['userfile']['tmp_name']['1'];
$locate3 = $_FILES['userfile']['tmp_name']['2'];
$directory = "../cars/";
$img1 = $directory.$image_name1;
$img2 = $directory.$image_name2;
$img3 = $directory.$image_name3;
// if file exist
if (!empty($allimages))
{
//image 1
if(!empty($locate1))
{
$filesize = filesize($locate1);
if ($filesize > 500000) {echo "image size is too big";}
if ($theextention1 = "jpg" || "jpeg")
{
$uploadedfile = $locate1;
$icreate = imagecreatefromjpeg($uploadedfile);
}
elseif ($theextention1 = "png")
{
$uploadedfile = $locate1;
$icreate = imagecreatefrompng($uploadedfile);
}
else//if (($theextention = "gif"))
{
$uploadedfile = $locate1;
$icreate = imagecreatefromgif($uploadedfile);
}
list($width,$height) = getimagesize($uploadedfile);
$newwidth = 400;
$newheight = ($height/$width) * $newwidth;
$tempimg = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tempimg,$icreate,0,0,0,0,$newwidth,$newheight,$width,$height);
//$finish = $tempimg.$image_name;
imagejpeg($tempimg,$img1,100);
//move_uploaded_file($tempimg,$img) or die("error");
}
//image 2
if(!empty($locate2))
{
$filesize = filesize($locate2);
if ($filesize > 500000) {echo "image size is too big";}
if ($theextention2 = "jpg" || "jpeg")
{
$uploadedfile = $locate2;
$icreate = imagecreatefromjpeg($uploadedfile);
}
elseif ($theextention2 = "png")
{
$uploadedfile = $locate2;
$icreate = imagecreatefrompng($uploadedfile);
}
else//if (($theextention = "gif"))
{
$uploadedfile = $locate2;
$icreate = imagecreatefromgif($uploadedfile);
}
list($width,$height) = getimagesize($uploadedfile);
$newwidth = 400;
$newheight = ($height/$width) * $newwidth;
$tempimg = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tempimg,$icreate,0,0,0,0,$newwidth,$newheight,$width,$height);
//$finish = $tempimg.$image_name;
imagejpeg($tempimg,$img2,100);
//move_uploaded_file($tempimg,$img) or die("error");
}
//image 3
if(!empty($locate3))
{
$filesize = filesize($locate3);
if ($filesize > 500000) {echo "image size is too big";}
if ($theextention3 = "jpg" || "jpeg")
{
$uploadedfile = $locate3;
$icreate = imagecreatefromjpeg($uploadedfile);
}
elseif ($theextention3 = "png")
{
$uploadedfile = $locate3;
$icreate = imagecreatefrompng($uploadedfile);
}
else//if (($theextention = "gif"))
{
$uploadedfile = $locate3;
$icreate = imagecreatefromgif($uploadedfile);
}
list($width,$height) = getimagesize($uploadedfile);
$newwidth = 400;
$newheight = ($height/$width) * $newwidth;
$tempimg = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tempimg,$icreate,0,0,0,0,$newwidth,$newheight,$width,$height);
//$finish = $tempimg.$image_name;
imagejpeg($tempimg,$img3,100);
//move_uploaded_file($tempimg,$img) or die("error");
}
header("location: loginsuccess.php?file=$image");
}
}
}
###### send to db ######
$query = "INSERT INTO cars (id, make, model, description, color, yearmodel, price, km, dealership, location, image1, image2, image3, date_added)
VALUES ('NULL', '".$make."', '".$model."', '".$desc."', '".$colour."', '".$yearmodel."', '".$price."', '', '', '', '".$image_name1."', '".
$image_name2."', '".$image_name3."', NOW())";
mysql_query($query) or die("error".mysql_error());
}
header("location: loginsuccess.php?caruploaded=$make");
}
ob_flush();
?>
i tried echoing all three image fields but the same result, if i post 3 then its fails. 1 or 2 iamges works fine