Click here to Skip to main content
13,666,184 members
Rate this:
 
Please Sign up or sign in to vote.
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 15-Feb-18 5:15am
Updated 15-Feb-18 5:45am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180810.1 | Last Updated 15 Feb 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100