When I try to upload an image to my chat website it doesn't save the variable - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Friday, September 16, 2022

When I try to upload an image to my chat website it doesn't save the variable

I have a simple chat website, and I'm trying to add a profile-picture feature. This is my code:

<?php
function loginForm(){
echo
'<div id="loginform">
<p>Please enter your name to continue!</p>
<form action="index.php" method="post">
  <label for="name">Display Name: </label>
  <input style="font-family: Sans-serif;" type="text" name="name" id="name" />
  <label for="name">Profile picture: </label>
  <input style="font-family: Sans-serif;" type="text" name="pfp" id="name" />
  <input style="font-family: Sans-serif;" type="submit" name="enter" id="enter" value="Enter" />
</form>
</div>
<center><h2>Upload profile picture: </h2><br><?php error_reporting(0); ?> <?php $msg = "";    if (isset($_POST[\'uploadfile\'])) { $filename = $_FILES["choosefile"]["name"]; $tempname = $_FILES["choosefile"]["tmp_name"]; $folder = "image/".$filename; $_SESSION[\'pfp\'] = $folder; $db = mysqli_connect("localhost", "root", "", "Image_upload"); $sql = "INSERT INTO image (filename) VALUES (\'$filename\')"; ($db, $sql);  if (move_uploaded_file($tempname, $folder)) { $msg = "Image uploaded successfully"; }else{ $msg = "Failed to upload image"; } } $result = mysqli_query($db, "SELECT * FROM image"); ?> <!DOCTYPE html> <html> <!DOCTYPE html> <html> <head> <title>Image Upload in PHP</title> <! link the css file to style the form > <style type="text/css"> #wrapper{ width: 50%; margin: 20px auto; } form{ width: 50%; margin: 20px auto; } form div{ margin-top: 5px; } img{ float: left; margin: 5px; width: 280px; height: 120px; } #img_div{ width: 70%; padding: 5px; margin: 15px auto; border: 1px solid #dad7d7; } #img_div:after{ content: ""; display: block; clear: both; } button, input, span { background: #41d18e; border: none; color: white; padding: 4px 10px; font-weight: bold; border-radius: 20px; } </style> </head> <body> <div id="wrapper"> <! specify the encoding type of the form using the enctype attribute > <form method="POST" action="" enctype="multipart/form-data"> <input type="file" name="choosefile" value="" /> <div> <button type="submit" name="uploadfile">Use as new profile pic</button> </div> </form> </div></center>';
}
?>
<div id="menu">
    <?php echo "<img style=\"width: 20px; border-radius: 9999px;\" src=".$_SESSION['pfp'].">"; ?>
    <p class="welcome">&nbsp;Welcome, <b><?php echo $_SESSION['name']; ?></b></p>
    <p class="logout"><a id="exit" href="#">Rename</a></p>
</div>

For some reason <p class="welcome"></p> works perfectly fine, but the image just doesn't show. I looked in my image folder but could not find the image that I uploaded earlier.

Thanks for all the support.



source https://stackoverflow.com/questions/73735650/when-i-try-to-upload-an-image-to-my-chat-website-it-doesnt-save-the-variable

No comments:

Post a Comment