Jump to content

Recommended Posts

I'm working on a two-file upload form and want to be able to check the mime types of two filenames in a string variable.

The code below was working when the variable @resume_path contained just one filename. How can I modify this to accommodate two files?

 

when I echo $resume_path, I get, for example:  cover_letter_centerline.doc, cover_letter_ctg.doc

 

Thanks!   

    //DO NOT TRUST $_FILES['upfile']['mime'] VALUE !!
    //Check MIME Type by yourself.
     $finfo = new finfo(FILEINFO_MIME_TYPE);
     if (false === $ext = array_search(
         $finfo->file($_FILES['resume_path']['tmp_name']),
         array(
            'txt' => 'text/plain',
            'doc' => 'application/msword',
            'pdf' => 'application/pdf',
         ),
         true
     )) {
         throw new RuntimeException('Invalid file format.');
     }
     echo "Sorry, invalid file format.  Please try again.";
     echo $ext;

I tried wrapping my code in a for loop as outlined above and I couldn't get it to work.  So I'm trying Plan B.

$fileTypeExt = pathinfo($path1,PATHINFO_EXTENSION);
    echo $fileTypeExt . "<br/>";
    
    $fileTypeExt2 = pathinfo($path2,PATHINFO_EXTENSION);
    echo $fileTypeExt2 . "<br/>";
    
    // Allow certain file formats
    if($fileTypeExt == "txt" || $fileTypeExt == "doc" || $fileTypeExt == "pdf") {
        echo "Your file extensions are allowed.";
        $uploadOk = 1;
    }
    else {
        echo "Sorry, only TXT, PDF, & DOC files are allowed. <br />";
        $uploadOk = 0;
    }
    
    // Allow certain file formats
    if($fileTypeExt2 == "txt" || $fileTypeExt2 == "doc" || $fileTypeExt2 == "pdf") {
        echo "Your file extensions are allowed.";
        $uploadOk2 = 1;
    }
    else {
        echo "Sorry, only TXT, PDF, & DOC files are allowed. <br />";    
        $uploadOk2 = 0;
    }

Plan B works, but I was wondering if there's a more compact way to write this.  I frequently get tripped up on how to extend working logic

to additional cases.  For instance, do I need a $fileTypeExt and $fileTypeExt2 variables?  Same with $uploadOk and $uploadOk2?  I guess a switch

statement would work if I had many cases.

 

Perhaps one answer is to make a giant conditional, e.g.,

if($fileTypeExt == "txt" || $fileTypeExt == "doc" || $fileTypeExt == "pdf") && ($fileTypeExt2 == "txt" || $fileTypeExt2 == "doc" || $fileTypeExt2 == "pdf")...but I didn't know if that was the best approach.  It would remove the need for two status variables ($uploadOk and $uploadOk2).

Thanks in advance for your ideas.

your goal should be to NOT repeat code, but to reuse the same, identical code. also, by creating a series of numbered variables, you must now type out or copy/paste/overtype the code for each additional set of data. you should instead use arrays for sets of same meaning data that will be processed in the same way.

 

the looping that hansford showed is for an array of uploaded files. the php.net upload handling documentation shows how to do this. see example #3 at this link - http://php.net/manual/en/features.file-upload.post-method.php

 

as to the validation in the php code, since this is user submitted data, each possible error should be checked separately, for each file, and you should give the visitor as much information as possible about what file and what error occurred with that file.

 

any validation error messages you produce should be added as elements to an array, so that you build a set of all the validation errors. you can then simply loop over this array of error messages at the point where you want to display them.

 

short-answer, use arrays when dealing with sets of data, i.e. sets of input uploaded files, sets of validation error messages, ...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.