473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

php multiple file upload using array or for loop

120 100+
I have a working example below of a single file input upload via php and now I'd like to upload multiple files either by iterating through each input type="file".

I've read that the multiple attribute on the input tag is not supported below IE10 so I think the best way would be to have several input type=files in order to loop through them

any help is appreciated

Expand|Select|Wrap|Line Numbers
  1.     <input type="file" name="FilesUpload1" class="filesUpload" />
  2.     <input type="file" name="FilesUpload2" class="filesUpload" />
  3.     <input type="file" name="FilesUpload3" class="filesUpload" />
  4.  
  5.     <?php
  6.     //Сheck that we have a file
  7.     if((!empty($_FILES["FilesUpload1"])) && ($_FILES['FilesUpload1']['error'] == 0)) {
  8.  
  9.         //Check if the file is JPEG image and it's size is less than 350Kb
  10.         $filename = basename($_FILES['FilesUpload1']['name']);
  11.         $ext = substr($filename, strrpos($filename, '.') + 1);
  12.  
  13.         //check file extension
  14.         if ((($ext == "gif") 
  15.         || ($ext == "jpeg")
  16.         || ($ext == "jpg")
  17.         || ($ext == "png")
  18.         || ($ext == "doc")
  19.         || ($ext == "docx")
  20.         || ($ext == "rtf")
  21.         || ($ext == "txt")            
  22.         || ($ext == "pdf"))
  23.  
  24.         //check file mime
  25.         && (($_FILES["FilesUpload1"]["type"] == "image/gif")
  26.         || ($_FILES["FilesUpload1"]["type"] == "image/jpeg")
  27.         || ($_FILES["FilesUpload1"]["type"] == "image/jpg")
  28.         || ($_FILES["FilesUpload1"]["type"] == "image/pjpeg")
  29.         || ($_FILES["FilesUpload1"]["type"] == "image/x-png")
  30.         || ($_FILES["FilesUpload1"]["type"] == "image/png")
  31.         || ($_FILES["FilesUpload1"]["type"] == "application/msword")
  32.         || ($_FILES["FilesUpload1"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
  33.         || ($_FILES["FilesUpload1"]["type"] == "application/rtf")
  34.         || ($_FILES["FilesUpload1"]["type"] == "text/plain")
  35.         || ($_FILES["FilesUpload1"]["type"] == "application/pdf"))
  36.  
  37.  
  38.         //check file size is less than 1048576 bytes [1 MB]
  39.         && (($_FILES["FilesUpload1"]["size"] < 1048576))) {
  40.  
  41.             //Determine the path to which we want to save this file
  42.             $newname = dirname(__FILE__).'/../entries/'.$_POST["CompanyName"].'-'.$filename;    
  43.  
  44.             //Check if the file with the same name is already exists on the server
  45.             if (!file_exists($newname)) {
  46.  
  47.                 //Attempt to move the uploaded file to it's new place
  48.                 if ((move_uploaded_file($_FILES['FilesUpload1']['tmp_name'],$newname))) {
  49.                     echo "It's done! Your file has been saved.";
  50.  
  51.                 } else {
  52.                     echo "Error: A problem occurred during file upload!";
  53.                 }
  54.  
  55.             } else {
  56.                 echo "Error: File ".$_FILES["FilesUpload1"]["name"]." already exists";
  57.             }
  58.  
  59.         } 
  60.  
  61.         else {
  62.             echo "Error: Only .gif, .jpeg, .jpg, .png, .doc, .docx, .rtf, .txt, .pdf files under 1 MB are accepted for upload.";
  63.         }
  64.  
  65.     } else {
  66.         echo "Error: No file uploaded";
  67.     }
  68.  
  69.     ?>
  70.  
  71.  
Sep 9 '13 #1
1 4056
Dormilich
8,658 Expert Mod 8TB
any help is appreciated
use a function.
Sep 10 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Rajesh S | last post by:
Hi, Is it possible to upload multiple files using WebClient Class(Like HttpRequest Class).? If Possible please help!!
1
by: Avin Patel | last post by:
Hi, I am looking for script to allow multiple files can be uploaded/attached in webform ( mostly cgi/perl or php). But I don't like the multiple input boxes using "<input type="file" size="40"...
5
by: Shawn H. Mesiatowsky | last post by:
I am creating an intranet App that is a document management system, and now I have been told they wan't the ebilty to version control directories, not just files. So I have the file upload and...
0
by: Yandos | last post by:
Hello all, I'm sorry for a bit off-topic post, but curl does not have own newsgroup, so I hope someone might help me here... I need to feed form like the following using libcurl: <form...
3
by: kksandeep | last post by:
i am using this three files to uplod file. i got this file from net but i think these have some error. i am new to this field plz help the script i found is some helpful but not too that i need ...
7
by: der_grobi | last post by:
That is the Problem: I have an ASP.NET Webapplicatipon where I can upload single files to the Server. That works fine. But now, I want to Upload multiple files. I know the path of the files, i...
5
by: John Devlon | last post by:
Hi, Some people like to go on vacation during christmas time, others try to do something they never did before.... I would like to create a multiple file upload page, with some nice progress...
1
by: ganesandeiav | last post by:
Dear frnds I am using jsp.I have a problem file upload using html control.I want filter seleted file such as *.gif&*.jpeg images .It should be list out jpeg gif image only. iam try this code but...
9
by: torso | last post by:
Hi Does someone know a good tutorial for multiple file upload with xmlHttpRequest. I am trying to do directory upload. So I could choose directorys and upload those to the server. Another...
8
johngault
by: johngault | last post by:
I've been working with this PHP page for several days now and I'm stumped. The page is supposed to allow the user to upload up to six images for their profile. When the user adds an image it (the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.