Image not getting upload in PHP - php

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>

Related

Problems by inserting multiple files into database

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>

Image upload not received

I am getting the img_description but my img_name remain empty.
Basic php code
$msg = "";
if(isset($_POST['upload'])) {
$target = "images/".basename($_FILES['image']['name']); // get error here
$image = $_FILES['image']['name']; // get error here
$text = $_POST['text'];
$sql = "INSERT into images (img_name, img_description) values ('$image', '$text')";
$smt = $heidisql->prepare($sql);
$smt->execute();
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) { //error here
$msg = "Image upload sucessfully";
}else {
$msg = "Image failed to upload properly";
}
My basic Form
<form method="POST" action="checkout.php" enctype="multipart/form-date">
<input type="hidden" name="size" value="1000000">
<div>
<input type="file" name="image">
</div>
<div>
<textarea name="text" cols="40" rows="4" placeholder="img text decription" ></textarea>
</div>
<div>
<input type="submit" name="upload" value="upload img">
</div>
</form>
Table: images, attribute -> img_id, img_name, img_description
Error: Undefined index: image
You have a typo in the enctype in your form tag.
Change
multipart/form-date
to
multipart/form-data
You have a write mistake enctype="multipart/form-date" use enctype="multipart/form-data" instead.

PHP Fail to upload image

test.php file
<?php
if(isset($_POST['submit']))
{
if(getimagesize($_FILES['image']['tmp_name'])==FALSE)
{
echo "Please select an image";
}
else
{
$image = addslashes($_FILES['image']['tmp_name']);
$image = file_get_contents($image);
$image = base64_encode($image);
saveimage($image);
}
}
function saveimage($image)
{
$con=mysql_connect("localhost","root","");
mysql_select_db("food",$con);
$query = "INSERT INTO info(image) VALUES ('$image')";
$result = mysql_query($query,$con);
if($result)
{
echo "<br> Image upload";
}
else
{
echo "<br> NOT";
}
}
function displayimage()
{
$con=mysql_connect("localhost","root","");
mysql_select_db("food",$con);
$query = "SELECT image from info";
$result = mysql_query($query,$con);
$row=mysql_fetch_array($result);
echo '<img src="data:image/jpeg;base64,'.$row["image"].'" width="200" height="200"/>';
}
?>
html file
<form method="post" action="test.php">
<label>Image:</label>
<input type="file" name="image"><br>
<input class="btn btn-primary" type="submit" name="submit" value="Confirm" style="height:50px; width:100px;">
<br><br>
</form>
Now i want to display the picture. But i keep getting a blank picture instead. Is there any problem with my display code there? Thanks
/dummy//aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa//dummy
add enctype="multipart/form-data" in your form
<form method="post" action="test.php" enctype="multipart/form-data">
Your are missing enctype="multipart/form-data" in your form tag.
The enctype attribute specifies how the form data should be encoded when submitting it to the server.
Note: The enctype attribute can be used only with method="post".
Add enctype="multipart/form-data" to your form tag.
you make a POST request, you have to encode the data that forms the body of the request.
When writing client-side code,you need to use multipart/form-data if your form includes any elements.

PHP Form - File Upload Field for Signup

