473,404 Members | 2,187 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,404 software developers and data experts.

Uploading 2 Files Optionally with Form

chunk1978
224 100+
hi there... i have a form where a user may optionally upload a maximum of 2 files along with other textual data (there are 2 file fields built in the form). i'm having trouble writing a php script that will allow 2 files to be uploaded optionally.

i have successfully written a small php script that will allow one file upload (with one file field in the form), as well as uploading 2 files (with 2 file fields in the form). but i don't know how to make it optional. it seems that if the file field is there the user MUST upload something or else there's an error...

here is my HTML form code:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  4. <title>Untitled Document</title>
  5. </head>
  6.  
  7. <body>
  8.  
  9. <form enctype="multipart/form-data" action="uploadtest.php" method="POST">
  10.  
  11. <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
  12. Choose a file to upload:<br /> <input name="datafileA" type="file" /><br />
  13.  
  14. <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
  15. Choose a file to upload:<br /> <input name="datafileB" type="file" />
  16.  
  17. <br /><br />
  18. <input type="submit" value="SUBMIT" />
  19. </form>
  20.  
  21. </body>
  22. </html>
  23.  
here is my PHP script:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. function UploadFiles()
  4.     {
  5.     $FileA = $_FILES['datafileA'];
  6.     $FileB = $_FILES['datafileB'];
  7.  
  8.     if (($FileA != "") && ($FileB == ""))
  9.     {
  10.     UploadA();
  11.     }
  12.     else if (($FileA == "") && ($FileB != ""))
  13.     {
  14.     UploadB();
  15.     }
  16.     else if (($FileA != "") && ($FileB != ""))
  17.     {
  18.     UploadAandB();
  19.     }
  20.     else
  21.     {
  22.     echo 'No Files Uploaded';
  23.     }
  24. }    
  25.  
  26. function UploadA()
  27.     {
  28.     $newfolder = "uploads/";
  29.     $uploadA = $newfolder . basename( $_FILES['datafileA']['name']); 
  30.  
  31.     if    (move_uploaded_file($_FILES['datafileA']['tmp_name'], $uploadA))
  32.     {
  33.     echo 'File A Uploaded';
  34.     }
  35.     else
  36.     {
  37.        echo 'Error';
  38.     }
  39. }
  40.  
  41. function UploadB()
  42.     {
  43.     $newfolder = "uploads/";
  44.     $uploadB = $newfolder . basename( $_FILES['datafileB']['name']); 
  45.  
  46.     if    (move_uploaded_file($_FILES['datafileB']['tmp_name'], $uploadA))
  47.     {
  48.     echo 'File B Uploaded';
  49.     }
  50.     else
  51.     {
  52.     echo 'Error';
  53.     }
  54. }
  55.  
  56. function UploadAandB()
  57.     {
  58.     $newfolder = "uploads/";
  59.     $uploadA = $newfolder . basename( $_FILES['datafileA']['name']); 
  60.     $uploadB = $newfolder . basename( $_FILES['datafileB']['name']);
  61.  
  62.     if    ((move_uploaded_file($_FILES['datafileA']['tmp_name'], $uploadA)) && (move_uploaded_file($_FILES['datafileB']['tmp_name'], $uploadB)))
  63.     {
  64.     echo 'File A and B Uploaded';
  65.     }
  66.     else
  67.     {
  68.     echo 'Error';
  69.     }
  70. }
  71.  
  72. ?>
  73.  
May 3 '07 #1
3 1511
vituko
48
your check is not ok.

>$FileA = $_FILES['datafileA'] ;
>if ($FileA != "")

it must be :
if (! $_FILES ['datafileA'] ['error'])

if there is an input type file named datafileA not used, empty, $_FILES ['datafileA'] will be an array, it wont be empty, but its field error will contain a value of 4.

Check php.net, all the errors are explained.

