简体   繁体   中英

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

Have been trying to add some codes into the old written program, which i did not write because i have no idea how good this code might be working. The following codes acts in two different pages with 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 the upload destination folder
        //$destination = realpath('../../app/webroot/img/gallery') . '/';

        $bigimg = WWW_ROOT . 'img/gallery/big/';
        $smallimg = WWW_ROOT . 'img/gallery/small/';

        // grab the file
        $file = $this->data['Gallery']['photofile'];
        $imageTypes = array("image/gif", "image/jpeg", "image/png"); //List of accepted file extensions. 
        foreach ($iamgeTypes as $type) {                 //check if image type fits one of allowed types
            if ($type == $this->data['type']) {
                // upload the image using the 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 to get mime type of uploaded file. There are two ways to do this depending on your 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.
// get 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 about fileinfo functions you can find in the documentation .

PHP 5.3 <

// get file mime type
$fileType = mime_content_type($file);

Documentation for mime_content_type() function.

I don't know how your model looks like, but it's good approach to move checking mime type in validator.

$file = $this->data["Gallery"]['photofile'];
if(!empty($file['tmp_name']))
{
    $type = explode('/', $file['type']);
    if($type[0] == 'image')
    {
     // Your Code
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM