我一直在尝试将一些代码添加到旧的编写程序中,但我没有编写这些代码,因为我不知道这些代码的工作效果如何。以下代码以不同的形式出现在两个不同的页面中。
$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'));
}
}
}
}
您需要获取上传文件的mime类型。根据您的PHP版本,有两种方法可以做到这一点。
PHP 5.3>=,PECL文件信息>=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'));
}
有关fileinfo
函数的更多信息,请参阅文档。
PHP 5.3<
// get file mime type
$fileType = mime_content_type($file);
mime_content_type()
功能的文档。
我不知道您的模型是什么样子的,但这是在验证器中移动检查mime类型的好方法。
$file = $this->data["Gallery"]['photofile'];
if(!empty($file['tmp_name']))
{
$type = explode('/', $file['type']);
if($type[0] == 'image')
{
// Your Code
}
}