【发布时间】:2026-02-08 16:10:01
【问题描述】:
一直在尝试将一些代码添加到旧编写的程序中,我没有编写这些代码,因为我不知道这段代码可能工作得有多好。 以下代码以不同的形式在两个不同的页面中起作用。
$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'));
}
}
}
}
【问题讨论】:
-
使用 cakephp 验证
-
public $validate = array( 'image' => array( 'rule' => array( 'extension', array('gif', 'jpeg', 'png', 'jpg') ), 'message' => '请提供有效图片。' ) );
标签: php validation cakephp mime-types fileinfo