how to update default profile pic localhost php mysql - php

I want to update the default profile picture
MY form is
<div class="profile-photo-container">
<img src="images/profile-photo.jpg" alt="Profile Photo" class="img-responsive" />
</div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=post enctype="multipart/form-data" >
<input type=hidden name="MAX_FILE_SIZE" value="1000000" />
<input type=submit value=submit />
</form>
And display code is
upload code is from this link

I do not see any php code but here is a code I use: please pay attention this is very basic, check security, etc.
Create a form:
<form action="handler.php" method='post' enctype="multipart/form-data">
Profile Description : <input type="text" name="description"/>
<input type="file" name="profile" value="profile" accept="image/png"/>
<input type="submit" name="submit" value="Upload"/>
</form>
handler.php
$name= $_FILES['profile']['name'];
$tmp_name= $_FILES['profile']['tmp_name'];
$submitbutton= $_POST['submit'];
$position= strpos($profile, ".");
$fileextension= substr($name, $position + 1);
$fileextension= strtolower($fileextension);
$description=$_POST['description'];
if (isset($name)) {
$path= 'path/to-your-upload/folder/';
if (!empty($name)){
if (move_uploaded_file($tmp_name, $path.$name)) {
echo 'Uploaded to Server !';
//connection to database
$conn= mysqli_connect($host, $user, $password);
if (!$conn)
{
die ('Could not connect:' . mysqli_error($conn));
}
mysqli_select_db($conn, $dbase);
if(!empty($description)){
mysqli_query($conn, "UPDATE $table SET description= '".$description."', profile_pic='".$name."' WHERE user_id='".$_SESSION['user_id']."' AND user_email='".$_SESSION['user_email']."' ");
}
mysqli_close($conn);

Related

Image not uploading into mysql database php

I am trying to upload an image by updating MySQL table column with the image location.
<?php
session_start();
if(isset($_POST['upload']))
{
$link = mysqli_connect("localhost", "root", "root", "rental");
if($link == false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$image = $_FILES['propic']['tmp_name'];
$propic = addslashes(file_get_contents($image));
$lipic = "img/user.png";
$aadhar="img/user.png";
$sql= "UPDATE profiles SET profilepic='$propic', license='$lipic', aadhar='$aadhar' WHERE email='".$_SESSION['email']."'";
$result=mysqli_query($link,$sql);
}
?>
HTML Code:
<form id="form" method="post" action="profile.php" enctype="multipart/form-data">
<input type="file" name="propic" accept="image/*">
<input type="file" name="lipic" accept="image/*">
<input type="file" name="aapic" accept="image/*">
<input type="submit" class="mybutton myfont" name="upload" value="Submit" style="width:100px; background-color:black;">
</form>
MySQL table
Try this code. You are missing file uploading code.
$image = $_FILES['propic']['tmp_name'];
$name = "img/$_FILES['propic']['name']";
move_uploaded_file($image, $name); //Here you have to upload a file in a folder.
This is just for a reference. Use this and you will be able to upload a file on server.

Unable to upload the images to mySQL database

Still a newb to coding . have almost no idea what im doing . i tried to make a php page which would let me upload and view an image . do not know what is wrong . i tried to do it as correctly as possible . could someone please help me out ?
<!doctype html>
<html>
<head>
</head>
<body>
<form method="post">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
<?php
if(isset($_POST['submit']))
echo "button has been clicked";
$con = mysqli_connect("127.0.0.1","root","","demo");
if(!$con)
echo "didnt connect to database ";
else echo "connected ";
$imagename= mysqli_real_escape_string($_FILES['image'] ['name']);
$imagefile =mysqli_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
$qry = "INSERT INTO image (name,file) VALUES ('$imagename','$imagefile')";
$result = mysqli_query($con,$qry);
if($result)
echo "image has been uploaded";
viewimage();
function viewimage()
{$recon = mysqli_connect("127.0.0.1","root","","demo");
$view = "SELECT * FROM image ";
$data =mysqli_query($recon,$view);
$res2 =mysqli_fetch_assoc($data);
$currimage =$res2['file'];
echo "$currimage <br/>";
}
?>
</body>
</html>
To be able to catch a post variable, you need to submit the form and handle the action. The first problem with your code is that your form is not complete - it's missing a closing tag. Second thing, to be able to send a file through the post, you'll need multipart form. You should add enctype="multipart/form-data" as an attribute of the form.
So, instead of
<form method="post">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
You'll need
<form method="post" enctype="multipart/form-data">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
</form>
you must move the uploaded image to server
try this code -- create directory /uploads/
<!doctype html>
<html>
<head>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
<?php
if(isset($_POST['submit']))
echo "button has been clicked";
$con = mysqli_connect("127.0.0.1","root","","demo");
if(!$con)
echo "didnt connect to database ";
else echo "connected";
$uploads_dir = '/uploads';
$tmp_name = $_FILES["image"]["tmp_name"];
$name = $_FILES["image"]["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
$qry = "INSERT INTO image (name,file) VALUES ('$name','$tmp_name')";
$result = mysqli_query($con,$qry);
if($result)
echo "image has been uploaded";
viewimage();
function viewimage()
{$recon = mysqli_connect("127.0.0.1","root","","demo");
$view = "SELECT * FROM image ";
$data =mysqli_query($recon,$view);
$res2 =mysqli_fetch_assoc($data);
$currimage =$res2['file'];
echo '<img src="'.$currimage.'" /> <br/>';
}
?>
</body>
</html>

I Want to Upload an image in database through php,the image is not uploading in database

i tried to upload an image but when getimagesize the image is empty it is returning false.. and warning is coming ,it is not saving in database
.the database name is project and table name is images and fields are name and image .Here is a code...
<?php
ini_set('mysql.connect_timeout',300);
ini_set('default_socket_timeout',300);
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/formdata">
<input type="file" name="image"><br><br>
<input type="submit" name="submit" value="upload">
</form>
<?php
// Create connection
$conn = mysql_connect('localhost', 'root', '');
// Check connection
if (mysqli_connect_error()) {
die("Database connection failed: " . mysqli_connect_error());
}
else
{
echo "Connected successfully";
}
//data upload
if( isset($_POST['submit'] ))
{
if(getimagesize($_FILES['image']['tmp_name'])==FALSE) //image size is checked
{
echo "upload image";
}
else
{
$image= addslashes($_FILES['image']['tmp_name']);
$name=addslashes($_FILES['image']['name']);
$image=file_get_contents($image);
$image= base64_encode($image);
saveimage($name,$image);
}
}
function saveimage($name,$image)
{
$conn = mysql_connect('localhost', 'root','');
mysql_select_db("project",$conn);
$result = mysql_query("insert into images(name,image) values('$name','$image')"); //query implemented
}
?> //function written to save image
</body>
</html>
Edit this line
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<?php
ini_set('mysql.connect_timeout',300);
ini_set('default_socket_timeout',300);
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="image"><br><br>
<input type="submit" name="submit" value="upload">
</form>
<?php
// Create connection
$conn = mysql_connect('localhost', 'root', '');
// Check connection
if (mysqli_connect_error()) {
die("Database connection failed: " . mysqli_connect_error());
}
else
{
echo "Connected successfully";
}
//data upload
if( isset($_POST['submit'] ))
{
mysql_select_db("project",$conn);
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$name=addslashes($_FILES['image']['name']);
$result = mysql_query("insert into images(name,image) values('.$name.','.$image.')");
mysql_close($conn);
}?>
</body>
</html>

file upload script not working with variables

I have a PDF uploader that is supposed to save a file to a file path that is based off of a username variable selected from a drop down menu.
Everything works but the uploader, as it displays the usernames and the directories are created upon registration. So there is no issue with that. My issue lies within the code below with the uploader:
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="pdf" /><br />
<select name="folder">
<?php
$con=mysqli_connect("host","user","pass","dbname");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT `first_name`, `last_name`, `username` FROM `cw_users` WHERE 1");
$user = 'username';
while($row = mysqli_fetch_array($result)) {
echo "<option value='". $row["username"] ."'>";
echo " $row[username] ";
echo "</option>";
}
mysqli_close($con);
?>
<?php
if (isset($_POST['submit'])) {
$pdfDirectory = "Users/".$_POST['folder']."/uploaded/";
//get the name of the file
$filename = basename( $_FILES['pdf']['name'], ".pdf");
//remove all characters from the file name other than letters, numbers, hyphens and underscores
$filename = preg_replace("/[^A-Za-z0-9_-]/", "", $filename).".pdf";
if (move_uploaded_file($_FILES['pdf']['tmp_name'], $pdfDirectory.$filename)) {
//the path to the PDF file
$pdfWithPath = $pdfDirectory.$filename;
}
}
?>
</select>
<input type="submit" value="Upload pdf" name="upload_pdf" />
</form>
P.S. if this could be adjusted to upload multiple files at the same time that would be great.
Your conditional statement if(isset($_POST['submit'])) is looking for a submit button named "submit", yet yours is named "upload_pdf".
<input type="submit" value="Upload pdf" name="upload_pdf" />
^^^^^^^^^^^^^^^^^
That should either read as if(isset($_POST['upload_pdf'])) or rename your submit button to:
<input type="submit" value="Upload pdf" name="submit" />

how to add many images to a mysql database?

i have a form which is able to upload multiple selected files to the folder. I just need to find how how to insert them into the database using PHP?
HTML form
<form name="demoFiler" id="demoFiler" enctype="multipart/form-data">
<input type="file" name="multiUpload" id="multiUpload" multiple />
<input type="submit" name="submitHandler" id="submitHandler" value="Upload" class="buttonUpload" />
</form>
PHP code
if($_SERVER['REQUEST_METHOD'] == "POST"){
if(move_uploaded_file($_FILES['file']['tmp_name'], "uploads/".$_FILES['file']['name'])){
echo($_POST['index']);
}
exit;
}
This is my code and first of all I can't get the files to be moved to the upload folder:
<?php
$username = "root";
$password = "";
$host = "localhost";
$database = "db_holiday";
// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Select your database
mysql_select_db ($database);
if($_SERVER['REQUEST_METHOD']=="POST")
{
foreach ($_FILES['multiUpload']['name'] as $fileName) {
$uploaddir= '/upload/';
$uploadfile = $uploaddir . basename($fileName);
if (move_uploaded_file($fileName, $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="multiUpload[]" id="multiUpload" multiple="multiple" />
<input type="submit" name="button"/>
</form>
HTML:
<input type="file" name="multiUpload[]" id="multiUpload" multiple />
php:
foreach ($_FILES['multiUpload'] as $fileName) {
//write your code here
//$fileName
}
Use 2 input type="file" elements
<input type="file" name="img_1" />
<input type="file" name="img_2" />
Access each file using
$_FILES['img_1'][];
$_FILES['img_2'][];
Or you can also use an array like
<input type="file" name="img[]" />
<input type="file" name="img[]" />
<!--Which is often used in checkboxes-->
And access each image using
$_FILES['img']['tmp_name'][0];
$_FILES['img']['tmp_name'][1];

Categories