Please help with image upload and exif_imagetype

Hi i am new at this game and i am stuck on a relatively easy image upload script.

I do not know why this is failing. It passes through the form and sends fine to the processing script.

Its not about the connection as it does not even get that far.

Basically it fails on the exif_imagetype. For some reason it does not recognise jpg, png or anyfile to be honest.

Can you please enlighten me to my noobish mistakes (:

Html is:


                  <form action="profileimageupload.php" method="post">
                  
                  <fieldset>
                  <label><span class="required">Files accepted are * JPG, GIF &amp; PNG only!</span></label>
                  <input name="file" type="file" class="text requiredField">
                  </fieldset>
                  <fieldset>
                  <br />
                  <input name="submit" type="submit" value="Upload Image" class="button big round deep-red">
                  </fieldset>
                  
                  </form>

the php is:


<?php
require_once("../applications/constants/connection.php");
require_once("../applications/controllers/basic.php");
require_once("../userlogin/user.cookies.php");

if(isset($_POST["file"]) && $_POST["file"] != "" && $_POST["file"] != " " && $_POST["file"] != NULL){

	$upname = trim($_SESSION["SESS_USERNAME"]);
	$username = mysql_real_escape_string($upname);
	$profile_pic = mysql_real_escape_string($_POST["file"]);
	$bimage = "";

	if (exif_imagetype($profile_pic) != IMAGETYPE_GIF || exif_imagetype($profile_pic) != IMAGETYPE_JPEG || exif_imagetype($profile_pic) != IMAGETYPE_PNG) {
		returnheader("../users/edit-profile-image.php?fail=incorrectfile");
	}else{
		
	$bimage=ereg_replace(" ","_",trim($_FILES["file"]["name"]));
	$bimage=rand(1,999).$bimage; 
	move_uploaded_file($_FILES["file"]["tmp_name"],"../resources/userimages/".$bimage);															
		
	$imageinsert = "UPDATE users SET ";
	if($bimage != ""){
	deleteimage($username,"file");
	$imageinsert = "usersimage='".$profile_pic."'";
	} else {
		returnheader("../users/edit-profile-image.php?result=nofile");
	}
	$imageinsert = " WHERE username='" . $username . "'";
	$imageinsert_result = mysql_query($imageinsert) OR die("Could not upload image to users - " . mysql_error());
	returnheader("../users/edit-profile-image.php?result=success");
	
	}
} else {
	returnheader("../users/edit-profile-image.php?result=nofile");
}
?>

Files are sent via the $_FILES array. Your trying to get $profile_pic from the $_POST array when it’ll be found in the $_FILES[‘name’] array.

Ok i have changed it from a posting to a file to posting to the same page.

The problem now i see is that the form is not sending file or at least that is what it says in the error array i setup. But it could be that i am doing the $_FILES array wrong.

Here is the form:


                  <div class="inner-content last">
                  <h1><?php echo ucwords($username) ; ?> Profile Pic Page</h1>
                  <form action="edit-profile-image.php" method="post">
                  
                  <fieldset>
                  <label><span class="required">Files accepted are * JPG, GIF &amp; PNG only!</span></label>
                  <input name="file" type="file" class="text requiredField">
                  </fieldset>
                  <fieldset>
                  <br />
                  <input name="submit" type="submit" value="Upload Image" class="button big round deep-red">
                  </fieldset>
                  </form>
				  <?php 
					  //this is your message for the login errors
					  if(count($errors) > 0){foreach($errors as $e){echo "<p class=\\"required\\">" . $e . "</p>";}}
                  ;?>
                  </div>

Here is the php i have used.


require_once("../applications/constants/connection.php");
require_once("../applications/controllers/basic.php");
require_once("../userlogin/user.cookies.php");
$upname = trim($_SESSION["SESS_USERNAME"]);
$username = mysql_real_escape_string($upname);
$userpanel_result = connect_users($username);
$result = mysql_fetch_array($userpanel_result);
$userimage = stripslashes($result["usersimage"]);
$errors = array();

//only exicute if the submit button was clicked
if(isset($_POST['submit'])){
	//check if file was uploaded
	if(isset($_FILES['file']['name']) && $_FILES['file']['name'] != "" && $_FILES['file']['name'] != " " && $_FILES['file']['name'] != NULL){ 
		$bimage = "";
		$profile_pic = $_FILES['file']['name']; 
		 //checking if the file that was uploaded was infact a gif, jpg or png
		 if (exif_imagetype($_FILES['file']['name']) == IMAGETYPE_GIF || exif_imagetype($_FILES['file']['name']) == IMAGETYPE_JPEG || exif_imagetype($_FILES['file']['name']) == IMAGETYPE_PNG) {
			 
			/*checking if there are spaces within the image
			 *if there is spaces the replace with an _
			 *then add a random number before the image name
			 *then move file into the image folder
			*/
			$bimage=ereg_replace(" ","_",trim($_FILES['file']['name'])); 
			$bimage=rand(1,999).$bimage;  
			move_uploaded_file($_FILES['file']['tmp_name'],"../resources/userimages/".$bimage); 
			
			/*update the database with the image name
			 *first check if image is still there
			 *then delete old image from folder and database
			*/
			$imageinsert = "UPDATE users SET "; 
			if($bimage != ""){ 
			deleteimage($username,'file'); 
			$imageinsert = "usersimage='".$profile_pic."'"; 
			} else { 
			$errors[] = "Sorry We Lost Your File, Please Upload Again";
			} 
			$imageinsert = " WHERE username='" . $username . "'"; 
			$imageinsert_result = mysql_query($imageinsert) OR die("Could not upload image to users - " . mysql_error()); 
			$successimageupload = "Congratulations Your Image Is Now Uploaded";
			
		 } else {
			 $errors[] = "The File You Uploaded Was Not In A GIF, JPG Or PNG Format";
		 }
	} else {
		$errors[] = "No File Was Uploaded";
	}
}

But i cant see why as the form looks ok and when i do a echo postfile it comes back with the file ok so i am not understanding why it is not working.

Is it my logic within the coding?

Thank you for trying to help

I do a little test and it didnt work.

for instance if i do a:


$profile_pic = $_FILES['name']['type'];
echo $profile_pic;

it comes back with an error “Undifined index”!

Any ideas?

I know its been sent correctly because when i do a simple


$profile_pic = $_POST["file"];
echo $profile_pic;

it comes back with the image.jpg etc.

Change for form action line of the form to:

<form action="profileimageupload.php" method="post" enctype="multipart/form-data">