post multiple image using 1 field in a form - php

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....!!!

Related

$_FILES[“image”][“tmp_name”] is empy with no data

I have written the following code. But I am getting empty $_FILES[“image”][“tmp_name”] with no value.
Php:
if ($_FILES) {
$imageFileType = strtolower(pathinfo("img/" . basename($_FILES["Image"]["name"]), PATHINFO_EXTENSION));
$target_file = "img/" . uniqid(rand()) . ".$imageFileType";
if (move_uploaded_file($_FILES["Image"]["tmp_name"], $target_file)) {
$pic = $target_file;
} else {
echo "Invalid File";
}
}
Html:
<form action="product-add.php" method="post" enctype="multipart/form-data">
<div class="products-form">
<p> Image:</p> <input type="file" name="Image" >
<input type="submit" value="ADD PRODUCT" class="btn">
</div>
</form>
Well it appears to me this is not right:
$target_file = "img/" . uniqid(rand()) . ".$imageFileType";
it should be
$target_file = "img/" . uniqid(rand()) . "." .$imageFileType;

File not uploading into the map

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)) {
}
}
}
}

move_uploaded_file wont work on my local machine

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>

Upload Multiple Files in PHP & INSERT path to MySQL

I've setup a form to upload multiple files from a PHP script and then insert it into the Database with path. Here's my code
<form action="" method="post" enctype="multipart/form-data">
<tr class='first'>
<td>Property Image : </td>
<td>
<input type="file" name="pic_upload[]" >
<input type="file" name="pic_upload[]" >
<input type="file" name="pic_upload[]" >
</td>
</tr>
<tr class='first'>
<td> </td><td><input type="submit" name="create" value="Add" /></td>
</tr>
</form>
<?php
if(isset($_POST['create'])) {
$path = "images/";
for ($i=0; $i<count($_FILES['pic_upload']['name']); $i++) {
$ext = explode('.', basename( $_FILES['pic_upload']['name'][$i]));
$path = $path . md5(uniqid()) . "." . $ext[count($ext)-1];
move_uploaded_file($_FILES['pic_upload']['tmp_name'][$i], $path);
}
$sql = "INSERT INTO post (`image`) VALUES ('$path');";
$res = mysqli_query($con,$sql) or die("<p>Query Error".mysqli_error()."</p>");
echo "<p>Post Created $date</p>";
}
?>
The Script runs successfully, but at the database end inside the column it looks like this.
images/8de3581eb72ee7b39461df48ff16f4a3.jpg024fae942ae8c550a4bd1a9e028d4033.jpg380cc327df25bc490b83c779511c015b.jpg
Help me out with this please
Move $path = "images/"; inside the for loop. Otherwise you are appending to the filename without resetting it after each iteration. Actually, you don't need to use a variable for that prefix at all. You can simply write 'images/' . md5(uniqid()) . "." . $ext[count($ext)-1] immediately.
To write the values to the database, you can either run the query in each iteration as well or add the paths to an array which is transformed to the comma-separated insertion list according to the SQL syntax.
Here is what worked for me:everything contained in the for loop then just return the $fileDest
<?php
if(isset($_POST['submit'])){
$total = count($_FILES['files']['tmp_name']);
for($i=0;$i<$total;$i++){
$fileName = $_FILES['files']['name'][$i];
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
$newFileName = md5(uniqid());
$fileDest = 'filesUploaded/'.$newFileName.'.'.$ext;
if($ext === 'pdf' || 'jpeg' || 'JPG'){
move_uploaded_file($_FILES['files']['tmp_name'][$i], $fileDest);
}else{
echo 'Pdfs and jpegs only please';
}
}
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form class="" action="test.php" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple>
<button type="submit" name="submit">Upload</button>
</form>
</body>
</html>
$files = array_filter($_FILES['lamp']['name']);
$total = count($files);
$path = "./asset/save_location/";
// looping for save file to local folder
for( $i=0 ; $i < $total ; $i++ ) {
$ext = explode('.', basename( $_FILES['lamp']['name'][$i]));
$tmpFilePath = $_FILES['lamp']['tmp_name'][$i];
if ($tmpFilePath != ""){
$newFilePath = $path .date('md').$i.".".$ext[count($ext)-1]; //save lampiran ke folder $path dengan format no_surat_tgl_bulan_nourut
// upload success
if(move_uploaded_file($tmpFilePath,$newFilePath)) {
$success="file uploaded";
}
}
} //end for
//looping for save namefile to database
for ($i=0; $i<count($_FILES['lamp']['name']); $i++) {
$ext = explode('.', basename( $_FILES['lamp']['name'][$i]));
$path = $path .date('md') .$i. "." . $ext[count($ext)-1].";";
$save_dir=substr($path, 22,-1);
}//end for
var_dump($save_dir);die();

Multiple Upload using CTRL Key PHP

would you help for my code , i need to do the multiple upload but i cant so will you help me please. i need so bad.
here's my code
i made multiple upload the form but it is not working. the output is "error array"
//HTML
<html>
<head>
<form name="Image" enctype="multipart/form-data" action="upload.php" method="POST">
<h1><font face="tahoma"> UPLOAD FILES</h1>
<label for="file">Filename:</label>
<input type="file" name="Photo[]" accept="image/*" multiple="multiple"/><br/><br/>
<input type="hidden" id="pageName" name="pageName">
<script type="text/javascript">
//get page name from parent
var value = window.opener.pageName
document.getElementById("pageName").value = value;
</script>
<INPUT type="submit" class="button" name="Submit" value=" Upload ">
<INPUT type="reset" class="button" value="Cancel"><br/><br/>
</form>
</head>
</html>
//PHP this is were upload is do.
<?php
include('global.php');
?>
<?
$uploadDir = 'directory/'; //Image Upload Folder
if(isset($_POST['Submit']))
{
$fileName = $_FILES['Photo']['name'][0];
$fileName1 = $_FILES['Photo']['name'][1];
$tmpName = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];
$filePath = $uploadDir . $fileName . $fileName1;
//upload error
if ($_FILES["Photo"]["error"] > 0)
{
echo "Error: " . $_FILES["Photo"]["error"] . "<br />";
}
//photo already exixts
else
//insert image into DB
{
move_uploaded_file($tmpName, $filePath);
$filePath = addslashes($filePath);
$filePath = stripslashes($filePath);
$filePath = mysql_real_escape_string($filePath);
$query = "INSERT INTO images (image , category ) VALUES ('$filePath', '$pageName')";
mysql_query($query) or die('Error, query failed');
echo" Upload Successful. <br/> <br/>";
echo "Stored in: " . "directory/" . $_FILES["Photo"]["name"];
?>
<br/><br/>
<img width="300" height="400" src="directory /<?=$_FILES["Photo"]["name"]?>"><br/>
<?
}
}
?>
Error: Array is telling you that the error being returned is actually an Array object, not a string. If you want to see the actual error message, you need to view the full contents of the array. Something like print_r($_FILES["Photo"]["error"]); or looping through the array like so
foreach($_FILES["Photo"]["error"] as $err) {
echo "error: " . $err . "<br>";
}
Or you can just print the first error returned just as you have returned the first file in your name array echo $_FILES["Photo"]["error"][0];

Categories