开发者

Is it possible to set 2 different ['type'] validations while getting them both from the same $_FILES array?

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 f开发者_Go百科ormat.<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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