Uploading 2 Files Optionally with Form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chunk1978
    New Member
    • Jan 2007
    • 224

    Uploading 2 Files Optionally with Form

    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:
    Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <form enctype="multipart/form-data" action="uploadtest.php" method="POST">
    
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
    Choose a file to upload:<br /> <input name="datafileA" type="file" /><br />
    
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
    Choose a file to upload:<br /> <input name="datafileB" type="file" />
    
    <br /><br />
    <input type="submit" value="SUBMIT" />
    </form>
    
    </body>
    </html>
    here is my PHP script:
    Code:
    <?php
    
    function UploadFiles()
    	{
    	$FileA = $_FILES['datafileA'];
    	$FileB = $_FILES['datafileB'];
    
    	if (($FileA != "") && ($FileB == ""))
    	{
    	UploadA();
    	}
    	else if (($FileA == "") && ($FileB != ""))
    	{
    	UploadB();
    	}
    	else if (($FileA != "") && ($FileB != ""))
    	{
    	UploadAandB();
    	}
    	else
    	{
    	echo 'No Files Uploaded';
    	}
    }	
    
    function UploadA()
    	{
    	$newfolder = "uploads/";
    	$uploadA = $newfolder . basename( $_FILES['datafileA']['name']); 
    	
    	if	(move_uploaded_file($_FILES['datafileA']['tmp_name'], $uploadA))
    	{
    	echo 'File A Uploaded';
    	}
    	else
    	{
           echo 'Error';
    	}
    }
    
    function UploadB()
    	{
    	$newfolder = "uploads/";
    	$uploadB = $newfolder . basename( $_FILES['datafileB']['name']); 
    	
    	if	(move_uploaded_file($_FILES['datafileB']['tmp_name'], $uploadA))
    	{
    	echo 'File B Uploaded';
    	}
    	else
    	{
        echo 'Error';
    	}
    }
    
    function UploadAandB()
    	{
    	$newfolder = "uploads/";
    	$uploadA = $newfolder . basename( $_FILES['datafileA']['name']); 
    	$uploadB = $newfolder . basename( $_FILES['datafileB']['name']);
    
    	if	((move_uploaded_file($_FILES['datafileA']['tmp_name'], $uploadA)) && (move_uploaded_file($_FILES['datafileB']['tmp_name'], $uploadB)))
    	{
    	echo 'File A and B Uploaded';
    	}
    	else
    	{
        echo 'Error';
    	}
    }
    
    ?>
  • vituko
    New Member
    • Dec 2006
    • 48

    #2
    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

    Comment

    • chunk1978
      New Member
      • Jan 2007
      • 224

      #3
      Originally posted by vituko
      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...

      Comment

      • chunk1978
        New Member
        • Jan 2007
        • 224

        #4
        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...

        Code:
        <?php
        
        $uploadfolder = "uploads/";
        $uploadfileA = $uploadfolder . basename($_FILES['datafileA']['name']);
        $uploadfileB = $uploadfolder . basename($_FILES['datafileB']['name']);
        
        if	(move_uploaded_file($_FILES['datafileA']['tmp_name'], $uploadfileA))
        	{ 
        	echo "Successful upload File A.\n\n";
        	}
        	else
        	{
        	echo "File A Empty.\n\n";
        }
        
        if	(move_uploaded_file($_FILES['datafileB']['tmp_name'], $uploadfileB))
        	{ 
        	echo "Successful upload File B.\n\n";
        	}
        	else
        	{
        	echo "File B Empty.\n\n";
        }
        
        ?>

        Comment

        Working...