Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi See my code below - can someone help me with code to set a default image if the user hasnt upload an image yet?

Thanks in advance

What I have tried:

if(isset($_SESSION ['currentUserID'])){ 
echo "<h4> Upload an image of yourself here!</h4><br>
	 <form action='myProfile.php' method='POST' enctype='multipart/form-data'>
		<input type='file' name='file' >
		<br> <button type='submit' name='submit'  class='button-form'>Upload photo</button><br>
 </form>";
 echo "<form action='deleteProfile.php' method='POST'
		<button type='submit' name='submit' class='button-form'>Remove Profile Photo</button>
 </form>";
}

$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");	
	}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='{$fileNameNew}' WHERE UserID='{$_SESSION ['currentUserID']}'");
     $stmt->execute();
 }
//SuccessMessage
   if (isset($_GET["successCode"])) {
      if ($_GET["successCode"]==1)
         echo "<h3>Profile Picture uploaded!</h3> ";       
   }
   //Display Image for User
   $stmt = $conn->query('SELECT ProfilePicture FROM Profile WHERE UserID='.$_SESSION ['currentUserID']);
while($image =$stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<img src=uploads/".$image['ProfilePicture']."' width='150' height='150'/>";
$profileimage = $image['ProfilePicture'];
}
Posted
Updated 15-Feb-18 5:45am

1 solution

Regardless of the type of code, the issue is still the same.
If a default item has not been provided, what should the default item be?
In this case, you can pick an image of your choice as the default image, then, when the user chooses an image, replace the image with what the provide.

So, your query state has an update statement to set the filename.

On startup, select the filename from the database; if there is no record, update the database with your default filename, then direct them to either accept that or select one of their own.

Alternately, when the 'Profile' record is created, set the default image to your choice.
 
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