Jpeg upload

hey all,
I’m having some problems with a php upload system i’m trying to get to work. I get pics to upload and even create thumbnails with the following script:

 $target_path = "uploads/memberspics/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
$_FILES['uploadedfile']['tmp_name']; // This is how we will get the temporary file... 
$target_path = "uploads/memberspics/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
if($uploadedfile_type == "image/pjpeg"){
$im= ImageCreateFromJPEG($target_path); 
$width= imageSX($im);			  // Original picture width is stored
$height= imageSY($im);
$n_width=250;  
$n_height= 250; 
$newimage= ImageCreateTrueColor("$n_width","$n_height");				 
ImageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
ImageJpeg($newimage,$target_path);
$string = "$target_path";
$target_path = str_replace(" ","",$string);
chmod("$target_path",0777);
 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
	 echo "pic uploaded successfully!";
}
}
$tsrc="$target_path";   // Path where thumb nail image will be stored
if($uploadedfile_type =="image/pjpeg"){$tsrc="uploads/memberspics/thumbnails/default.jpg";}
echo $tsrc;
if (!($uploadedfile_name !="" )){echo "I'm sorry, your picture must be in jpeg form. Please select another picture.";
exit();
}
if($uploadedfile_type=="image/pjpeg"){
$im= ImageCreateFromJPEG($target_path); 
$width= imageSX($im);			  // Original picture width is stored
$height= imageSY($im);
$n_width=100;  
$n_height= 100;			// Original picture height is stored
$newimage= ImageCreateTrueColor("$n_width","$n_height");				 
ImageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
ImageJpeg($newimage,$tsrc);
$newstring = "$tsrc";
$tsrc = str_replace(" ","",$newstring);
chmod("$tsrc",0777);
} else{
	 $target_path = "uploads/memberspics/default.jpg";
	 
} 

However, not all .jpg files are recognized as jpgs. I’ve tried the same pic over and over again, while changing the code above to “image/pJPEG” or “image/pjpg” any number of combinations to try to get it to work and nothing. The php echos its not a jpeg image.
I’m stumped with this problem. If the pic’s extension is .jpg, shouldn’t the php recognize it as a jpeg file??
Any help would be great! Thanks all!