如果用户没有上传图像,如何设置defauly图像? [英] How can I set a defauly image if user hasnt uploaded an image?

查看:69
本文介绍了如果用户没有上传图像,如何设置defauly图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


请参阅下面的代码 - 如果用户尚未上传图片,有人可以帮我设置代码来设置默认图片吗?



提前致谢



我的尝试:



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'];
}

推荐答案

_SESSION ['currentUserID'])){
echo< h4> 在这里上传你自己的照片!< / 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'>上传照片< / button>< br>
< / form>;
echo< form action ='deleteProfile.php'method ='POST'
< button type ='submit'name ='submit'class ='button-form'>删除个人资料照片< ; / button>
< / form>;
}
_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(
_SESSION['currentUserID']; if (isset(


这篇关于如果用户没有上传图像,如何设置defauly图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