I have a form, How to insert in database one by one.For example :-
<form method="post">
<input type="file" name="img[]">
<input type="file" name="img[]">
<input type="file" name="img[]">
</form>
if(isset($_POST['submit'])){
if(count($_FILES['img']['name']) > 0){
//Loop through each file
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if($tmpFilePath != ""){
//save the filename
$shortname = $_FILES['upload']['name'][$i];
//save the url and the file
$filePath = "uploaded/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $filePath)) {
$files[] = $shortname;
//insert into db
//use $shortname for the filename
//use $filePath for the relative url to the file
}
}
}
}
this may help you
Related
<form name="property" action="" method="post" enctype="multipart/form-data">
Select Image
<input type="file" name="upload[]" multiple="multiple" id="image"/>
<input type="submit" value="Submit" name="submit" style="width:200px; height:30px;" />
</form>
MY Php code:
if(isset($_POST['submit']))
{
$total = count($_FILES['upload']['name']);
for($i=0; $i<$total; $i++)
{
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
if ($tmpFilePath != "")
{
$newFilePath[] = "upload/" . $_FILES['upload']['name'][$i];
$values_insert = implode(',', $newFilePath);
if(move_uploaded_file($tmpFilePath, $newFilePath))
{
$date=date("Y-m-d");
}
}
}
$d=mysql_query("INSERT INTO properties (iname,cdate) VALUES('".$values_insert."',NOW())") or die(mysql_error());
}
Note:
In database the path upload directory folder and image image is going to database, I want only image name separated by comma. for example the expected output is Chrysanthemum.jpg,Desert.jpg. I don't know what is the problem. please guide me.kindly look at my code and image attachment details.
Thing this'll do fyi - indenting code would help people read and help you.
if(isset($_POST['submit']))
{
$total = count($_FILES['upload']['name']);
for($i=0; $i<$total; $i++)
{
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
if ($tmpFilePath != "")
{
$newFilePath = "upload/" . $_FILES['upload']['name'][$i];
$newFilePath2[] = $_FILES['upload']['name'][$i];
$values_insert = implode(',', $newFilePath2);
if(move_uploaded_file($tmpFilePath, $newFilePath))
{
$date=date("Y-m-d");
}
}
}
$d=mysql_query("INSERT INTO properties (iname,cdate) VALUES('".$values_insert."',NOW())") or die(mysql_error());
}
This is my html code:
<form action="producttoevoegen.php" method="post" enctype="multipart/form-data" name="FileUploadForm" id="FileUploadForm">
<label for="Upload"></label>
<input type="file" name="Upload[]" multiple="multiple" id="Upload" />
<input type="submit" name="UploadButton" id="UploadButton" value="Upload" />
</form>
This is my php code:
<?php
if(isset($_FILES['Upload'])){
$UploadName = $_FILES['Upload']['name'];
$UploadType = $_FILES['Upload']['type'];
$FileSize = $_FILES['Upload']['size'];
$UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName);
if(($FileSize > 125000)){
die("Error - File too Big");
}
for($i=0; $i<count($UploadName); $i++) {
$tmpFilePath = $_FILES['Upload']['tmp_name'][$i];
if ($tmpFilePath != ""){
$newFilePath = /upload/" . $UploadName[$i];
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
}
}
}
}
When I try to upload files it doesn't work. It doesn't show in the map. I've tried a lot of things but nothing worked. Does anyone see the mistake I've made? Thanks in advance!!
if(isset($_FILES['Upload'])){
for($i=0; $i<count($UploadName); $i++) {
$UploadName = $_FILES['Upload']['name'][$i];
$UploadType = $_FILES['Upload']['type'][$i];
$FileSize = $_FILES['Upload']['size'][$i];
$UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName);
if(($FileSize > 125000)){
die("Error - File too Big");
}
$tmpFilePath = $_FILES['Upload']['tmp_name'][$i];
if ($tmpFilePath != "") {
$newFilePath = "/upload/" . $UploadName[$i];
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
}
}
}
}
Please find updated code
<form action="producttoevoegen.php" method="post" enctype="multipart/form-data" name="FileUploadForm" id="FileUploadForm">
<label for="Upload"></label>
<input type="file" name="Upload" multiple="multiple" id="Upload" />
<input type="submit" name="UploadButton" id="UploadButton" value="Upload" />
</form>
Use this code for PHP script which is given by #Minesh Patel
if(isset($_FILES['Upload'])){
for($i=0; $i<count($UploadName); $i++) {
$UploadName = $_FILES['Upload']['name'][$i];
$UploadType = $_FILES['Upload']['type'][$i];
$FileSize = $_FILES['Upload']['size'][$i];
$UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName);
if(($FileSize > 125000)){
die("Error - File too Big");
}
$tmpFilePath = $_FILES['Upload']['tmp_name'][$i];
if ($tmpFilePath != "") {
$newFilePath = "/upload/" . $UploadName[$i];
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
}
}
}
}
please help me with my PHP upload problem:
My code:
<?php
if(isset($_POST['uploadsubmit'])) {
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "./images/" . $_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
//Handle other code here
}
}
}
}
?>
<form action="#" method="post" name="uploadimg">
<input type="file" name="upload[]" /><br />
<input type="file" name="upload[]" /><br /><br />
<input type="submit" name="uploadsubmit" value="Submit">
</form>
and my problem is:
Notice: Undefined index: upload in
C:\web\php_structure\type\article.php on line 71
line 71:
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
and upload is not executed.
Upload in php.ini is On
Thanks...
I'm new in PHP area. This is my try to upload a file:
<?php
if(isset($_FILES['file'])) {
$file = $_FILES['file'];
if($file['error'] === 0) {
$distination = 'uploads/';
$file_ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$filename = $distination . uniqid('', true) . '.' . $file_ext;
if(!move_uploaded_file($file['name'], $filename)) {
echo "File upload failed!";
}
}
}
?>
<form action="testupload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
the return value from the move_uploaded_file always false. I create the uploads folder in the same directory as the upload script file.
Your main problem is fact that you use name instead of tmp_name value from $_FILES array.
name is original name of uploaded file, tmp_name is location of file after upload. Manual Page
Fixed version below. I also added check for destination directory and automatic creation of it if not available.
<?php
if(isset($_FILES['file'])) {
$file = $_FILES['file'];
if($file['error'] === 0) {
$distination = 'uploads/';
if(!file_exists($distination)){
mkdir($distination, 0777, true);
}
$file_ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$filename = $distination . uniqid('', true) . '.' . $file_ext;
print_r($file);
if(!move_uploaded_file($file['tmp_name'], $filename)) {
echo "File upload failed!";
}
}
}
?>
<form action="testupload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
My code in here. how can I upload more then 1 image using 1 input field?
<?php
if (isset($_POST['beopen_form'])) {
if ($_FILES["file1"]["error"] > 0) {
echo "Error: " . $_FILES["file1"]["error"] . "<br>";
} else {
$x = mysql_insert_id();
$path_array = wp_upload_dir();
$old_name = $_FILES["file1"]["name"];
$split_name = explode('.', $old_name);
$time = time();
$file_name = $time . "." . $split_name[1];
$tmp_name = $_FILES["file1"]["tmp_name"];
move_uploaded_file($_FILES["file"]["tmp_name"], $path . "/" . $old_name);
$path2 = $path . "/" . $old_name;
mysql_query("INSERT INTO carimage (image,carid,created,updated) VALUES ('$path2','$x','$created','$updated') ON DUPLICATE KEY UPDATE image='$path2',description='$makeid',updated='$updated'");
}
}
?>
<form enctype="multipart/form-data" class="beopen-contact-form" id="signupForm" novalidate="novalidate" action="<?php echo get_permalink(); ?>" method="post">
<input type="file" name="file" id="file">
<div id="recaptcha_div"></div><br>
<input type="hidden" name="beopen_form" value="1" /><!-- button -->
<button class="button send-message" type="submit">
<span class="send-message"></span><?php _e('Update', 'beopen');?>
</button>
</form>
html :-
<input type="file" name="file" id="file" multiple>
PHP CODE:-
//Loop through each file
for($i=0; $i<count($_FILES['file']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['file']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "./uploadFiles/" . $_FILES['file']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
//Handle other code here
}
}
}
I TAKE THIS CODE FROM :-
Multiple file upload in php
Before Post any question you should search for problem....!!!