<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use App\Good;
use App\Image;
class testpost extends Controller
{
//
public function execute(Request $request){
$file = "/Users/local/Desktop/111/2.png";
//$folder = "/Users/local/Desktop/111";
//unlink($file);
dd(Storage::delete($file));
//$status_delete_file=Storage::deleteDirectory($folder);
}
}
I'm trying delete file "/Users/local/Desktop/111/2.png". And Storage does not remove this file or folder 111. No errors, always returns "false". Tried to remove file through standard function PHP "unlink"
unlink($file)
All well removes!
Help me please.
Laravel 5.4
PHP 7.1.1
Laravel Storage Facade used for managing file in $root directory of your Laravel filesystem disk.
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
....
],
Please refer to this documentation. If your disk is local then $root directory will be storage/app, or if it public then $root directory will be storage/app/public. All operation: create, move, delete will do inside the defined $root directory.
By the way Laravel use league/flysystem package to handle Storage operation. You can see in the package source code what is Storage::delete() actualy doing.
// flysystem/src/Adapter/Local.php
/**
* #inheritdoc
*/
public function delete($path)
{
$location = $this->applyPathPrefix($path);
return unlink($location);
}
The delete function actually call unlink function. But adding directory prefix before do the operation.
So example: if a you want to delete file uploads/image.jpg in your public disk storage, you just need to call
Storage::delete('uploads/image.jpg')
Related
I upload my Laravel app to C-panel, my public contents are in public_html and I change index.php on public to this :
require __DIR__.'/../thermotajhiz/vendor/autoload.php';
$app = require_once __DIR__.'/../thermotajhiz/bootstrap/app.php';
And other files in main root named thermotajhiz
Everything is ok but when I want to upload an image, instead of upload to public_html, a file with public name will create in thermotajhiz file outside the public_html
how can I fix this ?
I'm using this plugin for upload files :
alexusmai/laravel-file-manager
in config/filesystems.php set like this
// Filesystem Disks
'disks' => [
'public' => [
'driver' => 'local',
'root' => '../public_html',
'url' => env('APP_URL'),
'visibility' => 'public',
],
],
if doesnt work try to change ../public_html to ../../public_html or /home/yourcpanelusername/public_html
and also make sure in your config/file-manager.php you set:
'leftDisk' => 'public',
I put this code to index.php instead of register method in AppServiceProvider and it's work :
$app->bind('path.public', function() {
return base_path('../public_html');
});
After this code :
$app = require_once __DIR__.'/../thermotajhiz/bootstrap/app.php';
I've been trying surfing around this site looking for answers, but nothing works for me, i want to upload an image that save in public_html folder, instead of saving it in project folder (i seperate the project folder and put all public file in public_html folder). Already bind the public.path but it returns to my project folder, not public_html one.
My image upload controller
public static function uploadSubmit($request, $type, $for)
{
if($request->hasFile('photos'))
{
$allowedfileExtension=['jpg','png'];
$files = $request->file('photos');
foreach($files as $file)
{
$extension = $file->getClientOriginalExtension();
$check=in_array($extension,$allowedfileExtension);
//dd($check);
if($check)
{
$idx = Image::create([
'type' => $type,
'ids' => $for,
'ext' => $extension,
])->id;
$filename = $idx . '.' . $file->getClientOriginalExtension();
$filename = $file->storeAs(public_path('images/'), $filename);
}
}
}
}
i already do the public path bind like this in public_html/index.php
$app->bind('path.public', function() {
return __DIR__;
});
Thank you!
First, add config to filesystem.php on config folder:
'disks' => [
...
'public_html' => [
'driver' => 'local',
'root' => public_path(),
'visibility' => 'public',
],
...
],
Second, store image with code:
$file->storeAs('images', $filename, 'public_html')
P/S: Remember to configure the correct path for the public_html directory:
$app->bind('path.public', function(){
return __DIR__.'/../../public_html';
});
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.
When using the local driver, all file operations are relative to the root directory defined in your filesystems configuration file. By default, this value is set to the storage/app directory. Therefore, the following method would store a file in storage/app/file.txt:
Storage::disk('local')->put('file.txt', 'Contents');
Or in your case in storage/app/images/file.txt
$file->storeAs(public_path('images/'), $filename);
You can change where these files are stored by changing the filesystems configuration file.
'local' => [
'driver' => 'local',
'root' => public_path(),
],
I moved my website from localhost to shared hosting all it's good, but i need when i upload file stored directly to public_html: something like this
public_html/storage/
I tried use somenthing like :
symlink('/home/abdoweb/bmcelarave/storage/app/public', '/bmce/public_html/storage')
the problem still exists.
my Controller :
public function addcategory(Request $request){
$title = $request->input('category_name');
$image = $request->file('category-image');
$name = $image->getClientOriginalName();
$image->move(storage_path().'/app/public/category',$name);
$data[] = $name;
$query= DB::table('category')->insert(
[
'title' => $title,
'image' => $name,
"created_at" => Carbon::now()
]);
if($query){
return redirect('categories');
}
}
My folder :
home/abdoweb/{bmcelaravel} <= my public folder
Core laravel :
home/{bmce} <= core laravel
Thank you.
You can a use storage driver :
Inside config/filesystems.php :
'disks' => [
'public' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
'url' => env('APP_URL').'/public',
'visibility' => 'public',
]
]
//Now you can move the file to storage like :
Storage::disk('public')->putFile('category', $request->file('category-image'));
First of all the recommended location for that stuff is to stay on the public path not creating a new one, unless there is an actual reason for that. Did you actually check that the symlink was created?
Laravel has an own command the create a symlink from storage/app/public to public/storage (the storage folder will be generated afterwards):
php artisan storage:link
But if you want to create defaults symlinks you should create one for yourself, like you already did.
This is the symlink pattern:
ln -s target source (fill in your target and source path)
So if you actually get the correct file from your request, this code should work:
Storage::putFile($name, $request->file('category-image'));
For more and detailed infos look into the filesystem documentation
i added an upload field in my CRUD Controller.
Upload works fine and file gets loaded in my /storage/private directory.
Here is filesystems.php file:
'private' => [
'driver' => 'local',
'root' => storage_path('private')
],
Here are my custom functions in the File.php Model:
public static function boot()
{
parent::boot();
static::deleting(function($file) {
\Storage::disk('private')->delete($file->file);
});
}
public function setFileAttribute($value)
{
$attribute_name = "file";
$disk = "private";
$destination_path = "";
// Cifratura del file
file_put_contents($value->getRealPath(), file_get_contents($value->getRealPath()));
$this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
}
And here is my FileCRUDController.php code:
$this->crud->addField(
[ // Upload
'name' => 'file',
'label' => 'File to upload',
'type' => 'upload',
'upload' => true,
'disk' => 'private'
]);
When i try to download the file, however, it tries to fetch it from http://localhost:8000/storage/myfile.png instead of http://localhost:8000/storage/private/myfile.png
What i'm doing wrong? Thank you very much.
I would also like to know if there is a way to hook a custom function instead downloading the file directly from the CRUD view. My files are encrypted and i need a controller that cares about decrypting before sending the files to the user.
Method url() is still not usable for the files are placed in subdirectories.
You may also use the storage_path function to generate a fully qualified path to a given file relative to the storage directory:
$app_path = storage_path('app');
$file_path = storage_path('app/file.txt');
In reference to Issue #13610
The following works for version 5.3:
'my-disk' => [
'driver' => 'local',
'root' => storage_path(),
'url' => '/storage'
],
\Storage::disk('my-disk')->url('private/myfile.png')
this should return "/storage/private/myfile.png"
In laravel 5.3, if I upload a file from a form, it will be stored in own storage directory. Then I should create a symlink to that storage path in public path.
I can't use creating symlinks on my system because of the limitation. How can I upload files directly to public/uploads instead of storage/app/uploads?
I tried to set the path in AppServiceProvider.php but it doesn't work.
public function register()
{
//not working:
$this->app->useStoragePath( $this->app->basePath() . "/upload");
//also not working:
// $this->app->bind('path.storage', function () {
// return $this->app->basePath() . '/upload';
// });
}
by "not working" I mean that it is still uploading into the default storage directory. I cleared also the cache.
Here is the upload part (used in a controller):
$request->image->storeAs("uploads", "uploaded_image.jpg");
Thanks for any hints. :)
In Laravel 5 you now have to do this in config/filesystems.php. You can add a new disk like so:
'disks' => [
'uploads' => [
'driver' => 'local',
'root' => public_path(), // previously storage_path();
],
]
and then do the following to use it:
Storage::disk('uploads')->put('filename', $file_content);