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.
Related
I'm new to database designing and I'm working on an upload formula.
In my example I have 3 input buttons. The upload of all selected file to my uploads folder works fine, but my problem is, only the first of all selected files gets inserted in my database.
Here is my code:
$db = mysqli_connect("localhost", "root", "", "xy");
if (isset($_POST['upload_docs'])) {
for($mi = 0; $mi < count($_FILES['sel_file']['tmp_name']); $mi++) {
$doc = $_FILES['sel_doc']['name'][$mi];
$tmp_doc = $_FILES['sel_doc']['tmp_name'][$mi];
$target = "doc_uploads/". basename($doc);
if (move_uploaded_file($$tmp_doc, $target)) {
echo "Upload successfully!";
}
else {
echo "Upload failed!";
}
}
}
$sql_f = "INSERT INTO docs (doc_file) VALUES ('$doc')";
mysqli_query($db, $sql_f);
<form method="POST" action="upload.php" enctype="multipart/form-data">
<div>
<input type="file" name="sel_doc[]" multiple="multiple">
<input type="file" name="sel_doc[]" multiple="multiple">
<input type="file" name="sel_doc[]" multiple="multiple">
</div>
<div id="sel_button">
<button type="submit" name="upload_docs">upload</button>
</div>
</form>
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);
PHP-newbie here
I'm trying to make a simple script to upload a photo to my data-base.
I know it's not very efective, but I want to do it anyway.
So I have these two .php files:
<html>
<body>
<form action="upload.php">
<input type="file" name="photo">
<input type="submit" name="submit" value="Submit" />
</form>
</body>
and
<?php
error_reporting( error_reporting() & ~E_NOTICE );
$conn = mysqli_connect("localhost", "root", "", "poze");
if(!$conn){
die("Connection failed: ".mysqli_connect_error());
}
else echo "Succes";
$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
//$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
//$imageType = mysql_real_escape_string($_FILES["image"]["type"]);
if($imageName==NULL){
echo "Null";
}
else{
echo "The photo is okay!";
}
After uploading any kind of picture, it echoes out as empty.
Could, anyone explain me why? And how could I solve it?
first you must use this form element:
<form enctype="multipart/form-data" action="upload.php" method="POST">
and secondly you must read file content from temporary folder and save to DB:
$tmpName = $_FILES['userfile']['tmp_name'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
then save this content to a field in mysql of type BLOB.
PHP-newbie here
I'm trying to make a simple script to upload a photo to my data-base.
I know it's not very efective, but I want to do it anyway.
So I have these two .php files:
<html>
<body>
<form action="upload.php">
<input type="file" name="photo">
<input type="submit" name="submit" value="Submit" />
</form>
</body>
and
<?php
error_reporting( error_reporting() & ~E_NOTICE );
$conn = mysqli_connect("localhost", "root", "", "poze");
if(!$conn){
die("Connection failed: ".mysqli_connect_error());
}
else echo "Succes";
$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
//$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
//$imageType = mysql_real_escape_string($_FILES["image"]["type"]);
if($imageName==NULL){
echo "Null";
}
else{
echo "The photo is okay!";
}
After uploading any kind of picture, it echoes out as empty.
Could, anyone explain me why? And how could I solve it?
SOLVING:
I changed the APIs so that I would only use mysqli
Index:
<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="file" name="photo">
<input type="submit" name="submit" value="Submit" />
</form>
</body>
PHP script:
<?php
error_reporting( error_reporting() & ~E_NOTICE );
$conn = mysqli_connect("localhost", "root", "", "poze");
if(!$conn){
die("Connection failed: ".mysqli_connect_error());
}
else echo "Succes"."<br>";
$imageName = mysqli_real_escape_string($conn ,$_FILES["photo"]["name"]);
$imageData = mysqli_real_escape_string($conn, file_get_contents($_FILES["photo"]["tmp_name"]));
$imageType = mysqli_real_escape_string($conn, $_FILES["photo"]["type"]);
//process the image ...
I am uploading an image using the following php code but the file is not getting upload.
if(isset($_POST['submit'])){
$title = $_POST['title'];
$target_folder = "../newsimageuploads/";
$bannerimagelink = "http://example.com/newsimageuploads";
$bannerimage = addslashes(file_get_contents($_FILES['bannerimage']['tmp_name']));
$bannerimage_name = addslashes($_FILES['bannerimage']['name']);
$bannerimage_size = getimagesize($_FILES['bannerimage']['tmp_name']);
if ($bannerimage!=""){
$rand = rand(111111, 9999999);
$fname = $_FILES["bannerimage"]["name"];
$newname = "Image ".$rand.".png";
move_uploaded_file($_FILES["bannerimage"]["tmp_name"], $target_folder.$newname);
$bannerimage_location = $bannerimagelink."/".$newname;
}
$query =mysql_query("INSERT INTO mytable (title,image) VALUES ('$title','$bannerimage_location')")or die(mysql_error());
if (($query) === TRUE) {
echo "<p style='color:green;'>Added Successfully</p>";
} else {
echo "Some Error Occured :(";
}
}
And my HTML part is
<form action="#" method="post">
<input type="text" name="title">
<input type="file" name="bannerimage" accept="image/jpeg,image/png,image/gif">
<button type="submit" name="submit">Add</button>
</form>
My title is getting insert in the MySQL table but image does not.
You are missing enctype='multipart/form-data' in your form
<form action="#" method="post" enctype="multipart/form-data">
Look here for more details
Add
enctype="multipart/form-data"
to the form tag. Without this attribute you will only get the name of the file. But the file itself won't be uploaded.
<form action="#" method="post" enctype="multipart/form-data">
<input type="text" name="title">
<input type="file" name="bannerimage" accept="image/jpeg,image/png,image/gif">
<button type="submit" name="submit">Add</button>
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];