1. 程式人生 > >laravel 檔案上傳

laravel 檔案上傳

自用找方法 有不完善的地方

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UploadsController extends Controller
{
    
   public function upload(Request $request){


        $data =['123','456'];
        $arr =['errno'=>'0',$data];

       Log::error('123456');
       $error='asdfadsfasdf';

       Log::emergency($error);
       Log::alert($error);
       Log::critical($error);
       Log::error($error);
       Log::warning($error);
       Log::notice($error);
       Log::info($error);
       Log::debug($error);

        return $data;

//       dd($request->all());

       $res = '沒有上傳';
       if ($request->hasFile('editor-file')) {
           //
           $file = $request->file('editor-file');
           $data = $request->all();

           $rules = [
               'editor-file'    => 'max:5120',
           ];
           $messages = [
               'editor-file.max'    => '檔案過大,檔案大小不得超出5MB',
           ];


           $validator = Validator($data, $rules, $messages);
//            $validator = $this->validate($data, $rules, $messages);

           $res = 'error|失敗原因為:非法傳參';

           if ($validator->passes()) {
               $realPath = $file->getRealPath();
               $destPath = '/uploads';
               $savePath = $destPath.''.date('Ymd', time());
               is_dir($savePath) || mkdir($savePath);  //如果不存在則建立目錄
               $name = $file->getClientOriginalName();
               $ext = $file->getClientOriginalExtension();
               $check_ext = in_array($ext, ['gif', 'jpg', 'jpeg', 'png'], true);
               if ($check_ext) {
                   $uniqid = uniqid().'_'.date('s');
                   $oFile = $uniqid.'o.'.$ext;
                   $fullfilename = '/'.$savePath.'/'.$oFile;  //原始完整路徑
                   if ($file->isValid()) {
                       $uploadSuccess = $file->move($savePath, $oFile);  //移動檔案
                       $oFilePath = $savePath.'/'.$oFile;
                       $res = $fullfilename;

                       $res['errno'] = 0;
                       $res['data'] = $fullfilename;

                   } else {
                       $res = 'error|失敗原因為:檔案校驗失敗';
                   }
               } else {
                   $res = 'error|失敗原因為:檔案型別不允許,請上傳常規的圖片(gif、jpg、jpeg與png)檔案';
               }
           } else {
               $res = 'error|'.$validator->messages()->first();
           }
       }

       return $res;
   }


    public function upload2(Request $request)
    {
        dd(2);
         $tempFile =[];
         $pics = $request->file('editor-file');

         if($request->hasFile('editor-file')){
             $tempFile['errno'] = 0;

//             foreach($pics as $file) {
//                 $destinationPath = '/uploads';
//                 $extension = $file->getClientOriginalExtension();
//                 $fileName = time().rand(10000,99999).'.'.$extension;
//                 $path = $file->move($destinationPath, $fileName);
////                 $tempFile['data'][] = $path->getRealPath();
//                 $tempFile['data'][] = 'http://e.hiphotos.baidu.com/image/pic/item/18d8bc3eb13533faf60894e4a1d3fd1f40345bb9.jpg';
//                 //                array_push($params['multipart'] , ['name' => 'carousel[]' , 'contents' => fopen($path->getRealPath(), 'r')]);
//             }


                     $destinationPath = './uploads';
                     $extension = $pics->getClientOriginalExtension();
                     $fileName = time().rand(11111,99999).'.'.$extension;
                     $path = $pics->move($destinationPath, $fileName);
//                     array_push($params['multipart'] , ['name' => 'cover' , 'contents' => fopen($path->getRealPath(), 'r')]);
                     $tempFile['data'][] = $path->getRealPath();


         }else{
             $tempFile['errno'] = '未找到圖片';
         }

//        // 返回圖片的URL地址
//        $data['data'][0] = 'http://e.hiphotos.baidu.com/image/pic/item/18d8bc3eb13533faf60894e4a1d3fd1f40345bb9.jpg';
//        $data['data'][1] = 'http://e.hiphotos.baidu.com/image/pic/item/f3d3572c11dfa9ec665057dd6bd0f703908fc1fa.jpg';

        return json_encode($tempFile);
    }



     public function upload3(Request $request)
    {
        $tempFile = array();
        $tempFile['errno']=0;
        $tempFile['data'][0] = 'http://e.hiphotos.baidu.com/image/pic/item/18d8bc3eb13533faf60894e4a1d3fd1f40345bb9.jpg';
        $tempFile['data'][1] = 'http://e.hiphotos.baidu.com/image/pic/item/f3d3572c11dfa9ec665057dd6bd0f703908fc1fa.jpg';

        $url['url'] = 'http://e.hiphotos.baidu.com/image/pic/item/18d8bc3eb13533faf60894e4a1d3fd1f40345bb9.jpg';
//        return json_encode($tempFile);
//        echo 123;
         return json_encode($url);
    }


    public function upload4(Request $request){
dd(4);
        if ($request->hasFile('editor-file')) {
            //
            $file = $request->file('editor-file');
            $data = $request->all();
            $rules = [
                'editor-file'    => 'max:5120',
            ];
            $messages = [
                'editor-file.max'    => '檔案過大,檔案大小不得超出5MB',
            ];


            $validator = Validator($data, $rules, $messages);
//            $validator = $this->validate($data, $rules, $messages);


            $res = 'error|失敗原因為:非法傳參';


            if ($validator->passes()) {

                $realPath = $file->getRealPath();

                $destPath = 'uploads/content/';

                $savePath = $destPath.''.date('Ymd', time());

                is_dir($savePath) || mkdir($savePath);  //如果不存在則建立目錄

                $name = $file->getClientOriginalName();

                $ext = $file->getClientOriginalExtension();

                $check_ext = in_array($ext, ['gif', 'jpg', 'jpeg', 'png'], true);

                if ($check_ext) {

                    $uniqid = uniqid().'_'.date('s');

                    $oFile = $uniqid.'o.'.$ext;

                    $fullfilename = '/'.$savePath.'/'.$oFile;  //原始完整路徑

                    if ($file->isValid()) {

                        $uploadSuccess = $file->move($savePath, $oFile);  //移動檔案

                        $oFilePath = $savePath.'/'.$oFile;

                        $res = $fullfilename;

                    } else {
                        $res = 'error|失敗原因為:檔案校驗失敗';
                    }
                } else {
                    $res = 'error|失敗原因為:檔案型別不允許,請上傳常規的圖片(gif、jpg、jpeg與png)檔案';
                }
            } else {
                $res = 'error|'.$validator->messages()->first();
            }
        }
        return $res;
    }


}