I'm using this script to allow image uploads, but even though there are error messages at the end of the script, they don't display. Any type of file can still be uploaded. Any help would be appreciated. Files do get uploaded, but I need only images to be uploaded. thanks.
Code: Select all
/* Function to change profile picture */
function changeProfilePic() {
$post = isset($_POST) ? $_POST: array();
$max_width = "500";
//$userId = isset($post['hdn-profile-id']) ? intval($post['hdn-profile-id']) : 0;
$userId = $_SESSION['user'];
$path = 'images/profile_pictures/' . $userId;
$valid_formats = array("jpg", "png", "gif", "jpeg, JPEG, PNG, GIF ");
$name = $_FILES['profile-pic']['name'];
$size = $_FILES['profile-pic']['size'];
if(strlen($name)) {
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats)) {
if($size<(1024*1024)) {
$actual_image_name = 'avatar' .'_'.$userId .'.'.$ext;
$filePath = $path .'/'.$actual_image_name;
$tmp = $_FILES['profile-pic']['tmp_name'];
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
if(move_uploaded_file($tmp, $filePath)) {
$width = getWidth($filePath);
$height = getHeight($filePath);
//Scale the image if it is greater than the width set above
if ($width > $max_width){
$scale = $max_width/$width;
$uploaded = resizeImage($filePath,$width,$height,$scale, $ext);
} else {
$scale = 1;
$uploaded = resizeImage($filePath,$width,$height,$scale, $ext);
}
echo "<img id='photo' file-name='".$actual_image_name."' class='' src='".$filePath.'?'.time()."' class='preview'/>";
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
else
echo "Please select image..!";
exit;
}