天天看點

TP6如何使用檔案上傳

TP6檔案上傳示例:

一、最簡單的檔案上傳代碼

html:

<h3>TP6單檔案上傳</h3>

<form action="/index.php/test_upload/uploadAct01" enctype="multipart/form-data" method="post">
  <p>請選擇檔案: <input type="file" name="file1" /> </p>
  <p><input type="submit" value="送出"></p>
</form>      

控制器:

/**
     * TP6檔案上傳操作
     */
    public function uploadAct01()
    {
        // 擷取表單上傳檔案 例如上傳了001.jpg
        $file = request()->file('file1');
        // 上傳到本地伺服器
        echo $savename = \think\facade\Filesystem::disk('public')->putFile( 'files', $file);
    }      

檔案被上傳到了,public/storage/files/20211030 目錄

二、多檔案上傳示例

html:

<h3>TP6單檔案上傳</h3>

<form action="/index.php/test_upload/uploadAct02" enctype="multipart/form-data" method="post">
  <p>請選擇檔案1: <input type="file" name="files[]" /> </p>
  <p>請選擇檔案2: <input type="file" name="files[]" /> </p>
  <p>請選擇檔案3: <input type="file" name="files[]" /> </p>
  <p>請選擇檔案4: <input type="file" name="files[]" /> </p>
  <p>請選擇檔案5: <input type="file" name="files[]" /> </p>

  <p><input type="submit" value="送出"></p>
</form>      
/**
     * TP6多檔案上傳操作
     */
    public function uploadAct02()
    {
        // 擷取表單上傳檔案 
        $files = request()->file('files');

        $savename = [];
        foreach($files as $file){
            $savename[] = \think\facade\Filesystem::disk('public')->putFile( 'files', $file);
        }
        print_r($savename);
    }      

多檔案上傳bug,每個檔案框必須要選擇檔案,不然就會報錯!怎麼不會内部判斷下嗎?

如果項目中遇到這個問題,解決辦法就是自己寫上傳功能就好了,不用tp6的上傳

但行好事,莫問前程!

作者:yangphp