4\. Multiple files upload OOP, namespaces & How to recognize mobile device – OOP, SPA, MVC domain style, PHP outside web doc root ================================================================================================================================= **[HOME](http://phporacle.altervista.org/php-oracle-main-menu/ "Main menu")** Two scripts for multiple files upload – OOP, namespaces (see also article 10) ----------------------------------------------------------------------------- setMaxSize($max); $loader->allowAllTypes(); $loader->upload(); $result = $loader->getMessages(); } catch (Exception $e) { echo $e->getMessage(); } } ?> Multiple Upload '; foreach ($result as $message) { echo "
  • $message
  • "; } echo ''; } ?>

    Upload info ($\_FILES)

    
    Error levels in $\_FILES array $err = $\_FILES\['file'\]\['error'\];
    Error level Meaning
    0 Upload successful $err = UPLOAD\_ERR\_OK
    1 File exceeds maximum upload size specified in php.ini (default 2 MB)
      UPLOAD\_ERR\_INI\_SIZE
      upload\_max\_filesize in php.ini eg 6400M (=6,4 GB)
      post\_max\_size = 64000M
      file\_uploads = On  (see Local value in phpinfo -> Core section)
      upload\_tmp\_dir = "j:/wamp/tmp"  or whatever, default C:\\Windows\\Temp
      max\_file\_uploads = 20  for single request
      max\_execution\_time  60 sekundi
    max\_input\_time 60 sek. (parse $\_POST, $\_GET, $\_FILE arrays) 2 File exceeds size specified by MAX\_FILE\_SIZE in HTML form eg 6400M UPLOAD\_ERR\_FORM\_SIZE 3 File only partially uploaded UPLOAD\_ERR\_PARTIAL 4 Form submitted with no file specified UPLOAD\_ERR\_NO\_FILE Error level 5 is currently not defined. 6 No temporary folder UPLOAD\_ERR\_NO\_TMP\_DIR (no destination\_path) 7 Cannot write file to disk UPLOAD\_ERR\_CANT\_WRITE (chmod 777) 8 Upload stopped by an unspecified PHP extension UPLOAD\_ERR\_EXTENSION

    $\_POST parameters of this page

    $value) { $count++ ?> '; ?> destination = $path; } public function upload($renameDuplicates = true) { $this->renameDuplicates = $renameDuplicates; $uploaded = current($\_FILES); if (is\_array($uploaded\['name'\])) { // deal with multiple uploads foreach ($uploaded\['name'\] as $key => $value) { $currentFile\['name'\] = $uploaded\['name'\]\[$key\]; $currentFile\['type'\] = $uploaded\['type'\]\[$key\]; $currentFile\['tmp\_name'\] = $uploaded\['tmp\_name'\]\[$key\]; $currentFile\['error'\] = $uploaded\['error'\]\[$key\]; $currentFile\['size'\] = $uploaded\['size'\]\[$key\]; if ($this->checkFile($currentFile)) { $this->moveFile($currentFile); } } } else { if ($this->checkFile($uploaded)) { $this->moveFile($uploaded); } } } public function getMessages() { return $this->messages; } public function getMaxSize() { return number\_format($this->max/1024, 1) . ' KB'; } public function setMaxSize($num) { if (is\_numeric($num) && $num > 0) { $this->max = (int) $num; } } public function allowAllTypes($suffix = true) { $this->typeCheckingOn = false; if (!$suffix) { $this->suffix = ''; // empty string } } protected function checkFile($file) { $accept = true; if ($file\['error'\] != 0) { $this->getErrorMessage($file); // stop checking if no file submitted if ($file\['error'\] == 4) { return false; } else { $accept = false; } } if (!$this->checkSize($file)) { $accept = false; } if ($this->typeCheckingOn) { if (!$this->checkType($file)) { $accept = false; } } if ($accept) { $this->checkName($file); } return $accept; } protected function getErrorMessage($file) { switch($file\['error'\]) { case 1: case 2: $this->messages\[\] = $file\['name'\] . ' is too big: (max: ' . $this->getMaxSize() . ').'; break; case 3: $this->messages\[\] = $file\['name'\] . ' was only partially uploaded.'; break; case 4: $this->messages\[\] = 'No file submitted.'; break; default: $this->messages\[\] = 'Sorry, there was a problem uploading ' . $file\['name'\]; break; } } protected function checkSize($file) { if ($file\['error'\] == 1 || $file\['error'\] == 2) { return false; } elseif ($file\['size'\] == 0) { $this->messages\[\] = $file\['name'\] . ' is an empty file.'; return false; } elseif ($file\['size'\] > $this->max) { $this->messages\[\] = $file\['name'\] . ' exceeds the maximum size for a file (' . $this->getMaxSize() . ').'; return false; } else { return true; } } protected function checkType($file) { if (in\_array($file\['type'\], $this->permitted)) { return true; } else { if (!empty($file\['type'\])) { $this->messages\[\] = $file\['name'\] . ' is not permitted type of file.'; } return false; } } protected function checkName($file) { $this->newName = null; $nospaces = str\_replace(' ', '\_', $file\['name'\]); if ($nospaces != $file\['name'\]) { $this->newName = $nospaces; } $extension = pathinfo($nospaces, PATHINFO\_EXTENSION); if (!$this->typeCheckingOn && !empty($this->suffix)) { if (in\_array($extension , $this->notTrusted) || empty($extension)) { $this->newName = $nospaces . $this->suffix; } } if ($this->renameDuplicates) { $name = isset($this->newName) ? $this->newName : $file\['name'\]; $existing = scandir($this->destination); if (in\_array($name, $existing)) { // rename file $basename = pathinfo($name, PATHINFO\_FILENAME); $extension = pathinfo($name, PATHINFO\_EXTENSION); $i = 1; do { $this->newName = $basename . '\_' . $i++; if (!empty($extension)) { $this->newName .= ".$extension"; } } while (in\_array($this->newName, $existing)); } } } protected function moveFile($file) { $filename = isset($this->newName) ? $this->newName : $file\['name'\]; $success = move\_uploaded\_file($file\['tmp\_name'\] , $this->destination . $filename); if ($success) { $result = $file\['name'\] . ' was uploaded successfully to ' . 'server directory :
    '. $this->destination ; if (!is\_null($this->newName)) { $result .= ', and was renamed ' . $this->newName; } $this->messages\[\] = $result; } else { $this->messages\[\] = 'Could not upload ' . $file\['name'\]; } } } How to recognize mobile device – non OOP code --------------------------------------------- Excellent code to learn object oriented programming, model-view-controller code separation (structured programming), single page application (require scripts, not URL them), most code outside Apache doc root. `function findDevice() { $userAgent=strtolower($_SERVER['HTTP_USER_AGENT']); $device=array('iphone','ipad','android','silk','blackberry', 'touch'); $deviceLength=count($device); for($ii=0;$ii < $deviceLength;$ii ++) { if(strstr($userAgent, $device[$ii])) { return $device[$ii]; } else return 'desktop/laptop'; } }` OOP, SPA, MVC domain style, PHP outside web doc root ---------------------------------------------------- Display ibrowser properties (view class methode): 1. Device =**desktop/laptop** 2. Browser = firefox 3. userAgent = mozilla/5.0 (windows nt 6.3; win64; x64; rv:25.3) gecko/20150323 firefox/31.9 palemoon/25.3.1 Or display one property – returned from getter: Device = desktop/laptop About OOP programs ------------------ 1. **CODE INSIDE APACHE DOC ROOT (this page script)**:[ kod](http://dev:8083/inc/utl/showsource.php%20%20%20%20%20%20?file=J:\dev_web\htdocs\inc\utl\get_ibrowser_device.php &line=1&prev=10000&next=10000)[ edit](http://dev:8083/inc/utl/edservertxt.php%20%20%20%20%20%20?file=J:\dev_web\htdocs\inc\utl\get_ibrowser_device.php)[ phpinfo ](http://dev:8083/phpinfo_inc.php)**INCLUDED CODE FROM OUTSIDE APACHE DOC ROOT**: 2. code behind this page script (model .pcls) :[ kod](http://dev:8083/inc/utl/showsource.php%20%20%20%20%20%20?file=J:\dev_web\inc\utl\get_ibrowser_device.pcls &line=1&prev=10000&next=10000)[ edit](http://dev:8083/inc/utl/edservertxt.php%20%20%20%20%20%20?file=J:\dev_web\inc\utl\get_ibrowser_device.pcls)[ phpinfo](http://dev:8083/phpinfo_inc.php) 3. code config – set up :[ kod](http://dev:8083/inc/utl/showsource.php%20%20%20%20%20%20?file=J:\dev_web\inc\confglob.php &line=1&prev=10000&next=10000)[ edit](http://dev:8083/inc/utl/edservertxt.php%20%20%20%20%20%20?file=J:\dev_web\inc\confglob.php)[ phpinfo](http://dev:8083/phpinfo_inc.php) 4. code helper (util) :[ kod](http://dev:8083/inc/utl/showsource.php%20%20%20%20%20%20?file=J:\dev_web\inc\utl\kod_edit_run.php &line=1&prev=10000&next=10000)[ edit](http://dev:8083/inc/utl/edservertxt.php%20%20%20%20%20%20?file=J:\dev_web\inc\utl\kod_edit_run.php)[ phpinfo](http://dev:8083/phpinfo_inc.php) 5. /\*\* \* This page URL $idxurl = \* [http://dev:8083/inc/utl/get\_ibrowser\_device.php](http://dev:8083/inc/utl/get_ibrowser_device.php) \* displays o u t p u t o f server script \* $idx.DS.$idxscript = \* J:\\dev\_web\\htdocs\\inc\\utl\\get\_ibrowser\_device.php, \* which contains : \* MODEL\_fn\_call from public fn \_\_ c o n s t r u c t ( ) \* protected\_VIEW\_fn\_call from public fn \* CONTROLLER\_code before c l a s s C l i e n t \* \* server script i n c l u d e s scripts which are outside Apache doc root : \* 1. config $confglob : require\_once('J:\\dev\_web\\inc\\confglob.php'); \* 2. helper (util) $edrun : require\_once('J:\\dev\_web\\inc\\utl\\kod\_edit\_run.php'); \* 3. code behind (class) $codebehind : require\_once('J:\\dev\_web\\inc\\utl\\get\_ibrowser\_device.pcls'); \* \* $confglob contains P H P s e t u p : \* ini\_set('display\_errors','2'); \* ERROR\_REPORTING(E\_ALL); \*\*/ **SCRIPTS:** 1. **J:\\dev\_web\\htdocs\\inc\\utl\\get\_ibrowser\_device.php **`// 1. a d r e s s e s : if (!defined('DS')) define('DS',DIRECTORY_SEPARATOR); if (!defined('CONFGLOB_DIR')) define('CONFGLOB_DIR', realpath($_SERVER['DOCUMENT_ROOT'].'/../inc'));`$apl = dirname(dirname(\_\_DIR\_\_)).DS.’htdocs’; // \*\*\* !!! \*\*\* ONLY YOU TO SET UP, used for link $idx = \_\_DIR\_\_ ; $idxscript = basename(\_\_FILE\_\_) ;// 2. i n c l u d e s : $confglob = CONFGLOB\_DIR.DS.’confglob.php’; $edrun = CONFGLOB\_DIR.DS.’utl’.DS.’kod\_edit\_run.php’; $codebehind = CONFGLOB\_DIR.DS.’utl’.DS.’get\_ibrowser\_device.pcls’; require\_once($confglob); // c o n f i g require\_once($edrun); // h e l p e r require\_once($codebehind); // m o d e l// 3. c o n t r o l l e r : $trigger = new Client(); // trigger becouse can direct output view// \*\*\*\*\*\*\*\*\*\*\*\*\*\* e n d c o n t r o l l e r p r o g r a m// v i e w : ?> Is mobile

    How to recognize mobile device – non OOP code

    function findDevice() {
    $userAgent=strtolower($\_SERVER\[’HTTP\_USER\_AGENT'\]);
    $device=array(’iphone',’ipad',’android',’silk',’blackberry', ’touch');
    $
    deviceLength=count($device);

    for(
    $ii=0;$ii < $deviceLength;$ii ++) {
    if(
    strstr($userAgent, $device\[$ii\])) {
    return $device\[$ii\];
    } else return
    ’desktop/laptop';
    }
    }

    OOP, SPA, MVC domain style, PHP outside web doc root

    Display ibrowser properties (view class methode):
    out\_vew\_ibrowse\_params() ; ?>

    Or display one property – returned from getter: Device = get\_device(); ?>

    About OOP programs

    1. CODE INSIDE APACHE DOC ROOT (this page script) :

      INCLUDED CODE FROM OUTSIDE APACHE DOC ROOT :
    2. code behind this page script (model .pcls) :
    3. code config – set up :
    4. code helper (util) :
    5. /\*\*  
          \* This page URL $idxurl = ”>  
          \*   displays o u t p u t  o f  server script $idx.DS.$idxscript =  
          \*   , which contains :  
          \*      MODEL\_fn\_call from public fn \_\_ c o n s t r u c t ( )  
          \*      protected\_VIEW\_fn\_call from public fn  
          \*      CONTROLLER\_code before c l a s s  C l i e n t  
          \*  
          \* server script i n c l u d e s scripts which are outside Apache doc root :  
          \*   1. config $confglob                : require\_once(‘’);  
          \*   2. helper (util) $edrun            : require\_once(‘’);  
          \*   3. code behind (class) $codebehind : require\_once(‘’);  
          \*  
          \* $confglob contains  P H P  s e t  u p :  
          \*    ini\_set(‘display\_errors’,’2′);  
          \*    ERROR\_REPORTING(E\_ALL);  
          \*\*/ 
    IbrowserProp = new getIbrowserProp(); } // e n d p u b l i c f n \_ \_c o n s t r u c t ( ) public function get\_device() { return $this->IbrowserProp->findDevice() ; } // e n d // V I E W : public function out\_vew\_ibrowse\_params() { $this->vew\_ibrowse\_params(); } // e n d protected function vew\_ibrowse\_params() // public private protected { echo ‘
      ’; echo ‘
    1. Device = ‘ . $this->IbrowserProp->findDevice() . ‘
      ’; echo ‘
    2. Browser = ‘ . $this->IbrowserProp->findBrowser() . ‘
      ’; echo ‘
    3. userAgent = ‘ . $this->IbrowserProp->getUserAgent() . ‘
      ’; echo ‘
    ’; } // e n d p u b l i c f n \_ \_c o n s t r u c t ( ) } // e n d c l a s s C l i e n t ?> 2. **J:\\dev\_web\\inc\\utl\\get\_ibrowser\_device.pcls `userAgent=strtolower($\_SERVER\[‘HTTP\_USER\_AGENT’\]); //$this->userAgent=strtolower($this->userAgent);$this->device=array(‘iphone’,’ipad’,’android’,’silk’,’blackberry’, ‘touch’); $this->browser= array(‘firefox’,’chrome’,’opera’,’msie’,’safari’,’blackberry’,’trident’); $this->deviceLength=count($this->device); $this->browserLength=count($this->browser); } public function findDevice() { for($ii=0;$ii < $this->deviceLength;$ii ++) { if(strstr($this->userAgent,$this->device\[$ii\])) { //$device = $this->device\[$ii\]; //if (is\_null($device)) $device = ‘desktop/laptop’; //return $device; return $this->device\[$ii\]; } else return ‘desktop/laptop’; } }//public function get\_device() { // return $this->vew\_ibrowse\_params(); //} // e n dpublic function findBrowser() { for($ii=0;$ii < $this->browserLength;$ii ++) { if(strstr($this->userAgent,$this->browser\[$ii\])) { return $this->browser\[$ii\]; } } } public function getUserAgent() { return($this->userAgent); } }?> 3. **J:\\dev\_web\\inc\\confglob.php ` kod
    `** edit phpinfo EOKOD; /\* call it so: kod\_edit\_run( $idx // script\_dir\_path , $idxscript // script\_name , MDURL); // web\_docroot\_url = (Apache) web server URL \*/ } kod[ edit](”$web_docroot_url/inc/utl/edservertxt.php)[ phpinfo](”$web_docroot_url/phpinfo_inc.php) EOKOD; /\* call it so: kod\_edit\_run( $idx // script\_dir\_path , $idxscript // script\_name , MDURL); // web\_docroot\_url = (Apache) web server URL \*/ }
    Parameter name Value