php upload file issue using array - php

in my form I'm trying to upload multiple or single file using php. So my html table is look like this :
<tr>
<td width="142"><b>Docs</b>
<td width="142"><input type="file" name="files[]" id="project_docs1" class="docfile" /></td>
<td width="142"><input type="file" name="files[]" id="project_docs2" class="docfile" /></td>
<td width="142"><input type="file" name="files[]" id="project_docs3" class="docfile" /></td>
</td>
Now when I upload only one file it's showing me error message like : Invalid Format but it's should be accept one file. it's not require to must be upload all 3 files. Can you tell me why it's showing this error message called Invalid Format ? If upload all 3 files then it's working fine.
and when I press the upload button without upload any file it's showing me value 1 for $noOfUpload variable. why ?
$valid_formats = array("jpg", "png", "gif", "txt", "bmp");
$max_file_size = 1024*100; //100 kb
$path = "project_docs"; // Upload directory
$error = array(); // load error message
$files_name = array(); // get uploaded file name
foreach ($_FILES['files']['name'] as $key => $name) {
$size = $_FILES['files']['size'][$key]. "<br>";
$noOfUpload = count($name);
if($noOfUpload <= 0){
$error[] = "Upload your document/s<br>";
}elseif(!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){
$error[] = "invalid file formate : $name<br>";
}
//$name = md5(uniqid()) . '-' . htmlspecialchars_decode($name);
$files_name[] = "$name";
}
if(!empty($error)){
foreach ($error as $e) {
echo "<div class='error'>$e</div>";
}
}else{
foreach ($files_name as $fn) {
echo "$fn<br>";
}
}
You help is more appreciate. :)

Try this
if (isset($_FILES))
{
$valid_formats = array("jpg", "png", "gif", "txt", "bmp");
for ($i = 0; $i < count($_FILES['files']['name']); $i++)
{
if (in_array(pathinfo($FILES['files']['name'][$i], PATHINFO_EXTENSION), $valid_formats))
{
$tmp_path = $_FILES['files']['tmp_name'][$i];
if ($tmp_path != "")
{
if (move_uploaded_file($tmp_path, $new_path))
{
//Handle other code here
}
else
{
$error[] = ""; //your error handling
}
}
else
{
$error[] = ""; //your error handling
}
}
else
{
$error[] = "invalid file formate : $name<br>";
}
}
}

Your code is run for whole array either it's blank or not. First, count your array then run your code upto that count.

<tr>
<td width="142"><b>Docs</b>
<td width="142"><input type="file" name="files[]" id="project_docs1" class="docfile" /></td>
</tr>
PHP Code:
$valid_formats = array("jpg", "png", "gif", "txt", "bmp");
$max_file_size = 1024*100; //100 kb
$path = "project_docs"; // Upload directory
$error = array(); // load error message
$files_name = array(); // get uploaded file name
foreach ($_FILES['files']['name'] as $key => $name) {
$size = $_FILES['files']['size'][$key]. "<br>";
$noOfUpload = count($name);
if($noOfUpload <= 0){
$error[] = "Upload your document/s<br>";
}elseif(!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){
$error[] = "invalid file format : $name<br>";
}
//$name = md5(uniqid()) . '-' . htmlspecialchars_decode($name);
$files_name[] = "$name";
}
if(!empty($error)){
foreach ($error as $e) {
echo "<div class='error'>$e</div>";
}
}else{
foreach ($files_name as $fn) {
echo "$fn<br>";
}
}

Related

Display images from a directory and link to video with same name

