0

I have build a laravel application where I have some files on public/files directory. if I give this link to others such as download Link, they have chance to know my directory .. Suppose the link i have to give download link as

www.abc.com/files/45454553535.zip

But i don't want to let Users know that it's there in files directory. So How Do i hide the directory?

User57
  • 2,453
  • 14
  • 36
  • 72
  • Possible duplicate of [Laravel : How to hide url parameter?](http://stackoverflow.com/questions/39951509/laravel-how-to-hide-url-parameter) – Zac Webb May 09 '17 at 05:39

4 Answers4

0

Keep your files in the storage directory. That way you can serve the file to the users through code.

Try to follow the documentation: https://laravel.com/docs/5.4/filesystem

Angelin Calu
  • 1,905
  • 8
  • 24
  • 44
0

I don't know whether this would work or not but giving you an idea. Create a php file use like this:

header('Content-Type: application/zip');
$a=file_get_contents(file.zip)
echo $a;

From this user will not know from where the contents are fetched.

Vineet1982
  • 7,730
  • 4
  • 32
  • 67
0

Try this.

    public function getDownload()
    {
$filename='45454553535.zip'
        $file= public_path(). "/files/".$filename;

        $headers = array(
                  'Content-Type: application/zip',
                );

        return Response::download($file, $filename, $headers);
    }

".files/45454553535.zip"will not work as you have to give full physical path.

Update 20/05/2016

Laravel 5, 5.1, 5.2 or 5.* users can use the following method instead of Response facade. However, my previous answer will work for both Laravel 4 or 5.

return response()->download($file, $filename, $headers);

Gaurav Gupta
  • 1,588
  • 2
  • 14
  • 21
0

You can just create a your controller and route.

Route::get('files/{filename}', [
    'as' => 'file.get',
    'uses' => 'FileController@get',
]);

Controller should check your proper directory. Try to keep your files in storage path, not public.

class FileController extends Controller
{
    private $path;

    public function __construct()
    {
        $path = storage_path()
            . '/your-valid-directory/';
    }

    public function get($filename)
    {
        $file_path = $this->path
            . filter_var($filename, FILTER_SANITIZE_STRING);

        if (file_exists($file_path) && is_readable($file_path)) {
            return response(file_get_contents($file_path), 200, [
                'Content-Type: application/zip',
            ]);
        } else {
            abort(404);
        }
    }
}

Now you can get access to specific file by:

{{ route('file.get', ['filename' => '45454553535.zip') }}

This action generate link looks like: your-domain.com/files/45454553535.zip. :)

Anyway in my opinion - in the future just make file factory with specific headers, directories.

Good luck!

Patryk Woziński
  • 738
  • 1
  • 6
  • 18