How to change public folder to public_html in laravel 8? - laravel

I wanna deploy my application on shared hosting on Cpanel where the primary document root has public_html but Laravel project public

You have to follow 2 steps to change your application's public folder to public_html then your can deploy it or anything you can do :)
Edit \App\Providers\AppServiceProvider register() method & add this code .
// set the public path to this directory
$this->app->bind('path.public', function() {
return base_path().'/public_html';
});
Open server.php you can see this code
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
return false;
}
require_once __DIR__.'/public/index.php';
Just Replace it with :
if ($uri !== '/' && file_exists(__DIR__.'/public_html'.$uri)) {
return false;
}
require_once __DIR__.'/public_html/index.php';
Then serve your application with php artisan serve, you also can deploy it on your Cpanel shared hosting where primary document root public_html

You can rename your public directory to whatever you want and tell Laravel to use the current directory path as the public path. Just navigate to the index.php inside the public folder (or whatever you renamed it to) and add the following code after the $app definition:
$app = require_once __DIR__.'/../bootstrap/app.php';
/* *** Add this code: *** */
$app->bind('path.public', function() {
return __DIR__;
});
/* ********************** */

there, idk if you have same problem like me, my cases is i using shared hosting, and i deploy it in my main domain. i place all my files in the root, my problem is the storage:link keep between public, not public_html (because its default by the hosting) so what i need to do i changhe the link using this code :
Before :
'links' => [
public_path('storage') => storage_path('app/public'),
],
After :
'links' => [
app()->basePath('public_html/storage') => storage_path('app/public'),
],
I hope it can help few people :)

Just Rename the "public" folder to "public_html" and it will work.
No changes are required in the code. Tested in Laravel 8.

my two cents :) What helped to me was:
Open LaravelService/vendor/laravel/framework/src/Illuminate/Foundation/Application.php and change publicPath() method to return public_html.
public function publicPath()
{
return $this->basePath.DIRECTORY_SEPARATOR.'public_html';
}
Then if you are using webpack also change the output folder:
const output = 'public_html';
mix.ts('resources/js/web/App.ts', output + '/js/web').setPublicPath(output).react();
This helped to me. Only issue is that it is probably not recommended to change Application.php as it is part of Laravel framework and after updating it, it will be probably erased so you have to put it back.

Related

Unisharp Laravel File Manager where /public is \public_html

I am using Laravel 8. My site's public directory is public_html and I have made the appropriate changed to the appservice provider:
public function register()
{
$this->app->bind('path.public', function() {
return base_path().'/public_html';
});
}
and changed the lfm is config to
'base_directory' => 'public_html',
The filemanager is uploading to the correct directory and making thumbnails in a subdirectory correctly, but it's view is a broken symbol.
I appreciate any help.
You have to bind the new storage path to the app instance. Please try this:
AppServiceProvider
public function register()
{
$this->app->instance('path.storage', base_path() . '/public_html');
}
Just get rid of the /laravel-filemanager/ prefix from the src URLs.
you have got the photos as a route.
example:
https://quislingmovie.com/photos/shares/6183cadfd8b51.jpg

php artisan storage:link on shared Hosting is not showing images