I'm using below code to show all jpg's within a directory.
$dirname = "var/www/media/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
In the same directory I have movies with the same name. Example:
ls var/www/media
Aliens.mpg
Aliens.jpg
Simsons.avi
Simsons.jpg
I need each jpg image to be clickable, linking to the corresponding video file.
Is there an easy way doing that?
Here you go!
NOTE
1) Make sure file names are same.
2) You can add more ext file in the variables $imgExts and $vidExts.
<?php
$files = glob("media/*.*");
$vid = NULL;
$imgExts = array("gif", "jpg", "jpeg", "png", "tiff", "tif");
$vidExts = array("mp4", "mpg", "avi", "mk4", "ogg", "3gp");
for ($i=0; $i<count($files); $i++) {
$image = $files[$i];
$urlExt = pathinfo($files[$i], PATHINFO_EXTENSION);
if (in_array($urlExt, $imgExts)) {
for ($j=0; $j<count($files); $j++) {
$urlExt2 = pathinfo($files[$j], PATHINFO_EXTENSION);
if (in_array($urlExt2, $vidExts)) {
if (strcmp($urlExt, $urlExt2) == 0) {
$vid=$files[$j];
}
}
}
echo '<img src="'.$image .'" />'."<br />";
}
}
UPDATE:
It will show the error if any file not found(i.e. if image not found for video or vice versa)
<?php
$x=1; // initially giving value for x=1
$files = glob("media/*.*");
$vid=NULL;
$vidf=$files; //making copy of files array
$imgExts = array("gif", "jpg", "jpeg", "png", "tiff", "tif");
$vidExts = array("mp4", "mpg", "avi", "mk4", "ogg", "3gp");
for ($i=0; $i<count($files); $i++) {
$image = $files[$i];
$urlExt = pathinfo($files[$i], PATHINFO_EXTENSION);
if(in_array($urlExt,$imgExts )){
for($j=0; $j<count($files); $j++){
$urlExt2 = pathinfo($files[$j], PATHINFO_EXTENSION);
if(in_array($urlExt2,$vidExts )){
if(strcmp(pathinfo($files[$i], PATHINFO_FILENAME),pathinfo($files[$j], PATHINFO_FILENAME))==0){
$vid=$files[$j];
$x=0; // put the value of x=0 if video for that image found!
unset($vidf[array_search($vid , $vidf)]); // search & delete video from array with have images
}
}
}
if($x==0)
echo '<img src="'.$image .'" />'."<br />";
else if($x==1){ //check if image have the video
echo 'Video for Image <b>', pathinfo($image , PATHINFO_FILENAME),'.',pathinfo($image , PATHINFO_EXTENSION), ' </b>Not Found!<br>';
$x=0;}
}
}
foreach ($vidf as $vidf) { // show let out videos who's images not found
$urlExt2 = pathinfo($vidf, PATHINFO_EXTENSION);
if(in_array($urlExt2,$vidExts )){
echo "Image of the Video <b> ",pathinfo($vidf , PATHINFO_FILENAME),'.', pathinfo($vidf , PATHINFO_EXTENSION),'</b> Not found!' ;
echo '<br>';
}
}
?>

how to check post image by id in php

