upload multiple images to server at one input file in php - php

I'm trying to upload multiple images to server at one input(click on the button), and I have following code for index.html:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="contact" action="upload.php" method="post" enctype="multipart/form-data" >
<!-- image 1 -->
<label class="aa">
Upload an image
</label>
<div>
<input name="Field26" type="file" size="12" data-file-max-size="10" tabindex="1" required />
</div><br>
<!-- image 2 -->
<label class="bb">
Upload an image
</label>
<div>
<input name="Field33" type="file" size="12" data-file-max-size="10" tabindex="2" required />
</div>
</br>
<button name="submit" type="submit" value="Upload Image" data-submit="...Sending">Upload</button>
</form>
</div>
</body>
</html>
I have following upload.php code for uploading one image:
<?php
$dirCheck = 'images/$_POST[project]';
if (file_exists($dirCheck)){
echo "Sorry, directory already exists.";
$uploadOk = 0;
}else{
mkdir("images/$_POST[project]",0777,true);
echo "The new folder $_POST[project] has been created.</br>";
$uploadOk = 1;
}
$target_dir = "images/$_POST[project]/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ". ";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
How to change the code to upload multiple images? Thanks!

you have to use an array in name attribute instead:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileupload[]" /><br><br>
<input type="file" name="fileupload[]" /><br><br>
<input type="file" name="fileupload[]" /><br><br>
<input type="submit" name="submit" value="SAVE"/>
</form>
Or you can use multiple attribute:
<input type="file" name="fileupload[]" multiple />
Then in PHP you use this:
foreach($_FILES['fileupload'] as $file) {
....
}

Related

PHP upload file web form

i'm trying to create a form that lets the visitor upload image files. I have been using the code below but keep receiving the "not set" error as if $_FILES['image'] isn't picking up the image file.
Can anyone see any errors here?
Form code:
<h1>Contact form test</h1>
<form action="php/form.php" method="post">
First name:<br>
<input type="text" name="firstname" required><br>
Last name:<br>
<input type="text" name="lastname" required><br>
<p>
<input type="file" name="image" id="image" enctype="multipart/form-data"><br>
</p>
<input type="submit" value="Submit">
</form>
php code:
// Set variables
$firstname = secure($_POST['firstname']);
$lastname = secure($_POST['lastname']);
// Form Security
function secure($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// File upload
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
$file_type = $_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be exactly 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}else{
echo "not set";
}
use enctype="multipart/form-data" in form tag
<form action="php/form.php" method="post" enctype="multipart/form-data">
First name:<br>
<input type="text" name="firstname" required><br>
Last name:<br>
<input type="text" name="lastname" required><br>
<p>
<input type="file" name="image" id="image" enctype="multipart/form-data">
<br>
</p>
<input type="submit" value="Submit">
</form>
Reference : http://php.net/manual/en/features.file-upload.post-method.php
put enctype="multipart/form-data" in form tag
Just adds a multipart attribute for form tag, which is necessary if you would like to use the form to upload files with. The enctype attribute specifies how the form-data should be encoded when submitting it to the server.
<form action="demo_post_enctype.asp" method="post" enctype="multipart/form-data">
//you code here
</form>
Also ,the enctype attribute can be used only if method="post".

How to upload images to the disk drive of server?

I am making a Tuition Teacher finding website.I have made a sign up form. I would like to add a profile picture option to the form.
Here is the code for it,
<input type="file" name="fileToUpload" id="fileToUpload">
How do I store the image in the disk drive of the server computer?
use php move_uploaded_file
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>
move_uploaded_file(file,newloc)
file:- Specifies the file to be moved
newloc:- Specifies the new location for the file
First of all, your form should have attribute enctype="multipart/form-data". You can also use copy(file,to_file).
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
if (strlen($_FILES['fileToUpload']['tmp_name']) > 0) {
$path="path/to/new-location/IMAGENAME-With-Extension";
copy($_FILES['fileToUpload']['tmp_name'],$path);
}

File doesn't get uploaded in folder php