I have been working on a laravel app so for security reason I've placed all of my public files inside the base folder located in public_html and other files have been placed in the public_html/secondBase.
Now when I execute the command: php artisan storage:link the following error appears.
ErrorException
symlink(): No such file or directory
at vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:265
261| */
262| public function link($target, $link)
263| {
264| if (! windows_os()) {
> 265| return symlink($target, $link);
266| }
267|
268| $mode = $this->isDirectory($target) ? 'J' : 'H';
269|
I tired the solution below
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
public function register()
{
$this->app->bind('path.public', function() {
return base_path() . '/public_html/base';
});
}
the second method that I've tried is by making the file symlink.php, but the images are still broken.
symlink.php
<?php
symlink('/home/hmm/public_html/secondBase/storage/app/public','/home/hmm/public_html/base/storage');
and finally I made this route where the first error appears.
Route::get('/clear', function () {
Artisan::call('route:clear');
Artisan::call('storage:link', [] );
});
Thanks for any help.
Go to /public directory and run:
rm storage
Go to Laravel root directory and run:
php artisan storage:link
This problem comes when laravel project is moved/copied to some other folder.
The storage link is still there thus causing the exception error. public/storage folder exists and points to wrong location and it needs to be deleted with rm storage command.
After that run php artisan storage:link in terminal and it will create the storage link.
This needs to be done EVERY time when laravel is moved/copied/deployed!

Laravel, getting uploaded file's url

I'm currently working on a Laravel 5.5 project, where I want to upload files, and then, I want to get their url back of the file (I have to use it client side).
now my code looks like this:
public function pdfUploader(Request $request)
{
Log::debug('pdfUploader is called. ' . $request);
if ($request->hasFile('file') && $request->file('file')->isValid()) {
$extension = $request->file->extension();
$fileName = 'tmp' . round(microtime(true) * 1000) . '.' . $extension;
$path = $request->file->storeAs('temp', $fileName);
return ['status' => 'OK', 'path' => URL::asset($path)];
}
return ['status' => 'NOT_SAVED'];
}
It works fine, I got back the OK status, and the path, but when I want to use the path, I got HTTP 404. I checked, the file is uploaded fine..
My thought is, I should register the new url in the routes. If I have to, how can I do it dynamically, and if its not necessary what is wrong with my function?
Thx the answers in advance.
By default laravel store all uploaded files into storage directory, for example if you call $request->file->storeAs('temp', 'file.txt'); laravel will create temp folder in storage/app/ and put your file there:
$request->file->storeAs('temp', 'file.txt'); => storage/app/temp/file.txt
$request->file->storeAs('public', 'file.txt'); => storage/app/public/file.txt
However, if you want to make your uploaded files accessible from the web, there are 2 ways to do that:
Move your uploaded file into the public directory
$request->file->move(public_path('temp'), $fileName); // => public/temp/file.txt
URL::asset('temp/'.$fileName); // http://example.com/temp/file.txt
NOTE: make sure that your web server has permissions to write to the public directory
Create a symbolic link from storage directory to public directory
php artisan storage:link
This command will create a symbolic link from public/storage to storage/app/public, in this case we can store our files into storage/app/public and make them accessible from the web via symlinks:
$request->file->storeAs('public', $fileName); // => storage/app/public/file.txt
URL::asset('storage/'.$fileName); // => http://example.com/stoage/file.txt

Cannot download file from storage folder in laravel 5.4

I have store the file in storage/app/files folder by $path=$request->file->store('files') and save the path "files/LKaOlKhE5uITzAbRj5PkkNunWldmUTm3tOWPfLxO.doc" it in a table's column name file.
I have also linked storage folder to public through php artisan storage:link.
In my view blade file, I put this
<a href="#if(count($personal_information)) {{asset('storage/'.$personal_information->file)}} #endif" download>Download File</a>
and the link for download file is http://localhost:8000/storage/files/LKaOlKhE5uITzAbRj5PkkNunWldmUTm3tOWPfLxO.doc
But I get the error
NotFoundHttpException in RouteCollection.php line 161
If I add /app after the /storage it gives the same error. How can I download file from my storage/app/files folder?
Problem is storage folder is not publicly accessible in default. Storage folder is most likely forsave some private files such as users pictures which is not accessible by other users. If you move them to public folder files will be accessible for everyone. I had similar issue with Laravel 5.4 and I did a small go around by writing a route to download files.
Route::get('files/{file_name}', function($file_name = null)
{
$path = storage_path().'/'.'app'.'/files/'.$file_name;
if (file_exists($path)) {
return Response::download($path);
}
});
Or you can save your files into public folder up to you.
I'm using Laravel 6.X and was having a similar issue. The go around according to your issue is as follows:
1)In your routes/web.php do something like
/**sample route**/
Route::get('/download/{path}/', 'MyController#get_file');
2)Then in your controller (MyController.php) for our case it should look like this:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class MyController extends Controller
{
public function get_file($path)
{
/**this will force download your file**/
return response()->download($path);
}
}
If you have your files is stored in the public directory then use: public_path(). But if your files are stored in the storage directory, then use: storage_path()
NOTE: If your storage folder contains only attachment folder then consider having it this way: storage_path('photos/');
$destination = storage_path('storage/photos/');
or
$destination = public_path('storage/photos/');
$filename = "user_3423423465.png";
$pathToFile = $destination.$filename;
return response()->download($pathToFile,'user_profile_photo.png');
For Laravel 8
Upload File to Storage Folder
in this example, i have created a test folder inside storage folder name: uploadedfiles
In FileSystem.php
'uploadedfiles' => [
'driver' => 'local',
'root' => storage_path('uploadedfiles'),
],
Upload File
public function upload_file()
{
$file_name = "files_"."_".strtotime("now")."_".$_FILES['file']['name'];
$content = file_get_contents($_FILES['file']['tmp_name']);
Storage::disk('uploadedfiles')->put($file_name,$content);
}
Download File from Storage Folder
Route::get('download/files/{filename}', function($filename) {
$file = Storage::disk('uploadedfiles')->download($filename);
return $file;
});
Laravel8
Create a link
<a href="{{ url('download?path='. $user->avatar) }}">
Download
</a>
Define a route
Route::get('download', [DownloadController::class,'download']);
Handle it
public function download(Request $request){
return Storage::download($request->path);
}

Moving laravel 5.3 from local to live

What files do I need to change to get it running on live?
Right now this is my structure:
website.com/barber_base
In barber_base I have all the laravel files except public.
website.com/public_html/barber
In barber I have only the public content.
I searched on stack but I can't find a explanation for laravel 5+.
EDIT
I edited server.php in barber_base from :
if ($uri !== '/' && file_exists(__DIR__.'public/'.$uri)) {
return false;
}
require_once __DIR__.'public/index.php';
to
if ($uri !== '/' && file_exists(__DIR__.'../public_html/barber/'.$uri)) {
return false;
}
require_once __DIR__.'../public_html/barber/index.php';
But it is not working. I get a 500 error.
You need to change in below files
\config\app.php
'url' => 'http://localhost:8888',
\config\session.php
'domain' => null, // default will be null but sometimes session creates problem. If So then need to change in this section also.

Resources