I have created a PHP signup form for visitors to fill and submit that asks for their basic information.
I am trying to accomplish the following two tasks;
Add Image/File Upload Field
Redirect them to a confirmation page
I have been unable to make it work. Below is what I have.
My Code
HTML Form
<form name="form1" method="post" action="signup.php">
Username: <input type="text" name="user">
<br>Email: <input type="text" name="mail">
<br>Experience: <select name="exp"> <option value="beginner">Beginner</option>
<option value="intermediate">Intermediate</option> <option value="advanced">Advanced</option>
</select><br> <input type="submit" name="Submit" value="Sign Up">
</form>
Signup.php
<?php
$username = $_POST['user'];
$email = $_POST['mail'];
$experience = $_POST['exp'];
//the data
$data = "$username | $email | $experience\n";
//open the file and choose the mode
$fh = fopen("users.txt", "a");
fwrite($fh, $data);
//close the file
fclose($fh);
print "User Submitted";
?>
It seems like you lack an input field in your HTML to begin with.
here's an example of a form for uploading files.
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
Once you've done that you're not quite there yet because your file is stored in a temporary folder, you will need to move the file to your uploads folder like so:
$target_file = "uploads/" . basename ($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)
I hope this helps!
Ta add an upload you need to add enctype="multipart/form-data" to your form tag,then add the upload field. For the Email field change the type to HTML5 type="email", this will do a little validaation check that it is in the correct format. At the bottom of the php file add a location header if all went well. You could put it all in one file with an if statement at the top.You should also sanitize your inputs
this is a upload file script which will loop through all the data of a file and insert
if(isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
echo "<h1>" . "File ". $_FILES['filename']['name']. "uploaded successfully." . "</h1>";
}
$csv_file=$_FILES['filename']['tmp_name'];
$type=$_FILES['filename']['type'];
$handle = fopen($csv_file, "r");
$i=0;
while (($data = fgetcsv($handle)) !== FALSE) {
if($i>0) {
$import="insert into `table_name`(col1,col2,col3,col4,col5,col6,col7)values('".addslashes($data[0])."','".addslashes($data[1])."','".addslashes($data[2])."','".addslashes($data[3])."','".addslashes($data[4])."','$data[5]','$data[6]')";
mysql_query($import) or die(mysql_error());
}
$i=1;
}
echo "Success";
echo "<br>";
echo $_FILES['filename']['type'];
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="" id="">
Choose File:<br>
<input name="filename" type="file" />
<input type="submit" name="submit" value="submit" />
</form>

Data insert and Image Upload

I'm trying to Insert some data from a Text Area in MySQL and Upload an image in a specific folder and save the image name in MySQL. But when i submit it only text area data (titulli , permbjajtja) gets inserted in database, the picture doesn't get uploaded and name of the picture is not inserted in MySQL.
Here is the form code:
<form role="form" method="post" action="shtolajm1.php" enctype="multipart/form-data">
<div class="form-group">
<textarea class="form-control" rows="3" name="titulli"></textarea>
<textarea class="form-control" rows="10" name="permbajtja"></textarea>
<input type="file" name="files[]" style="margin-left:-5;"/>
<button type="submit" name="submit" class="btn btn-default" style="float:right;margin-top:10px;">Insert</button>
</div>
</form>
and here is the code that im using to insert the data to mysql and upload the picture:
if(isset($_POST['submit']))
{
if (!strlen(trim($_POST['titulli']))==0 && !strlen(trim($_POST['permbajtja']))==0 )
{
$titulli=$_POST['titulli'];
$permbajtja=$_POST['permbajtja'];
$data=date('Y-m-d');
$time=date('H:i');
$username=$_SESSION['myusername'];
$InsertQuerry="INSERT INTO lajme (titulli, permbajtja, data, ora, useri) VALUES ('$titulli','$permbajtja','$data','$time','$username')";
if (mysql_query($InsertQuerry))
{
echo "Data inserted";
}
}
else
{
echo "Type the title and container";
}
}
$id_lajmi=mysql_insert_id();
if(isset($_FILES['files']['name']))
{
for($i=0; $i<count($_FILES['files']['name']); $i++)
{
$tmpFilePath = $_FILES['files']['tmp_name'][$i];
if ($tmpFilePath != "")
{
$path = "lajme_foto/";
list($txt, $ext) = explode(".", $name);
if(move_uploaded_file($_FILES['files']['tmp_name'][$i], $path.$id_lajmi."photo".$i.".".$ext))
{
$emri_fotos=$path.$id_lajmi."photo".$i.".".$ext;
$insertfoto="INSERT INTO lajme_foto (id_lajmi,emri_fotos) VALUES ('$id_lajmi','$emri_fotos')";
if(mysql_query($insertfoto))
{
echo "Image Inserted";
}
}
}
}
}
Can anyone help me solve this problem?
Thanks in advice.
You forgot to add enctype="multipart/form-data" to your form.
Your form tag should look like this:
<form role="form" method="post" action="shtolajm1.php" enctype="multipart/form-data">

Categories