在Laravel中下载文件

时间:2018-06-18 10:26:00

标签: php laravel laravel-5

在Laravel中,我有一个页面,我试图提供文件的下载链接,我正在使用Laravel Filemananager上传这些文件。

这些文件的路径在lfm.php配置文件中设置为:

/*
|--------------------------------------------------------------------------
| Working Directory
|--------------------------------------------------------------------------
*/

// Which folder to store files in project, fill in 'public', 'resources', 'storage' and so on.
// You should create routes to serve images if it is not set to public.
'base_directory' => 'public',

'images_folder_name' => 'assets/uploads/images',
'files_folder_name'  => 'storage/files',

'shared_folder_name' => 'shares',
'thumb_folder_name'  => 'thumbs',

我按照Laravel网站上关于publicstorage文件夹的文档说明了以下内容:

  

公共磁盘用于公开发布的文件   无障碍。默认情况下,公共磁盘使用本地驱动程序和   将这些文件存储在 storage / app / public 中。使它们可访问   在网络上,你应该公共/存储创建一个符号链接   的存储/应用程序/公共即可。

     

此约定将使您可以公开访问   一个目录中的文件,可以在部署中轻松共享   当使用像Envoyer这样的零停机时间部署系统时。

     

要创建符号链接,您可以使用存储:链接Artisan   命令:

     

php artisan storage:link

在此之前我还设置了以下内容:

  1. 在我的.env我将应用网址更改为:http://127.0.0.1:8000,这是php artisan server旋转本地服务器的地方。
  2. filesystems.php中,我将公共数组更改为以下内容:

    'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', ],

  3. 然后我创建了SymLink。

    在我的公共目录中,我有以下内容:

    enter image description here

    这是指向storage/app/public

    的链接

    然后我将文件上传到shares,所以在我的storage文件夹中,我现在有了:

    app/public/shares/name-of-file

    为了测试这个,我有以下控制器和视图:

    控制器:

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Storage;
    
    class TemplateController extends Controller
    {
        public function index()
        {
            $files = Storage::allFiles('files/shares');
    
            return view('pages.templates-and-tools.index', compact('files'));
        }
    }
    

    视野中的相关部分

    <table id="templates" class=" table-striped table-vaccancies table-responsive">
        <thead>
            <tr>
                <th class="col-md-1 col-xs-1">Type</th>
                <th class="col-md-6 col-xs-6">Name</th>
                <th class="col-md-2 col-xs-2">Category</th>
                <th class="col-md-2 col-xs-2">Modified</th>
                <th class="col-md-1 col-xs-1">Size</th>
    
            </tr>
            <tr class="warning no-result">
                <td colspan="5">
                    <i class="fa fa-warning"></i>
                    <h3 class="text-center"> No result</h3>
                </td>
            </tr>
        </thead>
        <tbody>
    
    
            @foreach ($files as $file) 
            @php 
    
    
            $date = Storage::lastModified($file); $date = gmdate("d/m/Y - H:i", $date); 
            $size = Storage::size($file); $size = ceil ($size/1000); 
            $url = Storage::url($file);
    
            $fileName = pathinfo($file)['filename']; 
            $Category = pathinfo($file)['dirname']; 
            $type = pathinfo($file)['extension'];
            $Category = basename(dirname( $file)) 
            @endphp
    
            <tr>
                <td>{{$type}}</td>
                <td>
                    <a href="{{ Storage::download($url) }} ">{{$fileName }}</a>
                </td>
                <td>{{$Category}}</td>
                <td> {{$date}}</td>
                <td>{{$size}}kb</td>
            </tr>
    
    
            @endforeach
    
        </tbody>
    </table>
    

    上面抓取files/shares下的Storage文件夹中的所有内容,然后我将每个表格单元格中的一堆有关该文件的信息吐出。

    我面临的问题是这一行:

    <a href="{{ Storage::download($url) }}">{{$fileName }}</a>

    它返回以下错误:

    File not found at path: http:/127.0.0.1:8000/storage/files/shares/Hummingbird_Technologies_ES.pdf (View: C:\xampp\htdocs\my-newable\resources\views\pages\templates-and-tools\index.blade.php)

    这是一个相当简单的错误,但是......

    enter image description here

    您可以看到该文件存在。

    那么,我做错了什么?

1 个答案:

答案 0 :(得分:1)

首先,检查您的文件是否已成功上传。

然后在a代码download参数中添加了特定网址。像这样。

<a href="{{ Storage::url($path) }}" download>{{$fileName }}</a>

或在a标记中添加了下载网址。像这样。

<a href="{{ route('download_url',$path) }}">Download</a>

控制器。

 public function download($path)
 {
     return Storage::download($path);
 }
相关问题