Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a feature below in which allows user to upload a profile picture for themselves, Store in directory and in the database which links to their USERID. Once they upload, i want to display this image and also display if they go out of it and then back in?

Can someone help me out with code for this?

Thanks

What I have tried:

<?php
session_start();
include("dbConnect.php"); 

if(isset($_SESSION ['currentUserID'])){ 
echo "<h1> Upload an image of yourself here!</h1>
	 <form action='myProfile.php' method='POST' enctype='multipart/form-data'>
		<input type='file' name='file'>
		<br><br><button type='submit' name='submit'>Upload photo</button>
 </form>";
}
error_reporting(E_ALL);
ini_set('display_errors', 1);
$id =$_SESSION['currentUserID'];

if (isset($_POST['submit'])){
	$file = $_FILES['file'];

	$fileName = $_FILES['file']['name'];
	$fileTmpName = $_FILES['file']['tmp_name'];
	$fileSize = $_FILES['file']['size'];
	$fileError = $_FILES['file']['error'];
	$fileType = $_FILES['file']['type'];

	$fileExt = explode('.', $fileName);
	$fileActualExt = strtolower(end($fileExt));

	$allowed = array('jpg', 'jpeg', 'png', 'pdf');
	if(in_array($fileActualExt, $allowed)) {
	if($fileError ===0){
		if ($fileSize <1000000){
			$fileNameNew = "profile".$id.".".$fileActualExt;
	$fileDestination = 'uploads/'. $fileNameNew;	
	move_uploaded_file($fileTmpName, $fileDestination);
	header("Location: myProfile.php?successCode=1");	
	echo "<img src='uploads/profile".$id.".jpg'>";
	}else{
		echo "your file too big";
	}
	}else{
		echo "There was error uploading file";
	}
	}else {
		echo "you cant upload files of this type";
	}
  // Insert record
  	$stmt = $conn->prepare("UPDATE Profile SET ProfilePicture='{$fileName}' WHERE UserID='{$_SESSION ['currentUserID']}'");
     $stmt->execute();
 }
//SuccessMessage
   if (isset($_GET["successCode"])) {
      if ($_GET["successCode"]==1)
         echo "<h3>Profile Picture uploaded!</h3>"; 
   }
?>
Posted
Updated 23-Jan-18 2:34am
Comments
[no name] 23-Jan-18 6:20am    
You already have some code.Please let us know what is the issue.
Mohibur Rashid 23-Jan-18 6:52am    
What do you mean by display permanent?
[no name] 23-Jan-18 6:28am    
Sometimes your IE browser cache don't get clear.If you are using IE then try to check how can you programmatically clear browser cache in PHP.

1 solution

so you want the previous updated image on the page when turning back?
if(isset($_SESSION ['currentUserID'])){ 
echo "<h1> Upload an image of yourself here!</h1>
	 <form action='myProfile.php' method='POST' enctype='multipart/form-data'>
		<input type='file' name='file'>
		<br><br><button type='submit' name='submit'>Upload photo</button>
 </form>";
$id =$_SESSION['currentUserID'];
echo "<img src='uploads/profile".$id.".jpg'/>";
}
error_reporting(E_ALL);
ini_set('display_errors', 1);
...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900