An alternative, name both inputs as "yourname[]" then in php you will have an array ($_INPUT ['yourname']['name'][0], $_INPUT ['yourname']['name'][1], $_INPUT ['yourname']['error][0], etc) and you will can process it recursively (a loop maybe). 3 functions are not needed.

Good luck
May 3 '07 #2
chunk1978
224 100+
your check is not ok.

>$FileA = $_FILES['datafileA'] ;
>if ($FileA != "")

it must be :
if (! $_FILES ['datafileA'] ['error'])

if there is an input type file named datafileA not used, empty, $_FILES ['datafileA'] will be an array, it wont be empty, but its field error will contain a value of 4.

Check php.net, all the errors are explained.

An alternative, name both inputs as "yourname[]" then in php you will have an array ($_INPUT ['yourname']['name'][0], $_INPUT ['yourname']['name'][1], $_INPUT ['yourname']['error][0], etc) and you will can process it recursively (a loop maybe). 3 functions are not needed.

Good luck
hi vituko. thanks for the response.

i can't use an array... i have to figure out how to address each file by it's field name because the form's javascript is dependent on each file field having a separate name.

i know this should only be a super easy script to write, but it's so complicated and nothing works. i've been on this almost all day and i still have no idea how to write this small code...

for my form, a user isn't just uploading files... file uploading from the form is optional. but it seems that every file uploading tutorial i've read over the past few days is written so that an error occurs if a file or files aren't uploaded... so i don't know how:

1) to make the uploading optional
2) to address the 2 files as separate names without making an array
3) to continue processing text fields within the same php script after files are uploaded / or not uploaded.

i really hope someone can point me in the right direction...
May 3 '07 #3
chunk1978
224 100+
so this is the closest to success i've come so far... it works, but something very strange is happening with the uploaded files. after the file is uploaded, the file size is smaller.

for example: i uploaded a .JPG image that is 170kb from my desktop, but after is was uploaded, the file was only 115kb on the server. so to compare the differences, i downloaded the file off the server onto my computer... they both looked the same but had different file sizes. i'm assuming my PHP code is doing something to the file when it's being uploaded...

here is my current working PHP code... if anyone sees something wrong with it please educate me...

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $uploadfolder = "uploads/";
  4. $uploadfileA = $uploadfolder . basename($_FILES['datafileA']['name']);
  5. $uploadfileB = $uploadfolder . basename($_FILES['datafileB']['name']);
  6.  
  7. if    (move_uploaded_file($_FILES['datafileA']['tmp_name'], $uploadfileA))
  8.     { 
  9.     echo "Successful upload File A.\n\n";
  10.     }
  11.     else
  12.     {
  13.     echo "File A Empty.\n\n";
  14. }
  15.  
  16. if    (move_uploaded_file($_FILES['datafileB']['tmp_name'], $uploadfileB))
  17.     { 
  18.     echo "Successful upload File B.\n\n";
  19.     }
  20.     else
  21.     {
  22.     echo "File B Empty.\n\n";
  23. }
  24.  
  25. ?>
  26.  
May 3 '07 #4

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

Similar topics

4
by: dickiedyce | last post by:
Hi there. I've spent the weekend getting ever more frustrated, trying to get an upload file function working on a website. The site is hosted by a company called oneandone. They're using PHP...
6
by: Chamomile | last post by:
can anyone point me to some straightforward information on file uploading without using an html form? That is, direcly from within a php script. if I know the local path etc. to a particular...
5
by: Ron Brennan | last post by:
Good afternoon. The entire task that I'm trying to achieve is to allow a user to browse and upload multiple files simultaneously, hiding the Browse button of <input> tags of type="file" and...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
2
by: tshad | last post by:
I have a page that I am using for multiple uses that includes reposting of the page as well as going to another page where the "form" tag would be something like: <form id="something"...
5
by: Chris | last post by:
I have a meetings section I'm developing on our intranet. Using PHP/MySQL. Meeting info and Meeting docs reside on 2 related tables in the db. Users may want to upload anywhere from 1 to 10 or...
6
by: ajaykataram | last post by:
Hi all i am getting error in the following code to upload files csv and txt files i have to insert the following values only into database if firstname,lastname and email id is null it should not...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
0
by: LoriFranklin | last post by:
I'm a bit of a newbie here. I've learned a lot from reading the posts you all have here. I need some help uploading files using an asp form. I am using some code that I found from Jacob at...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.