定制 file/repo 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

file/repo

最新稳定版本:1.1.9

Composer 安装命令:

composer require file/repo

包简介

A Laravel package for file management

README 文档

README

Note:

currently it is not compatible with latest version of laravel 11, this is under development right now

Overview

The FileManager plugin is a custom Laravel solution designed to simplify file management. It handles file uploads, updates, deletions, and retrievals while maintaining references in a database table. This plugin leverages Laravel's built-in filesystem and database functionalities to offer a robust and easy-to-use file management system.

Features

  • Upload files: Store files and record their references in a database.
  • Update files: Store files and record their references in a database.
  • Delete files: Store files and record their references in a database.
  • Retrieve file paths: Store files and record their references in a database.

Installation

Step 1: Installation Command

You can install this plugin by following command :

composer require file/repo

Step 2: Register Service Provider

Add the service provider to if you are using laravel version less than 11 config/app.php:

'providers' => [
    // Other Service Providers
    file\repo\RepoServiceProvider::class,
],

and if you are using laravel 11 or update version you should add the service provider into bootstrap/providers.php:

<?php

return [
    // Other Service Providers
    file\repo\RepoServiceProvider::class,

];

Step 3: Run Migration

The service provider will automatically generate a migration for the file_repos table when the application boots. Run the migration using:

php artisan migrate

Usage

Upload Files

To upload a file, use the upload method. This stores the file and creates a record in the file_repos table.

use file\repo\FileManager;

FileManager::upload('users', $userId, $request->file('profile_picture'));

Update Files

To update an existing file, use the update method. This replaces the old file with a new one and updates the record in the database.

FileManager::update('users', $id, $request->file('new_profile_picture'));

Delete Files

To delete a file and its record from the database, use the deletefile method.

FileManager::deletefile($fileId);

Retrieve File Paths

To retrieve all file paths associated with a specific reference table and ID, use the get_path_by method.

$paths = FileManager::get_path_by('users', $userId);

Example: Product with image Management

Step 1: File Upload

Handle the file upload when a Product upload.

use Illuminate\Http\Request;
use file\repo\FileManager;
use App\Models\Product;

class ProductController extends Controller
{
 public function create(Request $request)
    {
        $request->validate([
            'name' => 'Required',
            'price' => 'Required',
        ]);
        $product = new Product();
        $product->name = $request->name;
        $product->price = $request->price;
        $product->save();
        if ($request->file) {
            FileManager::upload("product", $product->id, $request->file);
        }
        return back()->with('success', 'Product uploaded successfully.');
    }
}

Step 2: File Update

Allow admin to update their Product Image, replacing the old one.

public function update(Request $request, $id)
    {
         $request->validate([
            'name' => 'Required',
            'price' => 'Required',
        ]);
        $product = Product::find($id);
        $product->name = $request->name;
        $product->price = $request->price;
        $product->update();
        if ($request->file) {
          FileManager::update('product', $id, $request->file);
        }

        return back()->with('success', 'Product updated successfully.');
    }

Step 3: Retrieve File Paths

Display all images of Products.

    public function getById($id)
    {
        $product = Product::find($id);
        $image = FileManager::get_path_by("product", $id);
        return view('view', compact('product','image'));
    }
        @foreach ($image as $item)
            <img src="{{ $item['path'] }}" alt="product image">
        @endforeach

Step 4: File Deletion

Provide functionality to delete a user's profile picture.

public function delete($id)
    {
        $product = Product::find($id);
        if ($product) {
            $images = FileManager::get_path_by("product", $id);
            foreach ($images as $image) {
              FileManager::deletefile($image->id);
            }
            $product->delete();
        }

        return back();
    }

Conclusion

The FileManager plugin provides a structured and efficient way to manage file uploads in your Laravel application. By following the steps and examples outlined in this README, you can easily integrate this plugin into your project and enhance its file management capabilities.

统计信息

  • 总下载量: 43
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-19