I have 3 inputs :
1. application name
2. Background photo
3. logo photo
After I got help from #mith. The code is great. Images always go into correct folder. I adapt the code to change image's name after submit it into folder. But i don't know what wrong with this condition
$fieldname = ($key == 'image[]') ? 'image' : 'logo';
$filename = $applicationName . '_' . $fieldname . '.' .
pathinfo($upload["tmp_name"], PATHINFO_EXTENSION);
$filename always be logo. I don't know why the condition always false.
So, 2 images always named applicationName_logo. please help me find out.
HTML form :
<form action="yong.php" method="POST" enctype="multipart/form-data">
<h3>App name</h3>
<input type="text" id="applicationName" name="applicationName">
<h3>Background image</h3>
<input type="file" id="image" name="image[]" multiple="multiple" accept="image/*" />
<h3>Logo image</h3>
<input type="file" id="logo" name="logo[]" multiple="multiple" accept="image/*" />
<br>
<br>
<input type="submit">
</form>
PHP code :
<?php
$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 5000*100; //100 kb
$path = "home_dir/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
$applicationName = $_POST['applicationName'];
$sql_field_list = ['applicationName'];
$sql_value_list = [$applicationName];
foreach ($_FILES['image']['name'] as $f => $name) {
if ($_FILES['image']['error'][$f] == 4) {
continue; // Skip file if any error found
echo "Skip file if any error found";
}
if ($_FILES['image']['error'][$f] == 0) {
if ($_FILES['image']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
echo "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
echo "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
$tmp_name = $upload["tmp_name"];
$parts = explode('/', $upload['tmp_name']);
$tmpName = array_pop($parts);
$fieldname = ($key == 'image[]') ? 'image' : 'logo';
$filename = $applicationName . '_' . $fieldname . '.' . pathinfo($upload["tmp_name"], PATHINFO_EXTENSION);
}
//if(move_uploaded_file($_FILES["image"]["tmp_name"][$f], $path.$filename.png))
if(move_uploaded_file($_FILES["image"]["tmp_name"][$f], $path.'applicationName_bg/'.$filename.png))
$count++; // Number of successfully uploaded file
$message[] = "$name is uploaded";
echo "$filename is uploaded";
}
}
foreach ($_FILES['logo']['name'] as $f => $name) {
if ($_FILES['logo']['error'][$f] == 4) {
continue; // Skip file if any error found
echo "Skip file if any error found";
}
if ($_FILES['logo']['error'][$f] == 0) {
if ($_FILES['logo']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
echo "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
echo "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
$tmp_name = $upload["tmp_name"];
$parts = explode('/', $upload['tmp_name']);
$tmpName = array_pop($parts);
$fieldname = ($key == 'image[]') ? 'image' : 'logo';
$filename = $applicationName . '_' . $fieldname . '.' . pathinfo($upload["tmp_name"], PATHINFO_EXTENSION);
}
//if(move_uploaded_file($_FILES["logo"]["tmp_name"][$f], $path.$filename.png))
if(move_uploaded_file($_FILES["logo"]["tmp_name"][$f], $path.'applicationName_logo/'.$filename.png))
$count++; // Number of successfully uploaded file
$message[] = "$name is uploaded";
echo "$filename is uploaded";
}
}
}
Try the following code to save logo and image files separately:
HTML:
<form action="yong.php" method="POST" enctype="multipart/form-data">
<h3>App name</h3>
<input type="text" id="applicationName" name="applicationName">
<h3>Background image</h3>
<input type="file" id="image" name="image[]" multiple="multiple" accept="image/*" />
<h3>Logo image</h3>
<input type="file" id="logo" name="logo[]" multiple="multiple" accept="image/*" />
<br>
<br>
<input type="submit">
</form>
PHP
<?php
$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 5000*100; //100 kb
$path = "home_dir/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['image']['name'] as $f => $name) {
if ($_FILES['image']['error'][$f] == 4) {
continue; // Skip file if any error found
echo "Skip file if any error found";
}
if ($_FILES['image']['error'][$f] == 0) {
if ($_FILES['image']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
echo "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
echo "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["image"]["tmp_name"][$f], $path.'applicationName_bg/'.$name))
$count++; // Number of successfully uploaded file
$message[] = "$name is uploaded";
echo "$name is uploaded";
}
}
}
foreach ($_FILES['logo']['name'] as $f => $name) {
if ($_FILES['logo']['error'][$f] == 4) {
continue; // Skip file if any error found
echo "Skip file if any error found";
}
if ($_FILES['logo']['error'][$f] == 0) {
if ($_FILES['logo']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
echo "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
echo "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["logo"]["tmp_name"][$f], $path.'applicationName_logo/'.$name))
$count++; // Number of successfully uploaded file
$message[] = "$name is uploaded";
echo "$name is uploaded";
}
}
}
}

need php code to post below 5 images into one field of db separated by br

