Insert Name of Pic Into MySQL - php

I have simple code to upload multi pics on my server.. Evrything works fine but I need to inesert picture names in MySql something like 1.png, 2.png, 3.png
Please for help.. I am still learning..
form.html
<form action="upload.php" method="post" enctype="multipart/form-data">
<p><input type="file" name="img[]"></p>
<p><input type="file" name="img[]"></p>
<p><input type="file" name="img[]"></p>
<input type="submit" value="Upload all files">
</form>
upload.php
<?php
if(isset($_FILES['img'])){
$name_array = $_FILES['img']['name'];
$tmp_name_array = $_FILES['img']['tmp_name'];
$type_array = $_FILES['img']['type'];
$size_array = $_FILES['img']['size'];
$error_array = $_FILES['img']['error'];
for($i = 0; $i < count($tmp_name_array); $i++){
if(move_uploaded_file($tmp_name_array[$i], "pics/".$name_array[$i])){
echo $name_array[$i]." upload is complete<br>";
} else {
echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
}
}
}
?>

Try this upload.php:
<?php
if(isset($_FILES['img'])){
foreach ($_FILES['img'] as $img) {
$name_array[] = $img['name'];
$tmp_name_array[] = $img['tmp_name'];
$type_array[] = $img['type'];
$size_array[] = $img['size'];
$error_array[] = $img['error'];
for($i = 0; $i < count($tmp_name_array); $i++){
if(move_uploaded_file($tmp_name_array[$i], "pics/".$name_array[$i])){
echo $name_array[$i]." upload is complete<br>";
} else {
echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
}
}
}
}
?>

Please save file name in loop like below
$file = rand(1000,100000)."-".$_FILES['file']['name'];
and then insert after upload
$sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$file','$file_type','$file_size')";
mysql_query($sql);

Related

Common mistakes commited in saving image path to a MySQL table

I am working on a php project. In one of the webpage I want to keep file upload option for users.
Here is my user input form
<?php
$db=mysqli_connect('localhost','test','test123','study')
?>
<form form action="" method="post" enctype="multipart/form-data">
<label for="file_array[0]">Photo :</label>
<input type="file" name="file_array[0]" accept=".jpeg" required />
<input type="file" name="file_array[1]" accept=".jpeg" required />
<label for="file_array[0]">ID Proof :</label>
<input type="submit" value="Upload All">
</form>
<?php
if(isset($_FILES['file_array'])){
$name_array = $_FILES['file_array']['name'];
$tmp_name_array = $_FILES['file_array']['tmp_name'];
$type_array = $_FILES['file_array']['type'];
$size_array = $_FILES['file_array']['size'];
$error_array = $_FILES['file_array']['error'];
for($i = 0; $i < count($tmp_name_array); $i++){
$v1=rand(1111,9999);
$v2=rand(1111,9999);
$v3=$v1.$v2;
$v3=md5($v3);
$upload_directory="uploads/";
$TargetPath=$v3.$name_array[$i];
if(move_uploaded_file($tmp_name_array[$i],
$upload_directory.$TargetPath))
{
$sql = "INSERT INTO customers (imagepath)
VALUES ("$upload_directory.$TargetPath")";
echo $name_array[$i]." upload is complete<br>";
}
else {
echo "failed to ".$name_array[$i]."
<br>";
}
}
}
?>
the problem is, Image gets successfully uploaded to the directory, but image path is not being saved into the database.
Any suggestion where I'm doing wrong in my code.
In the above code your not executing the query. It is just saved in $sql. refer below code:
if ($db->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $db->error;
}
saving imagepath in db it also depends on the datatype as well.it will be text to save any length of data.

How to send file name to my gmail with PHP and HTML form?

