PHP code example of reg2005 / laravel-upload-manager

1. Go to this page and download the library: Download reg2005/laravel-upload-manager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

reg2005 / laravel-upload-manager example snippets


     
        use reg2005\UploadManager\UploadManager;
        
        class UploadController extend Controller
        {
            public function postUpload(Request $request)
            {
                $file = $request->file('avatar');
                $manager = UploadManager::getInstance();
                $upload = $manager->upload($file);
                $upload->save();
                return $upload;
            }
        }
    

     
        use reg2005\UploadManager\UploadManager;
        
        class UploadController extend Controller
        {
            public function postUpload(Request $request)
            {
                $fileUrl = $request->input('url');
                $manager = UploadManager::getInstance();
                $upload = $manager->upload($fileUrl);
                $upload->save();
                return $upload;
            }
        }
    

     
        use App\Upload;
        use reg2005\UploadManager\UploadManager;
        
        class UploadController extend Controller
        {
            public function postUpload(Request $request)
            {
                $uploadId = $request->input('id');
                $file = $request->file('avatar');
                
                $manager = UploadManager::getInstance();
                $upload = Upload::find($uploadId);
                if($manager->update($upload, $file))
                {
                    $upload->save();
                    return $upload;
                }
                return ['result'=>false];
            }
        }
    

     
        use App\Upload;
        use reg2005\UploadManager\UploadManager;
        
        class UploadController extend Controller
        {
            public function postUpload(Request $request)
            {
                $uploadId = $request->input('id');
                $fileUrl = $request->input('url');
                
                $manager = UploadManager::getInstance();
                $upload = Upload::find($uploadId);
                if($manager->update($upload, $fileUrl))
                {
                    $upload->save();
                    return $upload;
                }
                return ['result'=>false];
            }
        }
    

    
        use reg2005\UploadManager\UploadManager;
        
        class UploadController extend Controller
        {
            public function postUpload(Request $request)
            {
                $file = $request->file('avatar');
                $manager = UploadManager::getInstance();
                $upload = $manager->withValidator('image')->upload($file);    //加上验证组
                
                if($upload)
                {
                    $upload->save();
                    return $upload;
                }
                else
                {
                    $errorMessages = $manager->getErrors();                   //得到所有错误信息
                    $errorMessage = $manager->getFirstErrorMessage();         //得到第一条错误信息
                    throw new \Exception($errorMessage);
                }
            }
        }
    

    
        use reg2005\UploadManager\UploadManager;
        
        class UploadController extend Controller
        {
            public function postUpload(Request $request)
            {
                $file = $request->file('avatar');
                $manager = UploadManager::getInstance();
                $upload = $manager
                    ->withValidator('image')
                    ->withDisk('selectel')         // 储存到七牛磁盘里
                    ->upload($file);
                $upload->save();
                return $upload;
            }
        }
    

    
        use reg2005\UploadManager\UploadManager;
        
        class UploadController extend Controller
        {
            public function postUpload(Request $request)
            {
                $file = $request->file('avatar');
                $manager = UploadManager::getInstance();
                $upload = $manager
                    ->withValidator('image')
                    ->withDisk('localhost')
                    ->upload($file, function($upload){
                        if($upload->size > 1024*1024)
                        {
                            $upload->disk = 'selectel';
                        }
                        return $upload;
                    });
                $upload->save();
                return $upload;
            }
        }
    

        
         namespace App\Extensions;
        
        use reg2005\UploadManager\UploadStrategy as BaseUploadStrategy;
        use reg2005\UploadManager\UploadStrategyInterface;
        
        class UploadStrategy extends BaseUploadStrategy implements UploadStrategyInterface
        {
        
            /**
             * @param $filename
             * @return string
             */
            public function makeStorePath($filename)
            {
                $path = 'uploads/' . $filename;
                return $path;
            }
        
            /**
             * disk localuploads
             * @param $path
             * @return string
             */
            public function getLocaluploadsUrl($path)
            {
                $url = url('uploads/' . $path);
                return $url;
            }
        
            /**
             * disk selectel
             * @param $path
             * @return string
             */
            public function getselectelUrl($path)
            {
                $url = 'http://' . trim(\Config::get('filesystems.disks.selectel.domain'), '/') . '/' . trim($path, '/');
                return $url;
            }
        } 
    
config/app.php
 config/upload.php 
 UploadStrategy.php 
 config/upload.php 
 upload_strategy