Please help me
My html code is as follows:
here image_name is getting through echo from another upload query
My upload script code
$path = "uploads/";
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name)){
$ext = getExtension($name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
$time = time();
$ip = $_SERVER['REMOTE_ADDR'];
mysql_query("INSERT INTO uploads(image_name,poster_user,created,cat,status,ip) VALUES('$actual_image_name','$u_id','$time', 'Photos', '1', '$ip')");
echo "<img src='uploads/".$actual_image_name."' class='previewOfimgss'> ";
$allimages_name = "$actual_image_name";
echo "$allimages_name";
}
else
echo "Fail upload folder with read access.";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
else
echo "Please select image..!";
exit;
}
It is working quite good and giving result like
and i want images names in on textbox like
$items=$_POST["post_items"];
$final = "";
foreach($items as $item){
$final .= $item."<br>";
}
echo $final
And you can then pass the $final variable to the column.
There is another way to do it.
$items= $_POST["post_items"];
$final = implode("<br>",$items);
It will work only if $items is array.
Ok I've done a working solution for you. This is a kind of prototype of your system. Hope it helps you in what you are building.
fileForm.php (Where you select file to upload.)
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="uploadFile.php" method="post" enctype="multipart/form-data">
<input type="file" name="photoimg[]" multiple="yes">
<input type="submit" name="fileUploader">
</form>
</body>
</html>
uploadFile.php (Where you upload your files as in your question)
<?php
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$path = "uploads/"; // Upload directory
// Return's files extension
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP"); // Valid formats to upload
$fileCount=count($_FILES["photoimg"]["name"]); // Number of files uploaded
$files=array(); // Initilize an empty array to save names
// Loop through all files and upload them
for ($i=0; $i < $fileCount; $i++) {
$name=$_FILES["photoimg"]["name"][$i];
$tmp=$_FILES["photoimg"]["tmp_name"][$i];
$size=$_FILES["photoimg"]["size"][$i];
// If name is not empty
if(!empty($name)){
$ext = getExtension($name); // Get file extension
// If file is valid to upload
if(in_array($ext,$valid_formats)){
If file is less than 1 MB.
if($size<(1024*1024)){
$actual_image_name = time().substr(str_replace(" ", "_", $name), 5); // Final name of image
// If file uploads successfully
if(move_uploaded_file($tmp, $path.$actual_image_name)){
$time=time();
$ip=$_SERVER['REMOTE_ADDR'];
mysql_query("INSERT INTO uploads(image_name,poster_user,created,cat,status,ip) VALUES('$actual_image_name','$u_id','$time', 'Photos', '1', '$ip')"); // Insert into your table
echo "<img src='uploads/$actual_image_name' class='previewOfimgss'> "; // Show the image
$files[$i] = $actual_image_name; // Save file names
}else{
echo "Fail upload folder with read access.";
}
}else{
echo "Image file size max 1 MB";
}
}else{
echo "Invalid file format..";
}
}else{
echo "Please select image..!";
}
}
}
?>
<form action="toSaveFileName.php" method="post">
<?php
for ($i=0; $i < $fileCount; $i++) {
// Generate input fields
echo "<input type='text' name='post_items[]' value='{$files[$i]}'>";
}
?>
<input type="submit">
</form>
toSaveFileName.php (This is what you originally asked for.)
$items=$_POST["post_items"]; // from input fields
$todb=""; // to send to database
if(is_array($items)){
$todb=implode("<br>",$items);
}else{
$todb=$items;
}
echo $todb; // for output
//save to database
Now implementing it to your system is your job. And I hope you should be able to do it on your own.
Don't forget to mark this as answer and vote up.

Uploading Files and Images with mkdir specific location

What I am trying to put images where my folder is generate from "$tablename", but fail to bring the image there. How do I store the uploaded files ?
I like to upload images from my form.
<input type="file" id="file" name="files" multiple />
No matter which of those upload techniques I use is not good to use, to save the file to a specific location on the server.
If you have an idea how to do this problem please.
here is the mkdir code. The code is works fine.
<?php
$tablename = "fisa";
$next_increment = 0;
$qShowStatus = "SHOW TABLE STATUS LIKE '$tablename'";
$qShowStatusResult = mysql_query($qShowStatus) or die("" . mysql_error() . "" . $qShowStatus);
$row = mysql_fetch_assoc($qShowStatusResult);
$next_increment = $row['Auto_increment'];
echo "$next_increment";
$tablename = (string) ("$next_increment");
$year = date("Y");
$month = date("m");
$day = date("d");
If (!file_exists($year)) {
$createsyear = mkdir("$year", 0777);
} else {
If (!file_exists("$year/$month")) {
$createsmonth = mkdir("$year/$month", 0777);
} else {
If (!file_exists("$year/$month/$day")) {
$createsday = mkdir("$year/$month/$day", 0777);
} else {
If (!file_exists($year / $month / $day / $tablename)) {
$createsday = mkdir("$year/$month/$day/$tablename", 0777);
} else {
//dada
}
}
}
}
?>
Thank You.
Here I give basic example for file upload.
in HTML
<form action="phpfilename.php" method="post" enctype="multipart/form-data" >
<input type="file" name="files" />
<input type="submit" name="submit" />
</form>
In PHP
<?php
if(isset($_POST['submit']))
{
$path = $_FILES["files"]["name"];
$target = "$year/$month/$day/$tablename/$path"; //this is your path where image need to be saved
move_uploaded_file($_FILES["files"]["tmp_name"], $target);
}
?>
I fix the problem
<?php
$tablename = "fisa";
$next_increment = 0;
$qShowStatus = "SHOW TABLE STATUS LIKE '$tablename'";
$qShowStatusResult = mysql_query($qShowStatus) or die ( "" . mysql_error() . "" . $qShowStatus );
$row = mysql_fetch_assoc($qShowStatusResult);
$next_increment = $row['Auto_increment'];
echo "$next_increment";
$tablename = (string)("$next_increment");
$year = date("Y");
$month = date("m");
$day = date("d");
If(!file_exists($year)){
$createsyear = mkdir("$year", 0777);
}
else
{
If(!file_exists("$year/$month")){
$createsmonth = mkdir("$year/$month", 0777);
}
else
{
If(!file_exists("$year/$month/$day")){
$createsday = mkdir("$year/$month/$day", 0777);
}
else
{
If(!file_exists($year/$month/$day/$tablename)){
$createsday = mkdir("$year/$month/$day/$tablename/", 0777);
$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*100; //100 kb
$target = "$year/$month/$day/$tablename/";
$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
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $target.$name)) {
$count++; // Number of successfully uploaded files
}
}
}
}
}
}
else
{
}
}
}
}
?>