I would like if someone upload a file gets the name of the file to my gmail.For example, I would like you to email arrives "FileName: background.jpg"
PHP code:
<?php
if(isset($_FILES['file_array'])){
$name_array = $_FILES['file_array']['name'];
$tmp_name_array = $_FILES['file_array']['tmp_name'];
$type_array = $_FILES['file_array']['type'];
$size_array = $_FILES['file_array']['size'];
$error_array = $_FILES['file_array']['error'];
for($i = 0; $i < count($tmp_name_array); $i++){
if(move_uploaded_file($tmp_name_array[$i], "uploads/".$name_array[$i])){
header("Location:");
}
}
}
?>
I've tried to like this:
<?php
if(isset($_FILES['file_array'])){
$name_array = $_FILES['file_array']['name'];
$tmp_name_array = $_FILES['file_array']['tmp_name'];
$type_array = $_FILES['file_array']['type'];
$size_array = $_FILES['file_array']['size'];
$error_array = $_FILES['file_array']['error'];
$za = "mygmail#gmail.com";
$od = "Od: " . $Email . "\r\n";
$info .= "FileName:" .$name_array. "\r\n";
for($i = 0; $i < count($tmp_name_array); $i++){
if(move_uploaded_file($tmp_name_array[$i], "uploads/".$name_array[$i])){
header("Location:");
mail($za, $od, $info);
}
}
}
?>
But I just received in the mail "FileName: array".
HTML code:
<form class="contact_form" action="../assets/php/postavi_oglas.php" method="post" enctype="multipart/form-data">
<label>Izaberite fotografije:</label>
<input class="contact_area" type="file" name="file_array[]">
<input class="contact_area" type="file" name="file_array[]">
<input class="contact_area" type="file" name="file_array[]">
<input class="contact_area" type="file" name="file_array[]">
<input class="contact_area" type="file" name="file_array[]">
<input class="btn btn-theme" type="submit" value="Posalji Fotografije">
</form>
it says array because it is array and need to be in the loop
for($i = 0; $i < count($tmp_name_array); $i++){
if(move_uploaded_file($tmp_name_array[$i], "uploads/".$name_array[$i])){
// a array
$fname=$name_array[$i];
$info= "FileName:" .$fname. "\r\n";
header("Location:");
mail($za, $od, $info);
}
You got array here, because you send one in your mail. Just use it like code below and send just on email instead of one email by file. I you like this behavior more.
$info = '';
for($i = 0; $i < count($tmp_name_array); $i++){
if(move_uploaded_file($tmp_name_array[$i], "uploads/".$name_array[$i])){
$info .= 'Filename '.$i.': '.$name_array[$i]. "\r\n";
}
}
mail($za, $od, $info);
header("Location:");

How to insert multiple photos and multiple text fields into mysql database?

Plain and simple. I want to create a form that first of all inserts all fields values into the MySQL database. Second of all, I want to give users options to upload multiple images. but the code below wont insert image into the database. I am totally lost. don't know what else might be wrong with this code.
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post" enctype="multipart/form-data">
<p><input type="file" name="file_array[]"></p>
<p><input type="file" name="file_array[]"></p>
<p><input type="file" name="file_array[]"></p>
<input type="FirstN" name="Firstname" >
<input type="LastN" name="Lastname" >
<input type="submit" value="Upload all files">
</form>
<?php
if(isset($_POST['submit'])) {
if(isset($_FILES['file_array'])) {
$name_array = $_FILES['file_array']['name'];
$tmp_name_array = $_FILES['file_array']['tmp_name'];
$type_array = $_FILES['file_array']['type'];
$size_array = $_FILES['file_array']['size'];
$error_array = $_FILES['file_array']['error'];
for($i = 0; $i < count($tmp_name_array); $i++) {
if(move_uploaded_file($tmp_name_array[$i], "test_uploads/".$name_array[$i])) {
echo $name_array[$i]." upload is complete<br>";
} else {
echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
}
}
}
$sql = "INSERT INTO image, FirstName, LastName VALUES ('{FirstN}','{LastN}','{file_array}')";
}
?>

Multitple Image upload can not save descriptions into database. How to fix this?

I am trying to create a multiple image upload with description on each image. I have used a jquery coe that allows me to add fields on demand. However, even though the images are uploaded and saved into DB, in the description column I get Array[0] instead of the actual description..
How can I fix this?
This is the HTML
<form action="upload.php" method="post" enctype="multipart/form-data">
<div class="input_fields_wrap">
<button class="add_field_button">Add More Fields</button>
<div><input type="file" name="file_array[]"><input type="text" name="description[]" placeholder="Write Description"></div>
</div>
<input type="submit" value="Upload all files">
</form>
and here is the upload.php
if(isset($_FILES['file_array'])){
$name_array = $_FILES['file_array']['name'];
$tmp_name_array = $_FILES['file_array']['tmp_name'];
$type_array = $_FILES['file_array']['type'];
$size_array = $_FILES['file_array']['size'];
$error_array = $_FILES['file_array']['error'];
for($i = 0; $i < count($tmp_name_array); $i++){
if(move_uploaded_file($tmp_name_array[$i], "upload/".$name_array[$i])){
mysql_query("INSERT into projects (`image`,`description`) VALUES('$name_array[$i]','$_POST[description][$i]') ");
echo $name_array[$i]." upload is complete<br>";
} else {
echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
}
}
}
You need to add your description in quotes. So your query would be.
mysql_query("INSERT into projects (`image`,`description`) VALUES('".$name_array[$i]."','".$_POST['description'][$i]."') ");

Upload multiple images to Database MYSQLI

I am planning to have a web gallery. However, it is hard for me to use PHP to insert DB. The following code:
HTML -- I want to make a form which has category and multiple images that can be inserted into DB at the same time.
<form action="upload" method="POST" enctype="multipart/form-data">
<p> Upload : <input type="file" id="file" name="images" /> </p>
<p> Category : <input type="text" name="imageCategory"> </p>
<p> <input type="submit" name="submit" value="Upload!" /> </p>
</form>
DATABASE
I am using imageName as VARCHAR not BLOB TYPE.
PHP
<?php
include ("dbConnect.php");
if(isset($_POST["submit"])) {
$image = $_POST['images']['tmp_name'];
$imageName = $_POST['images']['name'];
$imageSize = $_POST['images']['size'];
$imageType = $_POST['images']['type'];
$imageCategory = $_POST['imageCategory'];
$result = $mysqli->query("INSERT INTO imageTable (imageName, imageCategory, imageSize, imageType)
VALUES ('$imageName', '$imageCategory', '$imageSize' , '$imageType' );")
or die(mysqli_error($mysqli));
} else {
echo "<p> It is not working </p>";
}
header("location: index");
$mysqli->close();
?>
The problem is, the category is the only one has inserted into the database successfully. But not with the imageName, imageType, and imageSize. And also i want the image to be stored into database so that I can retrieve the image from DB on the other web page. Any ideas?
You can use 'multiple' property in the 'input' tag like this :
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<p> <input name="userfile[]" type="file" multiple='multiple' /> </p>
<p> Category : <input type="text" name="imageCategory"> </p>
<input type="submit" value="Send files" />
</form>
User can select multiple files and upload them.
And at the server you will do this :
if (isset($_FILES["userfile"]) && !empty($_FILES["userfile"])) {
$image = $_FILES['userfile']['tmp_name'];
$imageName = $_FILES['userfile']['name'];
$imageSize = $_FILES['userfile']['size'];
$imageType = $_FILES['userfile']['type'];
$imageCategory = $_POST['imageCategory'];
$len = count($image);
$path = "images/";
for ($i = 0; $i < $len; $i++) {
if (isset($imageName[$i]) && $imageName[$i] !== NULL) {
if(move_uploaded_file($image[$i], $path.$imageName[$i])) {
$result = $mysqli->query("INSERT INTO imageTable (imageName, imageCategory, imageSize, imageType) VALUES ('$imageName[$i]', '$imageCategory', '$imageSize[$i]' , '$imageType[$i]' )");
}
}
}
}
$mysqli->close();
header("location: index");
First off the file information won't be in the $_POST global variable it will be in the $_FILES
From http://php.net/manual/en/features.file-upload.multiple.php (modified):
Form
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<p> <input name="userfile[]" type="file" /> </p>
<p> <input name="userfile[]" type="file" /> </p>
<p> Category : <input type="text" name="imageCategory"> </p>
<input type="submit" value="Send files" />
</form>
Parse the global file variable to an array (we'll assume this is a helper function called parseFiles.php):
<?php
function reArrayFiles(&$file_post) {
$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for ($i=0; $i<$file_count; $i++) {
foreach ($file_keys as $key) {
$file_ary[$i][$key] = $file_post[$key][$i];
}
}
return $file_ary;
}
Move the files and insert the file information into the database :
<?php
include ("dbConnect.php");
include ("parseFiles.php");
if ($_FILES['upload']) {
$file_ary = reArrayFiles($_FILES['userfile']);
foreach ($file_ary as $file) {
$image = $file['tmp_name'];
$imageName = $file['name'];
$imageSize = $file['size'];
$imageType = $file['type'];
$imageCategory = $_POST['imageCategory'];
$result = $mysqli->query("INSERT INTO imageTable (imageName, imageCategory, imageSize, imageType)
VALUES ('$imageName', '$imageCategory', '$imageSize' , '$imageType' );")
or die(mysqli_error($mysqli));
}
} else {
echo "<p> It is not working </p>";
}
header("location: index");
$mysqli->close();
Hopefully that should do the job. I've not tested this I've just blind coded it so there may be some bugs

Categories