1. 程式人生 > >laravel中上傳圖片並預覽

laravel中上傳圖片並預覽

(1)需要工具uploadify

(2)需要uploadify文件一份(uploadify文件連結

(3)demo模仿

<tr>
                    <th>縮圖:</th>
                    <td>
                        <input type="text" size="50" name="art_thumb" >
                        <input id="file_upload" name="file_upload" type="file" multiple="true">
                        <script src="{{asset('resources/org/uploadify/jquery.uploadify.min.js')}}" type="text/javascript"></script>
                        <link rel="stylesheet" type="text/css" href="{{asset('resources/org/uploadify/uploadify.css')}}">
                        <script type="text/javascript">
                            <?php $timestamp = time();?>
                            $(function() {
                                $('#file_upload').uploadify({
                                    'buttonText':'圖片上傳',
                                    'formData'     : {    
                                        'timestamp' : '<?php echo $timestamp;?>',
                                        '_token'     : '{{csrf_token()}}'  //csrf驗證,不然會出現500
                                    },
                                    'swf'      : "{{asset('resources/org/uploadify/uploadify.swf')}}",
                                    'uploader' : "{{url('admin/upload')}}",           //處理upload的php方法,在router設定路由
,                                    'onUploadSuccess':function(file,data,response){   //上傳成功的操作:(1)顯示相對路徑(2)展示預覽圖
                                        $('input[name=art_thumb]').val(data);
                                        $('#art_thumb_img').attr('src','/'+data); //需要在根目錄下面
                                    }
                                });
                            });
                        </script>
                        <style>    //優化樣式,變得更加優美
                            .uploadify{display:inline-block;}
                            .uploadify-button{border:none; border-radius:5px; margin-top:8px;}
                            table.add_tab tr td span.uploadify-button-text{color: #FFF; margin:0;}
                        </style>
                    </td>
                </tr>
                <tr>     
                    <th></th>
                    <td>
                        <img src="" alt="" id="art_thumb_img" style="max-width: 350px;max-height: 100px">  //展示縮圖

                    </td>
                </tr>

$('#art_thumb_img').attr('src','/'+data); //需要在根目錄下面

返回給upload方法的資料如下


我們需要得到Filedata這個資料,可以用Input::file('Filedata')

(4)upload方法(demo)

客戶端<form action="" method="post" enctype="multipart/form-data">    <input type="file" name="myfile">    <input type="submit" name="submit" value="Submit"></form>提交到伺服器$file = Input::file('myflie');if($file -> isValid()){
    //檢驗一下上傳的檔案是否有效    $clientName = $file -> getClientOriginalName(); //獲取檔名稱    $tmpName = $file -> getFileName();  //快取tmp資料夾中的檔名,例如 php9372.tmp 這種型別的    $realPath = $file -> getRealPath();  //這個表示的快取在tmp資料夾下的檔案的絕對路徑,例如我的是:D:\wamp\tmp\php9372.tmp    $entension = $file -> getClientOriginalExtension();  //上傳檔案的字尾    $mimeTye = $file -> getMimeType();  //大家對MimeType應該不陌生了,我得到的結果是 image/jpeg    $path = $file -> move('storage/uploads');    /**    * 如果你是這樣寫的話,預設會放置在我們 public/storage/uploads/ph1905.tmp    * 貌似不是我們希望的,如果我們希望將其放置在app目錄下的storage目錄下的uploads目錄中,並且需要更改的話...    *    */    $path = $file -> move(app_path().'/storage/uploads',$newName);    /**    * 這裡 app_path() 就是app資料夾所在的路徑,$newName 可以是你通過某種演算法獲得的檔名稱,主要是不能重複產生衝突即可,    * 比如 $newName = md5(date('ymdhis).$ClientName).'.'.$extension;    * 利用日期和客戶端檔名結合,使用md5演算法加密得到結果,不要忘記在後面加上檔案原始的副檔名    */}

demo.php

//圖片上傳
    public function  upload(){

        $file=Input::file('Filedata');
        if($file->isValid()){
            $entension=$file->getClientOriginalExtension();//獲得上傳檔案字尾
            $newName = date('YmdHis').mt_rand(100,999).'.'.$entension;冰潔

            $path = $file->move(base_path().'/uploads',$newName);//上傳檔案到伺服器指定目錄,並重命名【備註:base_path()指的是該程式根目錄】
            $filepath='uploads/'.$newName; //給使用者一個相對路徑
            return $filepath;
        }
    }

(5)設定預覽圖的大小

 <img src="" alt="" id="art_thumb_img" style="max-width: 350px;max-height: 100px">