Multiple image upload with PHP

I'm trying upload multiple images. Below you can see my code.
Image uploaded massage repenting (exactly the amount of image chosen to upload.)
How can i show "Image uploaded" massage only ones on successful submit?
If i put the message after the loop it will start to show no matter if there is any errors.
This is my PHP code:
<?php
error_reporting(0);
session_start();
include('db.php');
$id = $mysqli->escape_string($_GET['id']);
define ("MAX_SIZE","9000");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$valid_formats = array("jpg", "png", "gif", "jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$uploaddir = "gallery/"; //a directory inside
foreach ($_FILES['photos']['name'] as $name => $value)
{
$filename = stripslashes($_FILES['photos']['name'][$name]);
$size=filesize($_FILES['photos']['tmp_name'][$name]);
//get the extension of the file in a lower case format
$ext = getExtension($filename);
$ext = strtolower($ext);
if(in_array($ext,$valid_formats))
{
if ($size < (MAX_SIZE*1024))
{
$image_name=time().$filename;
$newname=$uploaddir.$image_name;
if (move_uploaded_file($_FILES['photos']['tmp_name'][$name], $newname))
{
$mysqli->query("INSERT INTO galleries(image) VALUES('$image_name')");
echo "Image uploaded";
}
else
{
echo '<span class="imgList">You have exceeded the size limit! so moving unsuccessful! </span>';
}
}
else
{
echo '<span class="imgList">You have exceeded the size limit!</span>';
}
}
else
{
echo '<span class="imgList">Unknown extension!</span>';
}
}
}
?>
Any help will be appropriated.
You can implement a counter and when an upload completes successfully you can increment that counter variable then compare it against total number of array items after foreach loop completes. I modified your code for this (haven't checked it but should work).
<?php
error_reporting(0);
session_start();
include('db.php');
$id = $mysqli->escape_string($_GET['id']);
define ("MAX_SIZE","9000");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$valid_formats = array("jpg", "png", "gif", "jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$uploaddir = "gallery/"; //a directory inside
$successfulUploads = 0;
foreach ($_FILES['photos']['name'] as $name => $value)
{
$filename = stripslashes($_FILES['photos']['name'][$name]);
$size=filesize($_FILES['photos']['tmp_name'][$name]);
//get the extension of the file in a lower case format
$ext = getExtension($filename);
$ext = strtolower($ext);
if(in_array($ext,$valid_formats)) {
if ($size < (MAX_SIZE*1024)) {
$image_name=time().$filename;
$newname=$uploaddir.$image_name;
if (move_uploaded_file($_FILES['photos']['tmp_name'][$name], $newname)) {
$mysqli->query("INSERT INTO galleries(image) VALUES('$image_name')");
//echo "Image uploaded";
$successfulUploads = $successfulUploads + 1;
} else {
echo '<span class="imgList">Moving unsuccessful! </span>';
}
} else {
echo '<span class="imgList">You have exceeded the size limit!</span>';
}
} else {
echo '<span class="imgList">Unknown extension!</span>';
}
}
if($successfulUploads === count($_FILES['photos'])){
echo 'UPLOAD SUCCESS!';
} else {
echo 'NOT ALL IMAGES WERE UPLOADED SUCCESSFULLY';
}
}
*If you wanted to get more complex with it you could create another array variable instead of a counter and if the upload fails you could add the file name to the array and then check the length of the array where I'm doing the comparison. If count > 0 then you would know there was an error and you could echo the filenames that failed to upload

Categories