Looking for the following solutions:
Form html / php provide the ability to add multiple graphic files (up to 11 files, max 5MB).
When sending to a server script should do the following:
check if the file has a good extension (jpg, png);
re-name each file according to the formula < string up to 32 characters > _ < 000 to 010 >. < extension >;
add file names to the table (one file = one column, row);
compress to a maximum resolution of 1280x1024;
change the proportions of graphics on 4: 3 images while maintaining the same proportions;
lessen the size of the file and save to the server in the appropriate folder.
I'm interested in possibly easy to use solution.Thank you very much for your help.
try it,
HTML Code
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Ppload with PHP</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
<input type="submit" value="Upload!" />
</form>
</body>
</html>
PHP CODE
<?php
$valid_formats = array("jpg", "png");
$max_file_size = 1280*1024;
$path = "uploads/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
$count++; // Number of successfully uploaded file
}
}
}
}
?>
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
<?php include 'connection.php';?>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Upload with PHP</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
<input type="submit" value="Upload!" />
</form>
</body>
</html>
<?php
$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*1000; //100 kb
$path = "uploads/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
$ext = pathinfo($_FILES['files']['name'][$f], PATHINFO_EXTENSION);
$uniq_name = uniqid() . '.' .$ext;
move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path . $uniq_name);
// $count++; // Number of successfully uploaded file
}
}
}
?>
You have to add your insert query inside the else statement when file successfully uploaded
I write a code to upload a image in specific location and display a number of uploaded image count. It will work perfectly when I select below 8 images. But this same code does not worked when if I select 10 to 20 images. I really don't have any idea why it would work when selecting minimum number of images and not working if I select a larger number of images. Please find my below code
storeimage.php
<?php
require_once 'pdoconnectionusingclass.php';
?>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Ppload with PHP</title>
</head>
<body>
<form action="storeimagename.php" method="POST" enctype="multipart/form-data">
<div>
<select name="moviename">
<option value = "">---Select---</option>
<?php
try
{
$dbobj=new database();
$dbobj->openconnection();
$sql='select * from tbl_movie';
$query=$dbobj->getdata($sql);
if(isset($query))
{
foreach ($query as $row)
{
echo '<option value='.str_replace(' ','_', $row['movie_name']).'>'.$row['movie_name'].'</option>';
}
}
$dbobj->closeconnection();
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
</div>
<br/>
<div>
<input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
<input type="submit" value="Upload!" />
</div>
</form>
</body>
</html>
storeimagename.php
<?php
require_once 'pdoconnectionusingclass.php';
$valid_formats = array("jpg", "png", "gif", "bmp");
$max_file_size = 1048576 *10; //100 kb
$path = "uploads/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
$total_image=count($_FILES['files']['name']);
$dbobj=new database();
$dbobj->openconnection();
for($i=0;$i<$total_image;$i++)
{
foreach ($_FILES['files']['name'] as $i => $name) {
if ($_FILES['files']['error'][$i] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$i] == 0) {
if ($_FILES['files']['size'][$i] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
// echo $_FILES['files']['tmp_name'][$i];
// echo $_FILES['files']['name'][$i];
if(move_uploaded_file($_FILES["files"]["tmp_name"][$i], $path.$name))
{
$count++; // Number of successfully uploaded file
$gallery.=','.$name;
}
}
}
}
}
if(count==18)
{
$sql='insert into tbl_movie_gallery values((select movie_id from tbl_movie where movie_name='.str_replace('_',' ',$_POST['moviename']).')'.$gallery.');';
$dbobj->insertdata($sql);
}
//unset($_FILES['files']);
$dbobj->closeconnection();
}
?>
Program is terminated when executing this $total_image=count($_FILES['files']['name']); line if i select 10 to 20 files. $total_image displays count as 0. Can any one help what is the problem is here? Thanks in advance.
PHP has a setting for the maximum amount of files you can upload at once. The default is 20. You'll need to change this in your php.ini file.
The setting you're looking for is max_file_uploads
More information
I want to upload multiple files with predefined file name, is it possible? how to retrieve the name from php?
How to retrieve the name 'doc1' and 'doc2' from the php?
My code:
<?
$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*1024; //1MB
$path = "uploads/doc/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
$count++; // Number of successfully uploaded file
}
}
}
}
?>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Ppload with PHP</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
Doc1: <input type="file" id="file" name="files['doc1']" multiple="multiple" accept="image/*" /><br>
Doc2: <input type="file" id="file" name="files['doc2']" multiple="multiple" accept="image/*" /><br>
<input type="submit" value="Upload!" />
</form>
</body>
</html>
Do check php code below that will return all details of files you uploaded.
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
echo "File Names:<br>Doc1= ".$_FILES['files']['name']["'doc1'"];
echo "<br>Doc2= ".$_FILES['files']['name']["'doc2'"];
echo "<br><br>";
echo "File Types:<br>Doc1= ".$_FILES['files']['type']["'doc1'"];
echo "<br>Doc2= ".$_FILES['files']['type']["'doc2'"];
echo "<br><br>";
echo "Tmp Name:<br>Doc1= ".$_FILES['files']['tmp_name']["'doc1'"];
echo "<br>Doc2= ".$_FILES['files']['tmp_name']["'doc2'"];
echo "<br><br>";
echo "Error:<br>Doc1= ".$_FILES['files']['error']["'doc1'"];
echo "<br>Doc2= ".$_FILES['files']['error']["'doc2'"];
echo "<br><br>";
echo "Size:<br>Doc1= ".$_FILES['files']['size']["'doc1'"];
echo "<br>Doc2= ".$_FILES['files']['size']["'doc2'"];
echo "<br><br>";
}
?>
I'm having issues uploading zip files and can't seem to find an answer.
Index.php
<form id="convertFile" action="convert.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input name="upload" type="file" id="inputFile">
</div>
<div class="form-group">
<button type="submit">Submit</button>
</div>
</form>
convert.php:
if(isset($_FILES)){
echo $_FILES['upload']['name'];
}else{
echo json_encode(array('status'=>'error'));
}
When I upload a zip file, I get: Notice: Undefined index: upload in C:\wamp\www\xmlconverter\convert.php on line 3
This is what chrome shows in the post header:
------WebKitFormBoundaryuFNy5dZtFj7olmD5
Content-Disposition: form-data; name="zip_file"; filename="123.zip"
Content-Type: application/x-zip-compressed
------WebKitFormBoundaryuFNy5dZtFj7olmD5--
This works on any other major file format, but can't get it to read the zip file. If I var_dump $_FILES or $_POST they are empty.
What am I missing? Why does all other files work but zip does not.
Thank you
using wamp and php 5.5.12
Where is your zip file type definition?
Anyways, let say this was the html form to upload zip files you'd have the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php if($message) echo "<p>$message</p>"; ?>
<form enctype="multipart/form-data" method="post" action="">
<label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
<br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>
On the server-side script which handles the post you'd have:
<?php
if($_FILES["zip_file"]["name"]) {
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];
$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
break;
}
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
$message = "The file you are trying to upload is not a .zip file. Please try again.";
}
$target_path = "/home/var/yoursite/httpdocs/".$filename; // change this to the correct site path
if(move_uploaded_file($source, $target_path)) {
//if you also wanted to extract the file after upload
//you can do the following
$zip = new ZipArchive();
$x = $zip->open($target_path);
if ($x === true) {
$zip->extractTo("/home/var/yoursite/httpdocs/"); // change this to the correct site path
$zip->close();
unlink($target_path);
}
$message = "Your .zip file was uploaded and unpacked.";
} else {
$message = "There was a problem with the upload. Please try again.";
}
}
?>
I used a script very similar to one posted by #unixmiah without issue on my cPanel based server. Worked great (and has for year now) but ran into issue of error "PHP, undefined index" when using locally with wamp.
Here is the mod that worked for me:
<?php
function rmdir_recursive($dir) {
foreach(scandir($dir) as $file) {
if ('.' === $file || '..' === $file) continue;
if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
else unlink("$dir/$file");
}
rmdir($dir);
}
if(!empty($_FILES)){
//added above to script and closing } at bottom
if($_FILES["zip_file"]["name"]) {
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];
$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
break;
}
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
$message = "<b>The file you are trying to upload is not a .zip file! Please try again...</b>";
}
$target_path = "somedir/somesubdir/".$filename; // change this to the correct site path
if(move_uploaded_file($source, $target_path)) {
$zip = new ZipArchive();
$x = $zip->open($target_path);
if ($x === true) {
$zip->extractTo("somedir/somesubdir/"); // change this to the correct site path
$zip->close();
unlink($target_path);
}
$message = "<h2>ZIP file was uploaded and content was replaced!</h2>";
} else {
$message = "There was a problem with the upload. Please try again.";
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>QikSoft ZIP Upper</title>
</head>
<body>
<?php if(!empty($message)) echo "<p>$message</p>"; ?>
<form enctype="multipart/form-data" method="post" action="">
<label><h3>Upload Your ZIP:</h3> <input type="file" name="zip_file" /></label>
<br /><br />
<input type="submit" name="submit" value="START UPLOAD" />
</form>
</body>
</html>
Also note that the echo message has been changed:
<?php if(!empty($message)) echo "<p>$message</p>"; ?>
One thing that does not work is file restriction. Currently allows other types besides ZIP. Anyone with fix, be greatly appreciated.
It's echo $_FILES['upload']['tmp_name'];
Try checking and adjusting the post_max_size value in your php.ini file, this worked for me as the default is 3M, raised the value to 128M and everything was peachy
The below is my source code where I want to use the variable $r1 which is at the bottom php script in another php script which is on top on the same page. I need a simple solution which solves this problem. I want to use that variable in the update query which is present in the code.
<?php
$con=mysql_connect("localhost","root","") or die("could not connect to db");
mysql_select_db("test");
$valid_formats = array("jpg", "png", "gif", "zip", "bmp","MP4","3GP");
$max_file_size = 1024*10000; //100 kb
//$path = "uploads/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to execute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
move_uploaded_file($_FILES["files"]["tmp_name"][$f],"uploads/" . $_FILES["files"]["name"][$f]);
// Number of successfully uploaded files
$file="uploads/".$_FILES["files"]["name"][$f];
/* $sql = "Update media set path = '$file' where username = '$r1'";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}*/
}
}
}
}
$sql="select * from media";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query))
{
$image=$row[2];
if($image!='')
{
echo "<img src='".$row['path']."' width='175' height='200' />";
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Upload with PHP - Demo</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="wrap">
<h1><a>Multiple File Upload with PHP</a></h1>
<?php
# error messages
if (isset($message)) {
foreach ($message as $msg) {
printf("<p class='status'>%s</p></ br>\n", $msg);
}
}
# success message
if($count !=0){
printf("<p class='status'>%d files added successfully!</p>\n", $count);
}
?>
<p>Max file size 100kb, Valid formats jpg, png, gif</p>
<br />
<br />
<!-- Multiple file upload html form-->
<form action="" method="post" enctype="multipart/form-data">
<span style="font-size:12pt;">Channel: </span> <select name="channels">
<option value="">Channel</option>
<?php
$sql_query="SELECT * FROM media";
$result1 = mysql_query($sql_query) or die(mysql_error());
while($data=mysql_fetch_row($result1))
{
?>
<option value="<?php $r1 = $data[0]; echo $r1;?>" ><?php $r1 = $data[1]; echo $r1 ?></option>
<?php
}
?>
</select>
<input type="file" name="files[]" id="image" multiple="multiple">
<input type="submit" value="Upload">
</form>
</div>
</body>
</html>
If you want to have the value of the <select name="channels"> you can use this
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
$r1 = $_POST["channels"];
//your code here
}
Hope this help.