how to generate download link Files that are located on another server in laravel - laravel

I put my videos on my Shared web host and user can direct download all files. but I want to hide my actual file paths and make time-limited download links.
if files were on same server it is work fine.
when I use this code :
return response()->download('/home2/alihoss1/domains/alihossein.ir/public_html/dl/video/MySql/Sql1.mp4');
i see this error :
is_file(): open_basedir restriction in effect. File(/home2/alihoss1/domains/alihossein.ir/public_html/dl/video/MySql/Sql1.mp4) is not within the allowed path(s): (/home2/alihosse/domains/alihossein.ir/:/tmp/:/usr/local/php-7.0/lib/php/)
What solution would you recommend ؟
videos and laravel Project are not same host.

You could use something like file_get_contents() to get the file from the other server. This would lead to unneccessary traffic though because server 1 would download the file from server 2. That applies also to scp etc.
You should think about encryption:
$hash = encrypt([
'valid_to' => strtotime('+30 minutes'),
'file_path' => '/home2/alihoss1/domains/alihossein.ir/public_html/dl/video/MySql/Sql1.mp4'
]);
return redirect('http://server2.example/download/hash/' . urlencode($hash));
You then need to decrypt this on the second server with the same key. If you do not have laravel installed there, you can implement your own decrypting functionality (see: laravel openssl encryption).

Related

Laravel configuration usage for different servers in pure php file

I am new in Laravel and want to know such problem
I am running an web application on several server using Laravel.
But I have encountered with an issue.
When there is modification for the project, I need to sync with git on several servers.
But it has different settings for each server (eg: DB name, DB password...)
I have set it manually because I couldn't use .env or configuration file since the file is just pure php file.
The issue I want to solve is how can I get Laravel configuration data from pure PHP file(not controller or whatever).
It will be thankful if someone teach me solution.
You asked:
how can I get Laravel configuration data from pure PHP file
The answer would be this:
You just make some file in config/ folder of laravel app (or use the existing file like config/app.php)
You make an array of your key value pairs
<?php
return [
'some_key' => 'some_value',
...
and you simply call it with this code where ever you need it:
config('config_file.key');
for example
config('app.name');
would give you Laravel by default.

Laravel storage link in shared hosting

im having problem with setting up storage link on shared hosting.
Because of that I can't upload any image to storage folder in shared hosting
Just create a route and access it once.
Route::get('generate', function (){
\Illuminate\Support\Facades\Artisan::call('storage:link');
echo 'ok';
});
Remove storage folder in public_html/public/storage if you perfomed storage:link in local
then Create a php file for example mysymlink.php in public_html folder
Add below code to mysymlink.php file
<?php
$targetFolder = $_SERVER['DOCUMENT_ROOT'].'/storage/app/public';
$linkFolder = $_SERVER['DOCUMENT_ROOT'].'/public/storage';
symlink($targetFolder,$linkFolder);
echo 'Symlink completed';
Go to web site example.com/mysymlink.php
Using Laravel in shared hosting environment probably not the best decision. Some many good options are now available for the same price and you just need to put a bit of effort to understand how to work with them, even if you have no prior experience. You can use DigitalOcean, Linode, Vultr or many more and it will save you a ton of time.
In case you for some reason still want to use shared hosting and to actually answer your question you can
Investigate to set a right symlink (This possibility nowadays exist almost everywhere) it should be something like ln -s /home/user/laravel/storage/app/public /home/user/public_html/storage. Of course it should by your path which can be different depending of your hosting structure, you should be able to figure out it yourself.
Try to implement in some workaround way. For example you can try to put files from storage to storage/app/public folder manually (you even can latter automate it by using some script). But workarounds product new workarounds, better not do it hard and wrong way but instead use simple and right even if it seems hard for you at first glance.
Instead of using Storage facade, you can use custom method to upload file to your public directory
if($request->has('image')){
$img = $request->file('image');
$ext = $img->getClientOriginalExtension();
$name = time().'.'.$ext;
$path = public_path('\img\uploads');
$img->move($path, $name);
User::where('id', $user->id)->update([
'image' => $name
]);
}

Laravel Secure Storage Route

I am using the Storage::url() function for generating download links for files inside the storage directory, i also linked the storage directory to the public directory and everything is working fine.
However now any person can download the files without needing any authentication. What is the proper way to secure all routes starting with /storage/..... without having to re-write the download logic inside my controller?
you must add the download URL as a route on your routes.php file :
Route::get('downloads/{file}','MyController#download')->where('file', '^[^/]+$')->middleware('auth');
of course, don't forget to check if the file exists, after that you can use
$reponse()->download()
to return a download response. the method takes 3 parameters : file path, file name and HTTP response headers.
for more details, check this :
https://laravel.com/docs/5.7/responses#file-downloads

configure my web developed in local to web host

I've done some small demo of a web site and I hope to send it to a web host, but the preblem is after updating my folders and type the address, it just cannot find my web page. On local I use linux + XAMPP + Laravel so the structure is
/htdocs/laravel/public
/app
/vendor
and I just type
http://localhost/laravel/public/mywebpage
to access my web. But to use a web host who provides a file position like:
/home/sitename/public_html
do i need to create/modify any configuration file??
You can try renaming your local public folder to public_html and reflect this change at the /bootstrap/paths.php file.
There you'll find 'public' => __DIR__.'/../public', change ../public to ../public_html in this case.
I have to do something similar when I upload a laravel app to OpenShift, for example.

I'm told to down load a text file from another website and to put in the root directory of my website

I want to know how to down load a text file from another website and how to put it in the root directory of my website.Can you help me with problem please.THANK'S !!
As a program, or as a human action?
As a human, you should be able to take the link and download the file, upload to your website using for example FTP or (hopefully no one does this now) frontpage.
If you mean programattically, well its almost the same. Your script would need to open the file in the root directory, open the URL and read in the data sent and save it to the file, close the file. However, how to do so exactly depends on the lanugage you want, is this a repeated event or a once off?
This type of request usually happens when you are requesting a service that requires proof that you are the website owner. Being the owner of that website would also indicate that you should have at least ftp access over your site. If you are hosting the website yourself, this is an easy task you just copy the file into the root directory (windows default is c:\inetpub\wwwroot, ubuntu default is /var/www/). However if your website is hosted, you need to find your ftp username and password and utilize an ftp program like FileZilla. If you want to tell us what file host you use maybe someone can give you exact instructions. But beware of what file you host.
If you have the URL of the text file you can put it into your browser and then save the file to your disk. You then need to FTP it to your web server (or whatever method you normally use to get files onto the server)
In PHP:
<?php
$resource = curl_init('http://www.someserver.com/file.txt');
// important, otherwise curl_exec will output directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($resource);
curl_close($resource);
file_put_contents('/dir/localfile.txt', $data);
Or even better, with a bash script with a simple wget and cp.

Resources