Laravel symlink and cPanel - laravel

On my Laravel website I'm using symlink to store and show the images from storage.
With
php artisan storage:link
I had created the symlink and everytime when I upload a new news article, the image is uploaded in the main Storage and with symlink it's setup to the public folder and I'm displaying the image properly.
So far so good, but when I've created a copy of the website, a problem appears...
When I've created a copy of the website with cPanels File Manager, and move to a new location, the storage symlink in the public directory has become a folder, not a symlink.
After that when I try to upload a new news article, I can see it's uploaded in the main Storage folder, but not in the public/storage, so as a result the image is not displaying. That's because it's not a symlink anymore, but now it's a folder.
I've deleted the storage folder from the public directory, with SSH I've used the command again
php artisan storage:link
and I've created a new news article and the image is displaying properly, but now all other images are gone.
Is there any command that will regenerate the paths, so all other images will be display again?
I'm using Laravel 5.5

You can solve it in another way to create a symlinkexample.php file
into your public folder and run the file path into browser.
Then Storage folder will be created into public folder. Folder path
will be public/storage with linked to your public folder.
Code below for symlinkexample.php :
<?php
$targetFolder = $_SERVER['DOCUMENT_ROOT'].'/storage/app/public';
$linkFolder = $_SERVER['DOCUMENT_ROOT'].'/public/storage';
symlink($targetFolder,$linkFolder);
echo 'Symlink process successfully completed';
?>

Try this:
In route/web.php add the following code:
Route::get('/storage', function(){
\Artisan::call('storage:link');
return "Se han vinculado las imágenes";
});

If for some reason you are using different file structure and for some reason don't have terminal access, \Artisan::call('storage:link') will fail since it won't find public path.
Route::get('cmd', function(){
$process = new Process(['ln', '[symlink-here]','[target-folder]' ]);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
});
read about Process class here

Related

Valet showing files under wrong Url