I need to upload a file in a folder but the file never get in the folder. The name of the file is added correctly but no file in the folder. What is going wrong?
Php error: Undefined index: foto on line 12-16
$name= $_FILES["foto"]["name"];
$type= $_FILES["foto"]["type"];
$size= $_FILES["foto"]["size"];
$temp= $_FILES["foto"]["temp_name"];
$error= $_FILES["foto"]["error"];
if ($error > 0)
die("Error uploading file! code $error.");
else
{
if($type=="image/png" || $size > 2000000)//condition for the file
{
die("Format not allowed or file size too big!");
}
else
{
move_uploaded_file($temp,"assets/");
echo "Upload complete!";
}
}
HTML:
<form id="form" style="margin-left: 200px" action="addNewProduct.php" method="post"><br>
<br>
<br>
<div id="imageUpload">
<label for="foto">Foto</label>
<input type="file" name="foto" /><br>
</div>
<div id="infoForm">
<label for="productNaam">Productnaam</label>
<input type="text" name="productNaam"/><br>
<br>
<label for="beschrijving">Productbeschrijving</label>
<input type="text" name="productBeschrijving" /><br>
<br>
<label for="btw">BTW</label>
<input type="number" name="productBtw" /><br>
<br>
<label for="prijsinclbtw">PrijsInclBTW</label>
<input type="number" name="productPrijsInclBtw" /><br>
<br>
<br>
<br>
<input type="submit" name="submit" value="Add new Pen">
</div>
</form>
Your $_FILES["foto"]["temp_name"]; is incorrect, you should change it to $_FILES["foto"]["tmp_name"];
And your folder assets should exist in your root folder.
Your form should like this:
<form action="uploads.php" method="post" enctype="multipart/form-data">
<input type="file" name="foto">
<input type="submit" name="submit">
</form>
Please try this, hope this help you out : uploads.php
$type= $_FILES["foto"]["type"];
$size= $_FILES["foto"]["size"];
// $temp= $_FILES["foto"]["temp_name"];
$file_name = $_FILES["foto"]["name"];
$source = $_FILES['foto']['tmp_name'];
$dir = "./assets/";
$file = $dir . $file_name;
//$directory = "./assets/upload/$file_name";
if(!file_exists ($file ))
{
move_uploaded_file($source,$file );
exit();
}
else
{
echo "File exist";
}
When you upload files, form has to have enctype attribute.
<form enctype="multipart/form-data" id="form" style="margin-left: 200px" action="addNewProduct.php" method="post">
^^

warning in mkdir and how to display images from the directory created

My program is supposed to create a folder for the uploaded images on the directory but gives this warning:
mkdir() [function.mkdir]: File exists in C:\XAMP\xampp\htdocs\gallery\uploader3.php on line 26
Here is the code:
<html>
<head>
<title> Sample1 - File Upload on Directory </title>
</head>
<body>
<div align="center">
<form action="uploader3.php" method="post" enctype="multipart/form-data" >
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Create an Album (limited to 10 images): <br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<br />
<input type="submit" value="Upload File" />
</form>
</div>
<?php
$target_path = "uploads1/";
if(!mkdir($target_path))
{
die('Failed to create folders...');
}
else
{
for($count = 0; $count < count($_FILES['uploadedfile']); $count++)
{
$target_path = $target_path . basename( $_FILES['uploadedfile']['name'][$count]);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$count], $target_path))
{
echo "The file ". basename( $_FILES['uploadedfile']['name'][$count]).
" has been uploaded";
}
else{
echo "There was an error uploading the file, please try again!";
}
}
}
?>
</body>
</html>
Modify your codes below:
if(!mkdir($target_path))
{
die('Failed to create folders...');
}
to:
if(!file_exist($target_path)) {
if(!mkdir($target_path))
{
die('Failed to create folders...');
}
}
That will check the folder first, if it's already exist, no need to create it again.
for your 2nd question, you need to store the uploaded image names to somewhere ( i guess DB is a good choice), Then, you can show them anywhere you want.
Or you can use below codes to search in folder and display them:
$image_files = glob("uploads1/*.jpg");
foreach($image_files as $img) {
echo "<img src='".$img."' /><br/>";
}
You should check first that the directory does not already exist before attempting to create it
if (!file_exists($target_path))
mkdir($target_path);
if (file_exists($target_path))
{
// Further processing here
}
else
{
// Could not create directory
}

PHP file upload count set file

I want to count set file upload. Here is my using code. Are there any better method to do this. Thanks.
<form action="index.php" method="post" enctype="multipart/form-data">
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<button name="submit" type="submit">Upload</button>
</form>
<?php
$img_error = '0';
$fill_img_count = '0';
if(isset($_POST['submit']))
{
$img_count = count($_FILES['new_image']);
echo "Total : ".$img_count."<br>";
for ($i=0 ; $i<=$img_count ; $i++)
{
if (isset($_FILES['new_image']) && !empty($_FILES['new_image']['name'][$i]))
{
$fill_img_count++;
}
}
echo "Set : ".$fill_img_count."<br>";
}
?>
$count_files = 0;
foreach ($_FILES['picture']['error'] as $item) {
if ($item != 4) {
$count_files++;
}
}
echo $count_files;
I'd recommend testing each ['error'] key against UPLOAD_ERR_OK.
You don't require to have name="new_image[]" as the name ... just new_image will suffice. If you post 1 or many, on the PHP side, you'll see $_FILES[]
Some useful links for you:
http://php.net/manual/en/reserved.variables.files.php
http://www.php.net/manual/en/features.file-upload.php
Some code:
if (empty($_FILES)) { echo "0 files uploaded"; }
else { echo count($_FILES) . " files uploaded"; }
Edit based on comment:
How to handle multiple file upload using PHP
From that post:
echo count($_FILES['file']['tmp_name']);
<?php
$count = 0;
foreach($_FILES['new_image']['error'] as $status){
if($status === UPLOAD_ERR_OK) {
$count++;
}
}
var_dump($count);
?>
<form action="test.php" method="post" enctype="multipart/form-data">
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<button name="submit" type="submit">Upload</button>
</form>

Categories