Php tải xuống nhiều hình ảnh

Chức năng Quản lý sản phẩm, các bạn làm tương tự như chức năng Back-end. Quản lý chuyên mục mình đã làm trước đó

Tải lên nhiều là một trong những tính năng được sử dụng khá nhiều trong trang web ứng dụng. Phần này mình sẽ hướng dẫn xây dựng chức năng upload ảnh cho sản phẩm

Trong bài viết này, mình sẽ sử dụng

php artisan make:controller Admin\AdminProductController
4. Nó là một thư viện đơn giản để tạo chức năng tải tệp lên theo định dạng "Kéo và thả". Khác với thư viện javascript khác, Dropzone hỗ trợ nhiều tùy chọn khác nhau

Xem trước. Chức năng upload nhiều hình ảnh cho sản phẩm.

Php tải xuống nhiều hình ảnh

Vâng. Bây giờ chúng ta sẽ bắt đầu

Bước 1. Thêm tuyến đường

Route::resource('product', 'AdminProductController');
Route::post('uploadImg', 'AdminUploadController@postImages'); 

Bước 2. Thêm phương pháp điều khiển

php artisan make:controller Admin\AdminProductController

File dung

php artisan make:controller Admin\AdminProductController
5

class AdminProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $this->data['title'] = 'List products';
        $productsInfo = DB::table('products')
            ->orderBy('id', 'desc')
            ->paginate(10);
        $this->data['listProduct'] = $productsInfo;
        return view('admin.product.index', $this->data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $this->data['title'] = 'Add new product';
        $listCate = DB::table('categories')->orderBy('id', 'desc')->get();
        $this->data['listCate'] = $listCate;
        return view('admin.product.create', $this->data);
    }

php artisan make:controller Admin\AdminUploadController

Bộ điều khiển này để xử lý tải lên, xóa hình ảnh = ajax

Nội dung

php artisan make:controller Admin\AdminProductController
6

    ajax()) {
            if ($request->hasFile('file')) {
                $imageFiles = $request->file('file');
                // set destination path
                $folderDir = 'uploads/products';
                $destinationPath = base_path() . '/' . $folderDir;
                // this form uploads multiple files
                foreach ($request->file('file') as $fileKey => $fileObject ) {
                    // make sure each file is valid
                    if ($fileObject->isValid()) {
                        // make destination file name
                        $destinationFileName = time() . $fileObject->getClientOriginalName();
                        // move the file from tmp to the destination path
                        $fileObject->move($destinationPath, $destinationFileName);
                        // save the the destination filename
                        $prodcuctImage = new ProductImage;
			            $ProdcuctImage->image_path = $folderDir . $destinationFileName;
			            $prodcuctImage->title = $originalNameWithoutExt;
			            $prodcuctImage->alt = $originalNameWithoutExt;
			            $prodcuctImage->save();
                    }
                }
            }
        }
    }

Bước 3. Thêm tập tin lưỡi dao

Php tải xuống nhiều hình ảnh
Nội dung Tạo file. lưỡi. php

________số 8

Bước 4. Thiết lập dropzonejs

Trước tiên, bạn cần tải xuống lib dropzonejs, Chi tiết về dropzonejs bạn có thể tải lên trang dropzonejs. com để tìm hiểu. Khi download về, copy các thư mục (js, css, images) vào project