This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 2

limit attachment types


  1. merriwhether
    Member

    Hi,

    I'm looking for a script to add attachments to a formmail and stumbled across this site.

    This script actually looks great though I have one concern. I've already done a cursory search of the forums for this question but it the only place it was mentioned didn't answer my question, so here I am, asking my question! :)

    Right, I need to be able to upload .doc/.pdf/.txt files ONLY. Can I define allowed file types using this script?

    Thanks in advance.

    Posted 16 years ago #
  2. yuniar

    Hi,

    At this moment, there is no built-in feature to restrict the file type of the attachments.
    However, with some small modification, this can be done.

    Let me know if you need it and I'll send you the code, or I'll just post it here.

    In the future, this feature should be added to the form builder.


    MachForm Founder

    Posted 16 years ago #
  3. merriwhether
    Member

    Thanks for your post, unfortunately as I was under time constraints to get the form up and running I chose an open source option

    Not ideal as I had to build the form in the 'traditional' way but it got the job done.

    Perhaps you'd consider making the code publicly viewable so if anyone comes along and needs to have this feature before making a purchase, the information would be here and you won't have lost another sale! :)

    I will be watching this product though as it looks to be very good indeed and I may have future need of it, so thanks in advance and best of luck with the product!

    Posted 16 years ago #
  4. yuniar

    ooops..sorry for the late response then.

    ok, if anyobody need this, all you need to do is to edit your includes/post-functions.php file.

    Search around line 811 and you'll find this code:

    //if validation passed, store uploaded file info into array
    if($_FILES[$element_name]['size'] > 0){
    	$uploaded_files[] = $element_name;
    }


    Replace that code with this one:

    //start file type checking -----------------
    $allowed_file_types = array("doc","pdf","txt");                                      
    
    if(!empty($_FILES[$element_name]['name'])){
    	$file_name = strtolower($_FILES[$element_name]['name']);
    	$file_ext  =  substr($file_name, strrpos($file_name, '.') + 1);                                              
    
    	if(!in_array($file_ext,$allowed_file_types)){
    		$error_elements[$element_id] = "Sorry this filetype is not allowed.";
    	}else{
    		//if validation passed, store uploaded file info into array
    		if($_FILES[$element_name]['size'] > 0){
    			$uploaded_files[] = $element_name;
    		}
    	}
    }
    //end file type checking -------------------


    and you are set.

    The code above would limit the file upload to *.doc, *.pdf and *.txt
    To limit another file type, simply change this line:

    $allowed_file_types = array("doc","pdf","txt");


    MachForm Founder

    Posted 16 years ago #
  5. Page
    Member

    This was just what I was looking for but unfortunatly, I got an error from it. It breaks the form with the notice below.

    "Parse error: syntax error, unexpected $end in /home/myaddress/includes/post-functions.php on line 2906"

    Is there a tweek that will fix this?

    Thanks!

    Posted 13 years ago #
  6. yuniar

    What version of MachForm are you using? As of version 2, there is no need to do the above hack. You only need to modify config.php file to restrict file types.


    MachForm Founder

    Posted 13 years ago #

RSS feed for this topic

Reply