Upload ảnh lên server là 1 trong vấn đề đâu đầu với đa số lập trình viên .Câu hỏi làm sao người dùng up đúng tài liệu ảnh,làm sao mình rất có thể lấy link hình ảnh đó giữ vào database .Hôm nay mình sẽ có tác dụng 1 lấy ví dụ như nho nhỏ tuổi hướng dẫn chúng ta các cách nhé
Bước 1 : chế tạo cơ sở dữ liệu lưu ảnh gồm id ,images lưu giữ link ảnh và alt lưu ngôn từ ảnh
-- phpMyAdmin SQL Dump-- version 4.5.1-- http://www.phpmyadmin.net---- Host: 127.0.0.1-- Generation Time: Dec 26, năm 2016 at 11:34 PM-- vps version: 10.1.16-MariaDB-- PHP Version: 5.6.24SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";SET time_zone = "+00:00";---- Database: `demoyii2`---- ------------------------------------------------------------ Table structure for table `images`--CREATE TABLE `images` ( `id` int(11) NOT NULL, `images` text NOT NULL, `alt` varchar(500) NOT NULL) ENGINE=InnoDB default CHARSET=utf8;---- Indexes for dumped tables------ Indexes for table `images`--ALTER TABLE `images` địa chỉ PRIMARY KEY (`id`);---- AUTO_INCREMENT for dumped tables------ AUTO_INCREMENT for table `images`--ALTER TABLE `images` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;Tiếp theo chúng ta đến phần models kết nối với database bởi file Images.php vào mục commom cùng với rules như sau
return < <<"images", "alt">, "required">, //<<"images">, "string">, <<"alt">, "string", "max" => 500>, <"images","safe">, <"images", "file", "extensions" => "jpg,jpeg, gif, png,jpeg,bmp">, >;Comment lại đonạ images string thế nó bằng file bạn nhớ nhằm file với extensions là những đuôi về ảnh nhé .Chỗ này tránh người dùng cố ý up shell lên server bọn chúng ta
Tiếp theo trong models Images đó chúng ta tạo 1 function upload()
public function upload(){if ($this->validate()) $alias = Yii::getAlias("
frontend");$link_khong_dau = Images::move_dau($this->images->baseName)."-".(String)time();$path = $alias."/web/uploads/user";FileHelper::createDirectory($path);$this->images->saveAs($path."/".
Bạn đang xem: Lưu đường dẫn ảnh vào database php
$link_khong_dau. "." .Xem thêm: Sửa Lỗi Tiếng Anh Trong Word 2010, Cách Kiểm Tra Lỗi Chính Tả Trong Word Như Thế Nào
$this->images->extension);$path_save_db ="/uploads/user/". $link_khong_dau. "." . $this->images->extension;$models_image = new Images();$models_image->images = $path_save_db;$models_image->alt = $this->alt;$models_image->save();return true; else return false;Để xử lý vấn đề tạo thư mục chúng ta cũng có thể tạo trước hoặc dùng FIleHelper như mình để chế tạo .Nếu như bọn họ là người nước ngoài thì ok rồi nhưng họ thường lưu hình ảnh bằng giờ việt nên đề nghị chuyển hình ảnh up lên từ tất cả dấu thanh lịch không dấu với function move_dau()public function move_dau($str)ửGiờ thì ok rồi kia .Tiếp theo chúng ta sẽ cho phần controller với tệp tin ImagesControllers trong phần frontend nội dung actionCreate()
public function actionCreate() $model = new Images(); if ($model->load(Yii::$app->request->post())) $model->images = UploadedFile::getInstance($model, "images"); if ($model->upload()) return $this->redirect("index"); else return $this->render("create", < "model" => $model, >); Chúng ta vẫn lấy dữ liệu $model->images = UploadedFile::getInstance($model, ‘images’); đầu vào chưa xử lý trải qua việc $models->uploads() sẽ phát triển thành link hình ảnh thành ko dấu với lưu vào folder uploads/user
Còn ở file views họ cần thay đổi 1 chút như sau
Ok vây là dứt giờ xem kế quả nhé


Link thử nghiệm : http://demo.darkedeneurope.com/images/index .
Bạn nghĩ sao về câu hỏi upload hình ảnh trong yii2 ? dễ hay nặng nề .Hãy cho mình biết ý kiến của doanh nghiệp về việc upload ảnh lên vps trong yii2 nhé .Sẽ thật tuyệt vời và hoàn hảo nhất hơn nếu chia sẻ bài viết này đến đồng đội .