以新闻为例:首先在新闻的ActiveRecord添加规则:
[[‘image‘], ‘file‘, ‘extensions‘ => ‘png, jpg, gif‘],
再定义上传路径
由于我把入口文件移动到了web目录下,目录结构如下:
index.php
<?php defined(‘YII_DEBUG‘) or define(‘YII_DEBUG‘, true); defined(‘YII_ENV‘) or define(‘YII_ENV‘, ‘dev‘); require(__DIR__ . ‘/../vendor/autoload.php‘); require(__DIR__ . ‘/../vendor/yiisoft/yii2/Yii.php‘); require(__DIR__ . ‘/../common/config/bootstrap.php‘); require(__DIR__ . ‘/../frontend/config/bootstrap.php‘); $config = yii\helpers\ArrayHelper::merge( require(__DIR__ . ‘/../common/config/main.php‘), require(__DIR__ . ‘/../common/config/main-local.php‘), require(__DIR__ . ‘/../frontend/config/main.php‘), require(__DIR__ . ‘/../frontend/config/main-local.php‘) ); (new yii\web\Application($config))->run();
admin/index.php
<?php defined(‘YII_DEBUG‘) or define(‘YII_DEBUG‘, true); defined(‘YII_ENV‘) or define(‘YII_ENV‘, ‘dev‘); require(__DIR__ . ‘/../../vendor/autoload.php‘); require(__DIR__ . ‘/../../vendor/yiisoft/yii2/Yii.php‘); require(__DIR__ . ‘/../../common/config/bootstrap.php‘); require(__DIR__ . ‘/../../backend/config/bootstrap.php‘); $config = yii\helpers\ArrayHelper::merge( require(__DIR__ . ‘/../../common/config/main.php‘), require(__DIR__ . ‘/../../common/config/main-local.php‘), require(__DIR__ . ‘/../../backend/config/main.php‘), require(__DIR__ . ‘/../../backend/config/main-local.php‘) ); (new yii\web\Application($config))->run();
上传路径: common/bootstrap.php
Yii::setAlias(‘@uploads‘, dirname(dirname(__DIR__)) . ‘/web/uploads‘);
同时定义了跟目录:
Yii::setAlias(‘@mywebroot‘, dirname(dirname(__DIR__)) . ‘/web‘);
上传图片类:common/models/UploadForm.php
<?php namespace common\models; use Yii; use yii\base\Model; use yii\web\UploadedFile; /** * This is the model class for upload. * * @property string $path */ class UploadForm extends Model{ /** * 上传图片 * @image_type common, news, product, cases, ad, category, downloads, friend_link * */ public static function uploads($upfile, $old_image = ‘‘, $image_type = ‘common‘){ $upload_time = date("Ymd"); $upload_type_path = $image_type . ‘/‘ . $upload_time . ‘/‘; //上传路径 $upload_save_path = Yii::getAlias(‘@uploads‘) . ‘/‘ . $upload_type_path; if(!file_exists($upload_save_path)){ @mkdir($upload_save_path); } //保存路径 $upload_save_url = yii::$app->params[‘uploads_dir‘] . ‘/‘ . $upload_type_path; $uploaded_file=UploadedFile::getInstanceByName($upfile); if($uploaded_file === null || $uploaded_file->hasError){ return $old_image; } //图片名称 $file_name = $uploaded_file->getBaseName(); //图片格式 $file_ext = $uploaded_file->getExtension(); //新文件名 $new_file_name=date("YmdHis") . rand(10000,99999). ‘.‘ .$file_ext; //删除旧文件 if(!empty($old_image)){ @unlink(Yii::getAlias(‘@mywebroot‘) . ‘/‘ . $old_image); } $uploaded_file->saveAs($upload_save_path . $new_file_name); return $upload_save_url . $new_file_name; } } ?> 然后再新闻的ActiveRecord中保存前操作: public function beforeSave($insert) { $old_image = Yii::$app->request->post()[‘old_image‘]; $uploaded_file=UploadForm::uploads("News[image]", $old_image, ‘news‘); $this->image = $uploaded_file; return parent::beforeSave($insert); }
时间: 2024-09-30 09:27:04