SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    canada
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Class and method processing

    Hello,

    I'm working on a class file where I like each method called and executed within the class.
    In the event a method or methods run into a problem it should update the error array with the
    appropriate err msgs.

    the last method would verify whether error array is empty or not
    if empty process the upload if not display the content of the error array.

    This posting is more or less how to run all 4 methods within the class or have the last method
    process the 3 methods .

    $upload = new Testing(var)
    $upload->processimage() - should either bring back the err message or complete the upload process pending it meets all
    of the critters set by the other methods.


    Markup code/class


    Class Testing
    {

    public errCheck = array();

    public function recordCount($_FILES['data'])
    { if(!count($_FILES['data']['name'] ) <= $file_Count)
    { $this->errCheck[] = 'The system only allows your to upload 3 images/files'; }
    }


    public function checkimageFormat($_FILES['data'])
    { if (!jpg, png, gif)
    { Add message to errcheck[] = "incorrect format"; }
    }

    public function checkimageSize($_FILES['data'])
    { if (!jpg, png, gif)
    { Add message to errcheck[] = "incorrect size"; }
    }


    public function processimage()
    { -run the above methods
    -check errCheck array to determine if the methods appended any err msgs.

    if empty upload files.
    if not empty send error message back
    }



    }

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If the only variable is $_FILES['data'] it needs not be passed to the class or to its functions because it is global.

    I prefer to have a second boolean error variable

    public $error = FALSE;

    it's easier to test than to see what's inside an array.

    To call the functions inside the class use
    PHP Code:
    // -run the above methods
    Testing::recordCount();
    Testing::checkimageFormat();
    Testing::checkimageSize();

    // -check errCheck array to determine if the methods appended any err msgs.
    if ($this->error) {
       return 
    $this->errCheck;
    } else {
       return 
    FALSE;

    Denny Schlesinger
    web services

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I'd try and make that class a bit more flexible.

    What happens if you want to reuse it to check say, 4 files max, and only jpgs?

    Think on designing something which handles this kind of call instead:
    PHP Code:

    $file_types 
    = array("jpg""png""gif");
    $count 3;

    $t = new Testing($count$file_types);
    if( 
    $t->process() ){  // ie process() return boolean
    // all went ok

    }else{

    // something went wrong
    var_dump($t->getErrors());


    This allows you to then, elsewhere, or at a later stage to do:

    PHP Code:
    $file_types "jpg";
    $count 4;
    $t = new Testing($count$file_types);
    ... 
    etc 
    I am not sure but I think your Q is asking how do I call those methods from inside the class, so returning to your code:

    PHP Code:
    public function processimage(){
    $this->checkimageFormat();
    $this->checkimageSize();


    In any case a single class which handles uploading, moving the file, checking the size and the file type is going to get pretty big pretty fast. The trick is to remove as many variables from inside it so that you can inject them instead -- that way you have a chance of re-using it in a variety of places.

    eg what if you wanted to upload a pdf file?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •