PHP: Upload more files - php

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

Related

How to upload multiple image with array?

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

Could not find the file and upload

I trying to do multiple image upload to the system but it won't able to found the images after upload.
When i try to var_dump the total number of item uploaded it keep showing 0 item uploaded.
Need some help here, Thank You.
This is my HTML code:
<form id="login-form" action="" method="post" enctype="multipart/form-data" >
<input type="file" name="file[]" multiple="multiple" />
<input type="submit" name="submit-scam" value="Submit">
</form>
This is my PHP code:
<?php
//Check Error
error_reporting(E_ALL);
//Start Session
session_start();
//Include Database
include_once('dbConnect.php');
if (isset($_POST['submit-scam'])){
echo "button clicked";
if(isset($_FILES['file'])){
/*This Function is to loop multiple upload file into DB*/
$total = count($_FILES['file']['name']);
for ($i=0; $i<$total; $i++) {
$file = rand(1000,100000)."-".$_FILES['file']['name'][$i];
$file_loc = $_FILES['file']['tmp_name'][$i];
$file_size = $_FILES['file']['size'][$i];
$file_type = $_FILES['file']['type'][$i];
$folder="uploads/";
$today = date('Y-m-d');
// new file size in KB
$new_size = $file_size/1024;
// new file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
//To Insert Latest base to SQL Latest submit ec_claim id
$sql="INSERT INTO db_evidence(db_evidence_name,db_evidence_type,db_evidence_scam_id,db_evidence_users_id)
VALUES('$final_file','$file_type','1','1')";
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
echo "success upload";
// header("Location:index.php");
}
else
{
echo "failed to upload";
}
}
} else {
echo "error no file is found";
}
}
?>
Add enctype="multipart/form-data" to the form
<form id="login-form" action="" method="post" enctype="multipart/form-data">
<input type="file" name="file[]" multiple="multiple" />
<input type="submit" name="submit-scam" value="Submit">
</form>
That should let you read from type="file"

Having issue in multiple image upload? db image attached

<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());
}

How to upload an array of files?

I'm trying to build an array of files by submitting a form several times, then move those files to a directory, but it's not working. Every uploads just overrides the previous and then it doesn't even move that one (the upload_to_file() function doesn't do anything)
HTML:
<form id="form" action="home.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="8000000">
<input class="upload_btn" type="file" name="images[]" id="image_file">
<input type="submit" id="img_submit" class="form_button" name="submit_image" value="upload"/>
</form>
It is important that there is only one upload button which can be used to upload many files.
I need them to be stored in an array so I can display their ['name'] anywhere with a loop.for ($i = 0; $i < count($_FILES['images']['name']); $i++){}
Then once another form is submitted it calls a function to move each file in the array to a directory.
Function inside an included php file:
function upload_to_file(){
$image_paths = array();
$target_dir = "uploads/images/";
$path = $target_dir . basename($_FILES['images']['name'][0]);
if(isset($_FILES['images']['name'][0]) && $_FILES['images']['size'][0] > 0)
{
if (move_uploaded_file($_FILES['images']['tmp_name'][0], $path)) {
$image_paths[0] = "uploads/images/";
}
}
return $image_paths;
}
I'm only testing it with the first element in the array, but will need to make a loop later.
Here is the program, which help you to upload multiple files. in the code "sub" is the submit button name. "upload" is the name of file controller, the uploaded files are stored in the directory called "img" that you have to create on your root folder.
<?php
if (isset($_POST['sub'])) {
if (count($_FILES['upload']['name']) > 0) {
for ($i=0; $i<count($_FILES['upload']['name']); $i++) {
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
if ($tmpFilePath != "") {
$shortname = $_FILES['upload']['name'][$i];
$filePath = "img/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];
if (move_uploaded_file($tmpFilePath, $filePath)) {
$files[] = $shortname;
}
}
}
}
echo "<h1>Uploaded:</h1>";
if(is_array($files)){
echo "<ul>";
foreach($files as $file){
echo "<li>$file</li>";
}
echo "</ul>";
}
}
?>
HTML Code is given
<form action="" enctype="multipart/form-data" method="post">
<input id='upload' name="upload[]" type="file" multiple="multiple" />
<input type="submit" name="sub" value="Upload Now">
</form>

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

Categories