PHP image Doesn't show, - php

The image is stored on the database and everytime when I upload an image on the web page, the database does work and update the database profile section but it is not showing on the web browser. :/ i tried different browsers but every browser failed to display the image, the Alt parameter of image does show but no image, i have inspected the page element the image path is there but i can't see the image there?
the code in the profile page is:
<?php
function change_profile_image($userID, $file_temp, $file_extn) {
$file_path ='C:/xampp/htdocs/cricket/user/images/profile/'. substr(md5(time()), 0, 10) . '.' . $file_extn;
move_uploaded_file($file_temp, $file_path);
mysql_query("UPDATE `user` SET `profile` = '" . mysql_real_escape_string($file_path) . "' WHERE `userID` = " . (int)$userID);
}
?>
<?php
$query = "select * from user where userID='".$_SESSION['id']."'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result)
?>
<link rel="stylesheet" type="text/css" href="../css/style.css">
<div id="content">
<h1>Edit Your Information</h1>
<div class="profile">
<?php
if (isset($_FILES['profile'])=== true) {
if (empty($_FILES['profile']['name']) === true) {
echo "Please choose a file!";
} else {
$allowed = array('jpg', 'jpeg', 'gif', 'png');
$file_name = $_FILES['profile']['name'];
$file_extn = strtolower(end(explode('.', $file_name)));
$file_temp = $_FILES['profile']['tmp_name'];
if (in_array($file_extn, $allowed)=== true) {
change_profile_image($_SESSION['id'], $file_temp, $file_extn);
} else {
echo "incorrect file type, Allowed: ";
echo implode(', ', $allowed);
}
}
}
if (empty($row['profile']) === false) {
echo '<img src="', $row['profile'], '" alt="', $row['user_firstname'], '\'s Profile Image">';
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="profile"> <br> <input type="submit">
</form>
</div>
<form method="post" action="">
<div class="txtbox1">Email</div>
<div class="txtbox">
<input type="text" name="email" value="<?php echo $row['user_email']?>"> </div><br><br><br>
<div class="txtbox1">First Name</div>
<div class="txtbox">
<input type="text" name="firstname" value="<?php echo $row['user_firstname']?>"></div><br><br><br>
<div class="txtbox1">Surname</div>
<div class="txtbox">
<input type="text" name="lastname" value="<?php echo $row['user_surname']?>"></div><br><br><br>
<div class="txtbox1">DOB</div>
<div class="txtbox">
<input type="text" name="dob" value="<?php echo $row['user_dob']?>"></div><br>
<div class="txtbox"><input type="submit" value="update" name="update"> </div>
</form>
<?php
if(isset($_POST['update'])){
$query1 = "update user set user_email='".$_POST['email']."', user_firstname='".$_POST['firstname']."', user_surname='".$_POST['lastname']."', user_dob='".$_POST['dob']."' where userID='".$_SESSION['id']."'";
$result1 = mysql_query($query1);
echo "<script>alert('Your Information has been changed SuccessFully..'); window.location = './edit.php';</script>";
}
?>
</div>
If anyone is interested in the CSS the styling code is:
.profile {
background:white;
border:1px dashed #ccc;
padding:5px;
}
.profile img {
width=100%;
}
i will really appreciate if anyone can guide me or help me in the right direction

Related

How to remove image from folder at update phpMysql?

Update query from the database:
<?php
include 'dpconnection.php';
$uid=$_REQUEST['upid'];
if (isset($_POST['update'])) {
$category= $_POST['category'];
$pname= $_POST['pname'];
$parea= $_POST['parea'];
$pPrices= $_POST['pPrices'];
$pAddress= $_POST['pAddress'];
$filename = $_FILES['new_image']['name'] ;
$tempname = $_FILES['new_image']['tmp_name'] ;
$filesize = $_FILES['new_image']['size'] ;
$fileextension = explode('.', $filename) ;
$fileextension = strtolower(end($fileextension));
$newfilename = uniqid().'images'.'.'.$fileextension ;
$path = "media/".$newfilename ;
$sql = mysqli_query($conn,"UPDATE product SET c_id='$category', p_name='$pname', p_area='$parea', p_prices='$pPrices',p_address='$pAddress', p_thumb_image='$path' WHERE p_id='$uid'");
if (move_uploaded_file($tempname, $path) && $sql) {
echo "<script>alert('Record Updated successfully');</script>";
echo "<script>location.href = 'adminViewProduct.php';</script>";
} else {
echo "Error updating record: " . $conn->error;
}
}
?>
Image not deleting from folder, i have two if statement but just one else statement so it looks like i am missing something but i really don't know what i am missing.
This is form:
<div class="form-group">
<label>Address</label>
<input class="form-control" type="text" name="pAddress" value="<?php echo $row1['p_address'];?>">
</div>
<div class="form-group">
<label>Thumb Image</label>
<input type="file" name="new_image" class="form-control">
<input class="form-control" type="hidden" name="pimage" value="<?php echo $row1['p_thumb_image'];?>">
<img class="float-left" src="<?php echo $row1['p_thumb_image'];?>" width="100px;"><br><br>
</div>
<div class="form-group text-center">
<input class="btn btn-primary" type="submit" name="update" value="Update">
</div>
</form>
You don't have a code that delete the files in php there's a function called unlink where you can delete a files but first you need fetch the path or filename and delete it before updating your data.
Here is the link of unlink.
https://www.php.net/manual/en/function.unlink.php

Form not sending any data when submitting form : $_POST is empty

I'm trying to submit a simple form with video files in it, a title and a description. My code works on my laptop that has a 8.1.9 php version and not working on my desktop that has 8.1.0 php version.
I checked this page : Form submit is not sending any POST data but I didn't find any solutions.
I checked php.ini but everything was set normaly... I checked for overlappings tags but didn't find any...
I hope that this is not to obvious...:/
Here is my code :
add-videos.php
<?php
require_once './partials/header.php';
include_once './fetch-videos.php';
?>
<h2>Modal Example</h2>
<!-- Trigger/Open The Modal -->
<?php if (isset($_SESSION['user_is_admin'])) : ?>
<button id="myBtn">Ajouter une vidéo</button>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<form action="add-videos-logic.php" method="POST" enctype="multipart/form-data">
<label for="video">Fichier vidéo</label>
<input type="file" name="video" /> <br><br>
<label for="title">Titre de la vidéo</label>
<input type="text" name="title" /> <br><br>
<label for="description">Description</label>
<input type="text" name="description" /> <br><br>
<input type="submit" name="submit" value="envoyer">
</form>
</div>
</div>
<?php endif ?>
<?php while ($fetch = mysqli_fetch_array($query)) { ?>
<h2> <?= $fetch['title'] ?> </h2>
<p><?= $fetch['description'] ?></p>
<video width="320" height="240" cont rols>
<source src="<?= $fetch['source'] ?>" data-id="<?= $fetch['id'] ?>">
</video>
<?php if (isset($_SESSION['user_is_admin'])) : ?>
<button><a href="delete-video.php?video_id=<?= $fetch['id'] ?>&source=<?= $fetch['source'] ?>">Supprimer</button>
<button type="submit" name="update" value="modifier"> </button>
<?php endif ?>
<?php } ?>
<?php
require_once './partials/footer.php';
?>
add-videos-logic.php
<?php
require './config/database.php';
date_default_timezone_set('Asia/Manila');
var_dump($_POST);
var_dump(isset($_POST["submit"]));
if (isset($_POST["submit"])) {
$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
extract($post);
$file_name = $_FILES['video']['name'];
$file_temp = $_FILES['video']['tmp_name'];
$file_size = $_FILES['video']['size'];
if ($file_size < 50000000) {
$file = explode('.', $file_name);
$end = end($file);
$allowed_ext = array('avi', 'flv', 'wmv', 'mov', 'mp4');
if (in_array($end, $allowed_ext)) {
$name = date("Ymd") . time();
$location = 'video/' . $name . "." . $end;
if (move_uploaded_file($file_temp, $location)) {
mysqli_query($connection, "INSERT INTO `videos` VALUES('', '$name', '$location', '$description', '$title')");
header('location: ' . ROOT_URL . '/admin/add-videos.php');
}
} else {
echo "<script>alert('Wrong video format')</script>";
echo "<script>window.location = 'index.php'</script>";
}
} else {
echo "<script>alert('File too large to upload')</script>";
echo "<script>window.location = 'index.php'</script>";
}
}
file size configuration to modify in php.ini post_max_size was too small.
Sorry for this question.
Adios stackoverflow

How to upload/insert image/photo on mysql using php

I searched for some solution and it seems can work by adding some code in it. My codes seems doesn't work. I don't know what to do. I'm new in php. The input type file seems working but the upload button doesn't work and it seems that is the problem of this.
<?php
include ('LoginFunction.php');
$conn = mysqli_connect('localhost','root','','danganan');
$user = $_SESSION['Username'];
$query = "SELECT * FROM tblactivity WHERE Username = '$user'";
$result = mysqli_query($conn,$query);
$row = mysqli_fetch_array($result);
This is the upload function
if (isset($_POST['upload'])) {
$file_name = $_FILES['file']['name'];
$file_type = $_FILES['file']['type'];
$file_size = $_FILES['file']['size'];
$file_tem_loc = $_FILES['file']['tmp_name'];
$file_store = "uploads/".$file_name;
if(move_uploaded_file($file_tem_loc, $file_store)) {
echo "Image uploaded!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Profile info</title>
</head>
<body>
<form method="POST">
<div class="container">
<h1><?php echo "Welcome " . " " . $user; ?></h1>
<img src="<?php $row['Picture']; ?>" width=150; height=150;>
<div class="send-button">
<input type="file" name="file" style="margin-right: -76px;"><br><br>
<input type="submit" name="upload" value="Upload">
<?php
$folder = "uploads/";
if(is_dir($folder)) {
if ($open = opendir($folder)) {
while (($file = readdir($open)) !=false) {
if($file == '.' || $file == '..') continue;
echo '<img src ="uploads/'.$file.'" width = "150" height=150>';
}
closedir($open);
}
}
?>
</div>
<input type="hidden" name="usrid" value="<?php echo $row['ID']; ?>">
<h2>FIRST NAME</h2><input type="text" name="fname" value="<?php echo $row['Fname']; ?>">
<h2>LAST NAME</h2><input type="text" name="lname" value="<?php echo $row['Lname']; ?>">
<h2>USERNAME</h2><input type="text" name="uname" value="<?php echo $row['Username']; ?>">
<h2>PASSWORD</h2><h3><?php echo $row['Password']; ?></h3>
</form>
</body>
</html>

PHP image: Image wont show in PHP Profile page

I have just made a profile page in PHP for a user which displays user profile information including their picture, The image is stored on the database and everytime when I upload an image on the web page, the database does work and update the database profile section but it is not showing on the web browser. :/ i tried different browsers but every browser failed to display the image, the Alt parameter of image does show but no image, i have inspected the page element the image path is there but i can't see the image there?
the code in the profile page is:
<?php
function change_profile_image($userID, $file_temp, $file_extn) {
$file_path ='C:/xampp/htdocs/cricket/user/images/profile/'. substr(md5(time()), 0, 10) . '.' . $file_extn;
move_uploaded_file($file_temp, $file_path);
mysql_query("UPDATE `user` SET `profile` = '" . mysql_real_escape_string($file_path) . "' WHERE `userID` = " . (int)$userID);
}
?>
<?php
$query = "select * from user where userID='".$_SESSION['id']."'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result)
?>
<link rel="stylesheet" type="text/css" href="../css/style.css">
<div id="content">
<h1>Edit Your Information</h1>
<div class="profile">
<?php
if (isset($_FILES['profile'])=== true) {
if (empty($_FILES['profile']['name']) === true) {
echo "Please choose a file!";
} else {
$allowed = array('jpg', 'jpeg', 'gif', 'png');
$file_name = $_FILES['profile']['name'];
$file_extn = strtolower(end(explode('.', $file_name)));
$file_temp = $_FILES['profile']['tmp_name'];
if (in_array($file_extn, $allowed)=== true) {
change_profile_image($_SESSION['id'], $file_temp, $file_extn);
} else {
echo "incorrect file type, Allowed: ";
echo implode(', ', $allowed);
}
}
}
if (empty($row['profile']) === false) {
echo '<img src="', $row['profile'], '" alt="', $row['user_firstname'], '\'s Profile Image">';
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="profile"> <br> <input type="submit">
</form>
</div>
<form method="post" action="">
<div class="txtbox1">Email</div>
<div class="txtbox">
<input type="text" name="email" value="<?php echo $row['user_email']?>"> </div><br><br><br>
<div class="txtbox1">First Name</div>
<div class="txtbox">
<input type="text" name="firstname" value="<?php echo $row['user_firstname']?>"></div><br><br><br>
<div class="txtbox1">Surname</div>
<div class="txtbox">
<input type="text" name="lastname" value="<?php echo $row['user_surname']?>"></div><br><br><br>
<div class="txtbox1">DOB</div>
<div class="txtbox">
<input type="text" name="dob" value="<?php echo $row['user_dob']?>"></div><br>
<div class="txtbox"><input type="submit" value="update" name="update"> </div>
</form>
<?php
if(isset($_POST['update'])){
$query1 = "update user set user_email='".$_POST['email']."', user_firstname='".$_POST['firstname']."', user_surname='".$_POST['lastname']."', user_dob='".$_POST['dob']."' where userID='".$_SESSION['id']."'";
$result1 = mysql_query($query1);
echo "<script>alert('Your Information has been changed SuccessFully..'); window.location = './edit.php';</script>";
}
?>
</div>
If anyone is interested in the CSS the styling code is:
.profile {
background:white;
border:1px dashed #ccc;
padding:5px;
}
.profile img {
width=100%;
}
i will really appreciate if anyone can guide me or help me in the right direction

Php - Combining multiple form elements

I am trying to combine multiple form elements. I have 2 buttons, 1 tex tbox and 1 upload box and 1 drop down menu as shown below:
So the user can select a directory where to upload, create a new one by clicking the 'Create' button and Upload files. However, with my current code, it does not work (files are uploaded to default directory).
This is the code:
<?php
include("login.php");
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>FileStore - Upload Files</title>
<link rel="stylesheet" href="./CSS/style.css" type="text/css" media="screen, projection" />
</head>
<body>
<div id="wrapper">
<header id="header">
<div id="header-content">
<strong><h1>FileStore</h1></strong> Upload multiple files at once!
</div>
<div class="login-info" >
<?php
if ($isLoggedin === false) {
echo ' <form action="" method="POST">
Username: <input name="username" >
Password: <input type="password" name="password" size="8">
<input type="submit" name="submit" value="Login">
</form>';
echo "<p align='right'>You are not logged in.</p>";
echo "<b><a href='registration.php'>Register</a></b>";
}else{
echo $welcomeMsg;
}
?>
</div>
</header><!-- #header-->
<section id="middle" align="center">
<div id="container">
<br><br>
<div id="content">
<strong><h1>Upload files</h1></strong><br><br>
<div id="upload-file" >
<form action="" method="post" enctype="multipart/form-data">
<input type="file" value="" name="upload[]" multiple>
<button type="submit">Upload!</button>
</form>
<select name="myDirs">
<option value="" selected="selected">Select a folder</option>
<?php
if (chdir("./files/" . $userid)) {
$dirs = glob('*', GLOB_ONLYDIR);
foreach($dirs as $val){
echo '<option value="'.$val.'">'.$val."</option>\n";
}
} else {
echo 'Changing directory failed.';
}
?>
<?php
include("dbConfig.php");
$Username = $_SESSION["username"];
global $userid;
$sql = "SELECT UserID FROM users WHERE Username = '".$Username."'";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
$userid = $row['UserID'];
}
$newDir = $_REQUEST["newDir"];
$upload_dir = './files/' . (string)$userid . '/';
$folder_path = $upload_dir . "/" . $newDir . "/" ;
if (!file_exists($folder_path)) {
mkdir($folder_path, 0777, true);
chmod($folder_path, 0777);
echo "Folder" . $newDir . "created.";
echo "Redirecting to upload page..";
header( 'Location: ../upload.php' ) ;
} else {
echo "Error creating " . $newDir;
}
echo "<form action='' method='post'>";
echo "<input type='text' name='newDir'> ";
echo "<input type='submit' value='Create'>";
echo "</form>";
?>
<?php
include("dbConfig.php");
$Username = $_SESSION["username"];
global $userid;
global $up_path;
$error_message[0] = "Unknown problem with upload.";
$error_message[1] = "Uploaded file too large (load_max_filesize).";
$error_message[2] = "Uploaded file too large (MAX_FILE_SIZE).";
$error_message[3] = "File was only partially uploaded.";
$error_message[4] = "Choose a file to upload.";
$sql = "SELECT UserID FROM users WHERE Username = '".$Username."'";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
$userid = $row['UserID'];
}
$vpath = strlen($userid);
$upload_dir = './files/' . (string)$userid . '/';
$num_files = count($_FILES['upload']['name']);
if (!file_exists($upload_dir)) {
mkdir($upload_dir, 0777, true);
chmod($upload_dir, 0777);
} else {
for ($i=0; $i < $num_files; $i++) {
$upload_file = $upload_dir . urlencode(basename($_FILES['upload']['name'][$i]));
if (#is_uploaded_file($_FILES['upload']['tmp_name'][$i])) {
if (#move_uploaded_file($_FILES['upload']['tmp_name'][$i],
$upload_file)) {
/* Great success... */
echo basename($upload_file) . " has been uploaded. <br>";
//$content = file_get_contents($upload_file);
//echo $content;
} else {
print $error_message[$_FILES['upload']['error'][$i]];
}
} else {
print $error_message[$_FILES['upload']['error'][$i]];
}
}
}
?>
</div>
</div><!-- #content-->
</div><!-- #container-->
<aside id="sideLeft">
<div id="menu-x" align="center"><br>
<strong>Menu</strong><br><br>
<div class="menu">
<ul>
<li>Home</li>
<li>Upload</li>
<li>Files</li>
<li>About</li>
<li>Help</li>
<li>Logout</li>
</ul>
<br style="clear:left"/>
</div>
</div>
</aside><!-- #sideLeft -->
</section><!-- #middle-->
<footer id="footer">
<strong>FileStore:</strong> A CMT 3315 Project by Brian Livori
</footer><!-- #footer -->
</div><!-- #wrapper -->
</body>
</html>
I wan to enable the user to create a directory before uploading, and then choose a directory to upload by selecting an option from the drop down.
How can I do this?
Your form tag ends before select box so move it below select box and you should get your post values related to chosen upload dir.

Categories