Problems by inserting multiple files into database - php

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>

Related

[PHP][MySQL] How to implement a title field for each selected file in an upload form?

I'm a php/mysql newbie and need your help!
I have an upload formula to upload multiple files to a database, selected one by one.
The files upload works fine, but I want to add a title field for each selected file. I have no idea how to implement this in my code.
Here is my code:
$db = mysqli_connect("localhost", "root", "", "xy");
if (isset($_POST['upload_docs'])) {
for($mi = 0; $mi < count($_FILES['sel_doc']['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[]">
<input type="file" name="sel_doc[]">
<input type="file" name="sel_doc[]">
<input type="file" name="sel_doc[]">
<input type="file" name="sel_doc[]">
</div>
<div id="upload_button">
<button type="submit" name="upload_docs">upload</button>
</div>
</form>
This is how 1 upload would looks like:
| id_doc | doc_file | doc_title|upload_id|
++++++++++++++++++++++++++++++++++++++++++++++++
|.....1......|...a.pdf...|....vvv....|....1....|
|.....2......|...b.pdf...|....www....|....1....|
|.....3......|...c.pdf...|....xxx....|....1....|
|.....4......|...d.pdf...|....yyy....|....1....|
|.....5......|...e.pdf...|....zzz....|....1....|
In my orginal code I insert in 2 tables for 1:n realationship (because of the "upload_id").
HTML
<div>
<?php for ($i=0; $i<5; $i++){ ?>
<b><?php echo $i ?></b><br>
File: <input type="file" name="sel_doc[<?php echo $i ?>]"><br>
Title: <input name="title[<?php echo $i ?>]"><br>
<?php } ?>
</div>
Will print this:
<div>
<b>0</b><br>
File: <input type="file" name="sel_doc[0]"><br>
Title: <input name="title[0]"><br>
<b>1</b><br>
File: <input type="file" name="sel_doc[1]"><br>
Title: <input name="title[1]"><br>
<b>2</b><br>
File: <input type="file" name="sel_doc[2]"><br>
Title: <input name="title[2]"><br>
<b>3</b><br>
File: <input type="file" name="sel_doc[3]"><br>
Title: <input name="title[3]"><br>
<b>4</b><br>
File: <input type="file" name="sel_doc[4]"><br>
Title: <input name="title[4]"><br>
</div>
PHP code
$db = mysqli_connect("localhost", "root", "", "xy");
if (isset($_POST['upload_docs'])) {
for($mi = 0; $mi < count($_FILES['sel_doc']['tmp_name']); $mi++) {
$doc = $_FILES['sel_doc']['name'][$mi];
$tmp_doc = $_FILES['sel_doc']['tmp_name'][$mi];
$target = "doc_uploads/". basename($doc);
$title = (isset($_POST['title'][$mi])?$_POST['title'][$mi]:"");
if (move_uploaded_file($tmp_doc, $target)) {
echo "Upload successfully!";
}
else {
echo "Upload failed!";
}
$sql_f = "INSERT INTO docs (doc_file, title) VALUES ('$doc', '$title')";
mysqli_query($db, $sql_f);
}
}
Btw; This example code is not safe for sql injection.

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.

Image not getting upload in 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>

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