Upload multiple image files and display on the next page. PHP - php

I am attempting to upload multiple images at once, and then on submit display those images on the page. This is going to be used with mPDF. I am using the examples in the manual at http://mpdf1.com/manual/index.php?tid=467
It has a text box and 1 image uploader, and displays what ever was in the text box and the image on the next page. How can I convert this to use multiple images?
Page 1:
<?php
$html = '
<html>
<body>
<form action="example_userinput2.php" method="post" enctype="multipart/form-data">
Enter text:
<br />
<textarea name="text" id="text"></textarea>
<br />
<label for="file">Choose Image to upload:</label> <input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
';
echo $html;
exit;
?>
Page 2: (also more specifically what I change the areas I marked ** **, after allowing multiple images.)
<?php
if (($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/jpeg")
& $_FILES["file"]["size"] < 20000) {
// If the destination file already exists, it will be overwritten
move_uploaded_file($_FILES["file"]["tmp_name"], "../tmp/" . $_FILES["file"]["name"]);
}
else {
echo "Invalid file";
}
$html ='
<html>
<body>
<div>'.$_POST['text'].'</div>
**<img src="' ."../tmp/" . $_FILES["file"]["name"].'" />**
<form action="example_userinput3.php" method="post" enctype="multipart/form-data">
<textarea style="display:none" name="text" id="text">'.$_POST['text'].'</textarea>
**<input type="hidden" name="filename" id="filename" value="'. $_FILES["file"]**["name"].'" />
<input type="submit" name="submit" value="Create PDF file" />
</form>
</body>
</html>
';
echo $html;
exit;
?>
Page 3 goes to the mPDF generator so I can convert this to PDF for another project I have in mind.
Any help would be awesome.

From php manual, to find here: http://php.net/manual/en/features.file-upload.multiple.php
<form action="example_userinput2.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input type="submit" value="Send files" />
</form>
On page2, you can continue with a loop and handle those files at once:
foreach ($_FILES['array_of_files'] as $position => $file) {
// should output array with indices name, type, tmp_name, error, size
var_dump($file);
}
You can do the same as with one file in the loop

You can have multiple <input type="file"> html elements set up on your page set up this way:
<input type="file" name="file[0]" />
<input type="file" name="file[1]" />
etc.
And then in PHP loop through them:
foreach($_FILES['file'] as $file){
//refer to everything as $file instead of $_FILES['file']
}
That should be enough to get you started.

Related

php uploading file (powerpoint)

I'm using these codes to upload images and then I tried it to upload CSV files. These codes worked for both uploading images and CSV files, but it won't work in uploading powerpoint files. What am I missing here?
<?php if (isset($_POST["calendarformat"])){
$calendarfilename = $_POST['calendarfilename'];
$calendarfile = $_FILES['calendarfile']['name'];
$calendarlocation = "calendar/".$calendarfile;
move_uploaded_file($_FILES['calendarfile']['tmp_name'],$calendarlocation);
$quer_calendar = "INSERT into calendar (name,format,path) values ('$calendarfilename','$calendarfile','$calendarlocation')";
$quer1_calendar = mysqli_query($con,$quer_calendar);
if ($quer1_calendar==true)
{
echo "<script>alert('Upload Success');</script>";
}
else {
echo "<script>alert('Upload Failed');</script>";
} }?>
This is the html form:
<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<h4><input type="file" name="calendarfile"/></h4>
<h4><input type="text" name="calendarfilename" placeholder="File Name"/></h4>
<button type="submit" name="calendarformat" class="btn">Upload Calendar</button>
</form>
Try add this to your form:
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<h4><input type="file" name="calendarfile"/></h4>
<h4><input type="text" name="calendarfilename" placeholder="File Name"/></h4>
<button type="submit" name="calendarformat" class="btn">Upload Calendar</button>
</form>
This is my form, I already added the <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
The PPT file is already saved into my database, but it doesn't went to the location folder which is the calendar folder.

How to check size of a file in PHP

I am trying to check the size of my file using echo $_FILES['photo']['size'] but nothing is being returned.
HTML
<form method="POST" action="sendToDatabase.php">
<fieldset>
<legend>Artwork</legend>
<label>Album Artwork</label>
<input type="file" name="art" />
<p>
File must be saved as a .jpg file.<br />Please crop to 150px wide X 200px tall before uploading.
</p>
</fieldset>
<input type="submit" name="submit" value="Submit Album" class="submitBtn" />
</form>
PHP
<?php echo $_FILES['art']['size']; ?>
you need to add enctype="multipart/form-data" into form tag
enctype="multipart/form-data"
<form method="POST" action="sendToDatabase.php" enctype="multipart/form-data" >

Upload files with multiple inputs

I have a form where i want to upload files with multiple inputs.My form looks like:
<form action="" method="post">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
I do not know how to process this form..
You form doesn't work until you don't include 'enctype="multipart/form-data"', because it is necessary to use input type file.
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
Now browse the file and submit the form. You will get all file data inside $_FILES. so to check what you get inside the file data, you can use :
echo '<pre>';
print_r($_FILES)
I'm not sure whether you have gone through the tutorials before, however below is the code which will help you to process it.
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
If you upload the file, you can get the files from,
$_FILES global array, i.e. $_FILES['tax'] and $_FILES['ta'].
More info can be found on php.net
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
<?php
//print_r($_POST);
if(isset($_POST['submit'])){
$name = $_FILES['tax']['name'];
$name1 = $_FILES['ta']['name'];
$temp_name = $_FILES['tax']['tmp_name'];
$temp_name1 = $_FILES['ta']['tmp_name'];
var_dump($_FILES);
if(isset($name)){
if(!empty($name)){
var_dump($_FILES);
$location = 'images/'.$name;
if(move_uploaded_file($temp_name, $location)){
echo 'File uploaded successfully';
}
}
} else {
echo 'You should select a file to upload !!';
}
if(isset($name1)){
if(!empty($name1)){
var_dump($_FILES);
$location = 'images/'.$name1;
if(move_uploaded_file($temp_name1, $location)){
echo 'File uploaded successfully';
}
}
} else {
echo 'You should select a file to upload !!';
}
}
?>

Multi image upload

I'm learning PHP and can not find a way to make this work. Did I write correct code to upload multiple images?
I can not think of a reason why this should be wrong.
$imageName = mysql_real_escape_string($_FILES["image, drie1, drie2, drie3, add, strip"]["name"]);
$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
$imageType = mysql_real_escape_string($_FILES["image, drie1, drie2, drie3, add, strip"]["type"]);
HTML
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input type="submit" value="Send files" />
</form>
PHP (file-upload.php)
var_dump($_FILES);
This will display the info of the uploaded files
You can add accept attribute to the input to limit the allowed filetypes by extention
Html
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="image_file[]" multiple=""> /* multiple tag is used to upload multiple files */
<input type="submit" name="Submit" value="Submit" />
</form>
Php
<?php
foreach($_FILES["image_file"]["name"] as $key => $value)
{
$name = $value;
$tmp_name = $_FILES["image_file"]["tmp_name"][$key];
$type = $_FILES["image_file"]["type"][$key];
echo $name." , ".$tmp_name." , ".$type."\n";
}
?>

var_dump only shows 1 array when upload more than 1 file

i have a script that uploads images and then crops them. but when i use
<input type="file" name="filename" multiple="multiple"/><br />
and then do a var_dump i only get 1 array instead of 'for example' 10 images.
My uploadform:
<form action="uploaded.php" method="POST" enctype="multipart/form-data">
<hr>
<input type="file" name="filename" multiple="multiple"/><br />
<br /><br />
<hr width="60" align="left">
<input type="submit" value="Upload" />
</form>
My code to see the uploades files:
<?php
session_start();
echo "<pre>";
var_dump($_FILES);
die();
?>
when i upload 10 images. it only shows the first selected and prints out 1 array.
Please help!
Thanks
EDIT: i think that i have to use a session for this. but how do i do that?
Same as any other form element used more then once: <input type="file" name="filename[]" multiple="multiple"/>

Categories