Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 635807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:26:44+00:00 2026-05-13T20:26:44+00:00

In my form I am asking for someone to upload a PDF file &

  • 0

In my form I am asking for someone to upload a PDF file & an image. They are both being sent to the same array $_FILES['rfiles'] array. I want to validate $_FILES['rfiles']['type'][0] to “application/pdf” or “text/plain” specifically, and $_FILES['rfiles']['type'][1] to an “image/*” This first part is the working part of the code. I’m trying to use the (Foreach / [$key]) to its fullest extent. I’ve attempted to send the 2 files without using the form NAME='rfiles[]', but then $_FILES has a coniption. So it seems the Foreach method must be used anyway.

foreach ($_FILES['rfiles']['error'] as $key => $error) {
   $tmp_name = $_FILES['rfiles']['tmp_name'][$key];
   if (!$tmp_name) continue;

   $name = basename($_FILES['rfiles']['name'][$key]); #the recycled variable

  #This is where the other code would rest.

   if ($error == UPLOAD_ERR_OK)
{   
    if (file_exists("/restdata/" . $_FILES["rfiles"]["name"][$key])) #Checking for a duplicate filename
        {
  echo $_FILES["rfiles"]["name"][$key] . " is currenty in use. Please rename your file to a unique name and try again.<br />\n";$badul[$key]=1; #badul[] is to be used later in the page in the followup form.
        }
    else
        { 
            if ( move_uploaded_file($tmp_name, "/restdata/".$name) ) {
            $uploaded_array[] .= "Uploaded file '".$name."'.<br/>\n";
                echo "File is valid, and was successfully uploaded.\n<br />\n";
                echo ($_FILES["rfiles"]["type"][$key]. " is a proper format.<br />\n"); #Mostly being used for testing purposes to get the correct MIME. But, how do I validate it?
                echo ($uploaded_array[$key] . "<br>\n");
                }
        else 
                {
        $errormsg .= "Could not move uploaded file '".$tmp_name."' to '".$name."'<br/>\n";  
                } # end if/else move_upoaded File
            } #end if/else file_exists 
} #end if $error == UPLOAD_ERR_OK
else $errormsg .= "Upload error. [".$error."] on file '".$name."'<br/>\n";
    echo ( $errormsg );
} #End Foreach

I have attempted to keep the Foreach loop intact trying 2 different methods, the 1st one would have been optimal because I was going to be abe to keep using the $key but you can’t seem to quite make an array in an array. I removed these from my working script because the errors seemed unpassable

#absolutey predefine these variables before the Foreach statement. 
$allowedExtensions[0] = array("txt", "rtf", "doc", "pdf");
$allowedExtensions[1] = array("gif", "jpg", "jpeg");

       ...
        $file = $_FILES['rfiles'];

        function isAllowedExtension($fileName) {
          global $allowedExtensions[$key]; #tricky tricky?

          return in_array(end(explode(".", $fileName)), $allowedExtensions[$key]);
        }

        if($error == UPLOAD_ERR_OK) {
          if(isAllowedExtension($file['name'][$key]))
        ...

The other method just leaves too many holes because if 1 of the arguments passes, it won’t matter what the 2nd file is:

    ...
if (((($_FILES["rfiles"]["type"][1] == "image/gif")
    || ($_FILES["rfiles"]["type"][1] == "image/jpeg")
    || ($_FILES["rfiles"]["type"][1] == "image/pjpeg"))
    && ($_FILES["rfiles"]["size"][1] < 5000000))
    || ((($_FILES["rfiles"]["type"][0] == "text/plain") #probaby the wrong syntax, but worth a shot.
    || ($_FILES["rfiles"]["type"][0] == "application/pdf")
    || ($_FILES["rfiles"]["type"][0] == "text/rtf"))
    && ($_FILES["rfiles"]["size"][0] < 15000000)))
      {
      if ($error == UPLOAD_ERR_OK)...

I’ve attempted to attack it directly via $_FILES & tried to explode it.. Is there another way?

EDIT: For clarification purposes – I need to be able to handle the file validations separately. Their values are individually being processed into a DB. Not being able to provide the customer with a specific error will create too much confusion on their end.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:26:45+00:00Added an answer on May 13, 2026 at 8:26 pm

    Replace your second method with the following, then it should work as expected :

    ...
    if (((($_FILES["rfiles"]["type"][1] == "image/gif")
        || ($_FILES["rfiles"]["type"][1] == "image/jpeg")
        || ($_FILES["rfiles"]["type"][1] == "image/pjpeg"))
        && ($_FILES["rfiles"]["size"][1] < 5000000))
        && ((($_FILES["rfiles"]["type"][0] == "text/plain") #probaby the wrong syntax, but worth a shot.
        || ($_FILES["rfiles"]["type"][0] == "application/pdf")
        || ($_FILES["rfiles"]["type"][0] == "text/rtf"))
        && ($_FILES["rfiles"]["size"][0] < 15000000)))
      {
      if ($error == UPLOAD_ERR_OK)...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my form I'm asking users to enter a text and then when they
Sorry for asking this again but ppl seem to connect csrf only with form
I'm asking about using a Twitter Bootstrap modal window with a Rails form helper
Instead of asking a client timezone in registration form (to correctly format datetime, all
I'm trying to post a username & password from an HTML form to a
I have a feature whereby a class in a dll displays a form asking
My form does not go to recipient when submitted! I changed the file mail.tpl.txt
I have an events form that allows someone to add a new event. An
I feel pretty stupid for asking this, but I'm doing a form where the
After asking a couple of questions I managed to create this form. I didn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.