Currently running into issues on my server with links to files in my Laravel application.
I currently have a images folder in storage/app/public and I linked the storage/app/public folder to public with php artisan storage:link.
Now i have a image.jpg in the images folder.
On my local system while using valet I can see the image with the tow following links:
/images/image.jpg
/storage/images/image.jpg
On my server only the second one works (which I assume is correct as there is no images folder directly in the public-directory.
Problem is, I often run into issues when deploying as customers can't see images online (as the link is not working). This wouldn't be an issue if I could not see images local while testing the UI.
Anyone also has this issue and can lead me to some kind of solution?
The issue is that the LaravelValetDriver has a fallback test; if the file doesn't exist in the public directory then it will check the public storage directory.
You can find that here:
if ($this->isActualFile($storagePath = $sitePath.'/storage/app/public'.$storageUri)) {
return $storagePath;
}
You can prevent this behaviour by creating your own Valet Driver that extends the LaravelValetDriver and overrides the isStaticFile method, e.g:
class NoStorageFallbackLaravelValetDriver extends LaravelValetDriver
{
public function isStaticFile($sitePath, $siteName, $uri)
{
if (file_exists($staticFilePath = $sitePath. '/public' . $uri)
&& is_file($staticFilePath)) {
return $staticFilePath;
}
return false;
}
}
Using the public disk is straight forward and should work without problems out of the box.
The public disk uses the 'local' driver which by default uses the storage/app directory.
The command:
php artisan storage:link
will create a symbolic link 'storage' inside your public folder that links to the non-public folder storage/app/public.
When the link is working, you can use the following command:
Storage::disk('local')->put('public/file.txt', 'Contents');
This will store file.txt to
storage/app/public/file.txt
You won't be accessing the file at storage/app/public.file.txt instead you will be accessing it using the symlink public/storage...
Therefore your saved file will publicly accessible at
public/storage/file.txt
Use the asset helper you can get the location of the file:
echo asset('file.txt');

Uploading images to Public_html file rather than public project file

Had this working but just updated my website on the server and it seems to have made all my uploaded files head to my public folder in my project rather than the public_html. Any idea on which file i must have uploaded and the fix?
You said you had it working before.
Was that on the same server...live?
If so, then could that be a server issue and not necessarily laravel issue?
Maybe you should ask your host for help and clarifications about what changed?
If you are convinced it was about what you changed...then care to show us?
Ideally if the server is cpanel, you will want to upload all your laravel files into your home's directly...then all your /public folder content into public_html
It will look like this:
.cpanel/
app/
etc/
bootstrap/
mail/
public_html/
content from your laravel /public directory
vendor/
...etc
You also need to go into your /public_html and edit the index.php content:
require __DIR__.’/../vendor/autoload.php’;
$app = require_once __DIR__.’/../bootstrap/app.php’;
And make sure it points to the correct location of your bootstrap folder..if you follow the structure above, then its ok.
Lastly, confirm that your PHP is indeed >= PHP7.0
With help from this answer: https://stackoverflow.com/a/50261573/3475551
I managed to get laravel to save uploaded files to my servers public directory instead of app/public.
Edit app/Providers/AppServiceProvider.php like this:
public function register()
{
$this->app->bind('path.public', function() {
return realpath(base_path().'/../../public_html');
});
}
Hope this helps.

Laravel: How to save images to public folder?

I am having trouble uploading a user image to my public folder. The file name generates correctly, and saves the name to my database, except the iamge itself refuses to get saved into my public folder. What am I doing wrong?
public function update_avatar(Request $request) {
if($request->hasFile('avatar')) {
$avatar = $request->file('avatar');
$filename = time() . "." . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300,300)->save(public_path('/uploads/'.$filename)); ==> This is causing me errors
user = Auth::user();
$user->avatar = $filename;
$user->save();
}
The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public. This convention will keep your publicly accessible files in one directory that can be easily shared across deployments.
To create the symbolic link, you may use the storage:link Artisan command:
php artisan storage:link
https://laravel.com/docs/5.5/filesystem
I think you should try this:
$destinationPath = public_path('uploads');
Image::make($avatar)->resize(300,300)->save($destinationPath.'/'.$filename);
First you must move your image to destination directory and then resize it.
$avatar->move(public_path('/uploads/'.$filename));
Image::make(public_path('/uploads/'.$filename))->resize(300,300)->save(public_path('/uploads/'.$filename));
Note
Check the directory that you moving your file there is already exist.

How to create symlink from public/storage to storage/app/public in laravel?

I have no idea on how to create symbolic link or symlink.
I am working on File system in laravel 5.2.
The document says that i need to create a symbolic link from public/storage to storage/app/public to keep the publicly accessible files in one directory.
How to create that symlink or symbolic link?
Which file or directory should I place that code?
App::make('files')->link(storage_path('app/public'), public_path('storage'));
And don't forget to use App after namespace.
Run this command:
php artisan storage:link
In a Windows environment, you can:
cmd with Run as administrator
Run the mklink command:
mklink /D "C:\xampp\htdocs\xxxx\yyy\public\storage\"
"C:\xampp\htdocs\xxxx\xxx\storage\public\"
On Shared Server, where one doesn't have ssh access to run
php artisan storage:link this helps me run that from a controller, the if block code section can also be placed in a Service Provider as well as suggested by #shìpu-ahamed
public function displayForm()
{
if(!file_exists(public_path('storage'))) {
\App::make('files')->link(storage_path('app/public'), public_path('storage'));
}
return view('admin.index');
}
Added same code but still getting issue.
Method link does not exist.
currently i am adding link in my controller constructor.
here is code:
public function index()
{
$shots=[];
App::make('files')->link(storage_path('app\public'), public_path('..\public\storage'));
return View::make('adminpages.index',['shots'=>$shots]);
}

How to use SimplePie with Laravel

I am using Laravel 5.2. I need to get RSS feeds using SimplePie, so I install the library through composer.json and now my vendor folder contains simplepie but when i run the following code this error shows:
ErrorException in SimplePie.php line 1379: ./cache is not writeable.
Make sure you've set the correct relative or absolute path, and that
the location is server-writable.
My code in route:
Route::get('feed', function () {
$feed = new SimplePie();
$feed->set_feed_url(array(
'http://www.thenews.com.pk/rss/2/16' //it has no images
));
$feed->init();
$itemCount=$feed->get_item_quantity();
return $itemCount;
});
Create a writeable cache folder for simplepie in the /storage/ folder (I called mine simplepie_cache).
Set the path in your feed object:
$feed->set_cache_location(storage_path() . '/simplepie_cache');
That should do the trick.
check what is saying ./cache is not writeable can't write to cache folder check permissions to this folder or try a L5 wrapper for SimplePie https://github.com/willvincent/feeds

Resources