php - CakePhp, How do i add to check if the uploaded file is image only? -


have been trying add codes old written program, did not write because have no idea how code might working. following codes acts in 2 different pages different forms.

$type = $this->data['gallery']['type'];  if (!empty($this->data)) {     if (!isset($this->data['gallery']['gallery_category_id'])) {         if ($this->data['gallery']['type'] == 1) {             echo "<script>alert('" . infogallerysection . "')</script>";         } elseif ($this->data['gallery']['type'] == 2) {             echo "<script>alert('" . infoshrotsection . "')</script>";         } else {         }     } else {         // set upload destination folder         //$destination = realpath('../../app/webroot/img/gallery') . '/';          $bigimg = www_root . 'img/gallery/big/';         $smallimg = www_root . 'img/gallery/small/';          // grab file         $file = $this->data['gallery']['photofile'];         $imagetypes = array("image/gif", "image/jpeg", "image/png"); //list of accepted file extensions.          foreach ($iamgetypes $type) {                 //check if image type fits 1 of allowed types             if ($type == $this->data['type']) {                 // upload image using upload component                 $result = $this->upload->upload($file, $bigimg, null, array('type' => 'resize', 'size' => '965', 'output' => 'jpg'));                 $result = $this->upload->upload($file, $smallimg, null, array('type' => 'resize', 'size' => '146', 'output' => 'jpg'));             }         }     } } 

you need mime type of uploaded file. there 2 ways depending on php version.

php 5.3 >=, pecl fileinfo >= 0.1.0

$file = $this->data['gallery']['photofile']; $imagetypes = array("image/gif", "image/jpeg", "image/png"); //list of accepted file extensions. // file mime type $fileinfo = finfo_open(fileinfo_mime_type); $filetype = finfo_file($fileinfo, $file); finfo_close($fileinfo);  if (in_array($filetype, $imagetypes)) {     $result = $this->upload->upload($file, $bigimg, null, array('type' => 'resize', 'size' => '965', 'output' => 'jpg'));     $result = $this->upload->upload($file, $smallimg, null, array('type' => 'resize', 'size' => '146', 'output' => 'jpg')); } 

more fileinfo functions can find in documentation.

php 5.3 <

// file mime type $filetype = mime_content_type($file); 

documentation mime_content_type() function.

i don't know how model looks like, it's approach move checking mime type in validator.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -