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
Related
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
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
I am trying to upload a pdf file and an image in php mysql but it seems that the move_uploaded_file function can only work with one file. I have tried to make it work with both files but it doesn't seem to me working. It moves just the images to the target folder and adds both image name and pdf name to the database but it doesn't move the pdf to target folder. This is the code. pls help
<?php
session_start();
require_once("includedfunctions.php");
include 'dbh.php';
if (isset($_POST['submit'])){
$title=$_POST['title'];
$author=$_POST['author'];
$target = "img/pic_book/";
$target2 = "img/pdf/";
$imgname = basename($_FILES["cphoto"]["name"]);
$bookname = basename($_FILES["book"]["name"]);
$newname = $target.basename($_FILES["cphoto"]["name"]);
$newname2 = $target2.basename($_FILES["book"]["name"]);
$img_type = pathinfo($newname,PATHINFO_EXTENSION);
$book_type = pathinfo($newname2,PATHINFO_EXTENSION);
if($img_type!='jpg' && $img_type!='JPG' && $img_type!='png' && $img_type!='PNG'){
$message="Please ensure you are entering an image";
}else{
if($book_type != 'pdf'){
$message="books must be uploaded in pdf format";
}else{
if(!preg_match("/^[a-zA-Z'-]+$/",$author)){
$message = "<p style='color:red'>Please enter the real name of the author; not a nickname.</p>";
}else{
if (move_uploaded_file($_FILES["cphoto"]["tmp_name"], $newname)) {
if(move_uploaded_file($_FILES["book"]["tmp_name"], $newname2));{
$sql = "INSERT INTO books (Title, Author, pathtopdf, pathtoimage) VALUES ('$title', '$author', '$bookname', '$imgname')";
$result = mysqli_query($conn, $sql);
if($result){
$message = "upload successful";
}else{
$message = "upload failed1";
}
}
}else{
$message = "upload failed";
}
}
}
}
}
else{
$message="";
$title="";
}
?>
<html>
<head>
<title>Libraria</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/contactcss.css" rel="stylesheet">
<script src="js/respond.js"></script>
</head>
<body>
<br><br><br><br><br>
<!-- content -->
<div class="container">
<?php
echo '<p>Welcome ' . $_SESSION['name']. '</p><br>';
echo '<p>' . $message. ' </p>';
?>
<br><br>
<!--form-->
<div class="row">
<form action="admin2.php" method = "post" enctype="multipart/form-data">
<label for="Title">Title</label><br>
<input type="text" id="fname" value ="<?php echo $title; ?>" name="title" placeholder="Title of the book" required><br>
<label for="author">Author</label><br>
<input type="text" id="lname" name="author" placeholder="Author of the book" required><br>
<label for="Cover photo">Cover photo</label><br>
<input type="file" id="cphoto" name="cphoto" required><br>
<label for="book">Book</label>
<input type="file" id="book" name="book" required><br>
<button class="submit" type="submit" name="submit"><b>Upload</b></button>
</form>
</div>
</div>
</body>
</html>
Code is all correct. just check pdf size. if size is more than 4MB than it will not allowed to upload. you need to increase upload file size in php.ini file or apache config settting file.
Have a great day :)
application/pdf is the mime type for pdf not just pdf.
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
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.