tempName); $existing = Image::find()->where([ 'sha256' => $hash ])->one(); if ($existing) { return $existing; } copy($file->tempName, env('UPLOADS_PATH') . '/' . $hash); if (!preg_match('/^[a-z0-9-_]+(|\.[\w\d]+)$/i', Iuliia::translate($file->name, Iuliia::WIKIPEDIA))) { throw new BadRequestHttpException('Filename must be a-Z0-9_-!'); } $new = new Image([ 'sha256' => $hash, 'original_name' => Iuliia::translate($file->name, Iuliia::WIKIPEDIA), 'mime' => FileHelper::getMimeType($file->tempName) ]); $new->save(true); return $new; } /** * {@inheritdoc} */ public function rules() { return [ [['sha256', 'original_name'], 'required'], [['original_name'], 'match', 'pattern' => '/^[a-z0-9-_]+(|\.[\w\d]+)$/i'], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'sha256' => 'Sha256', 'original_name' => 'Original Name', ]; } }