Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

php - Get files with server path in application, not over http?

I have upload area for my customers which files should be private and not public accessible. Because of non-public availability, how can I get this files for preview in application? Is there any other way to get it directly from server?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are working with images:

Route::get('/file/download', function() {
    // get your filepath
    $filepath = 'path/to/image/image.png';
    return Response::download($filepath);
});

Then in your view:

<img src="{{url('/file/download')}}" class="rounded-circle" />

For any other file:

Route::get('/file/download', function() {
    // get your filepath
    $filepath = 'path/to/file/essay.docx';
    return Response::download($filepath);
});

Your view:

<a href="{{url('/file/download/')}}">Download</a>

If you wish you may use a controller:

namespace MyNamespace;

use IlluminateRoutingController;

class FilesController extends Controller
{
    public function downloadFile()
    {
        // get your filepath
        $filepath = 'path/to/file/essay.docx';
        return Response::download($filepath);
    }
}

Then your route definition would look like:

Route::get('/file/download', ['as' => 'file.download', 'uses' => 'MyNamespaceFilesController@downloadFile']);

And your view:

<a href="{{route('file.download')}}">Download</a>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...