Laravel upload without form and request functionality - laravel

I want to run a scheduler (CRON) that reads a csv file every minute and imports it into a database.
I want to grab the file from a predefined directory on my windows directory system and upload it into the storage area in Laravel on my host server.
I created a test function to read the contents of a directory. I get an error
'The "C:/Users/alfre/code" directory does not exist.'.
What is the correct way to upload files with a scheduler?
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'odoofiles' => [
'driver' => 'local',
'root' => '\Users\alfre\code\storage\csv',
],
I could not find an answer yet on the web.

you should not use absolute paths from windows.
try placing the cvs folds in in the storage folder and try
on your computer it will be something like C:/Users/alfre/code/storage/csv
public function test()
{
$files = File::allFiles(storage_path('csv'));
foreach ($files as $file)
{
echo (string)$file, "\n";
}
}

Related

Laravel Symoblic link not working on shared hosting

I modified the using filesystem configuration as
'local' => [
'driver' => 'local',
'root' => public_path('images'),
'throw' => false,
],
So on local system, I'm storing the all the files in public directory
localhost:8000/public/images
by using file upload
$this->logo->storeAs('brand', 'abc.jpg');
I can directly access images using
url('images')
but when i upload my project to live, images are not displaying. I tried and executed the following route, but fail to load images on live server
Route::get('/link', function () {
$target = public_path();
$link = $_SERVER['DOCUMENT_ROOT'] . '/new';
if (symlink($target, $link))
die("SUCCESS");
else
die("ERROR");
});

Unit testing in laravel using filesystems

I'm doing unit testing using the filesystems config. I want to map my file system disk to a testing directory where I will put my files(JSON, Excel, txt) to be used by the application for testing purposes. I don't want to use fake directories as I need to put the file in the 'import' location.
filesystems.php:
'disks' => [
'import' => [
'driver' => 'local',
'root' => '/import/clientname',
'visibility' => 'private'
],
];
In the application, here is the function which i want to test:
public function importFile(){
$filesystem = Storage::disk('import');
...
}
The solution I was thinking is to use this location for my testing:
Is there a way to implement this solution?
Yes, you can, for example, upload file with each of your disks, then assert if file exist in that directory.
Example of unit test for upload image with form:
public function testStorage()
{
$file = UploadedFile::fake()->image('File10.png');
$response = $this->post(route("save.image"), [
'file' => $file,
]);
$response
->assertStatus(200)
->assertSessionHasNoErrors();
Storage::disk('local')->assertExists("/images/" . $file->name);
}

Shared Hosting Laravel 8 Hosting problem: Symlink is not working

I need the deploy my laravel project to the web. With this introduction But the hosting provider is Doesn't allow me to use a symbolic link(symlink), I called customer service, and told me blocked for security reasons.
So, since I do not use symbolic links, there are big problems in uploading images and deploying them.
Is there a way to do deployment my app?
Change your disk configurations in the config/filesystems.php to read and write to public folder via public_path() function.
If you're deploying over a shared host, you probably have another folder like public_html as your public folder which is not included in your source. Try to link to the public_html using dots ../../public_html/
'public' => [
'driver' => 'local',
// 'root' => storage_path('app/public'),
'root' => public_path(),
'url' => env('APP_URL'),
'visibility' => 'public',
],
'photos' => [
'driver' => 'local',
'root' => public_path('photos'),
'url' => env('APP_URL') . '/photos',
'visibility' => 'public',
],
Another way is just create something like symlink using php, example you can create a route like below:
Route::get(
'/storage/{file}',
function ($file) {
$filepath = "../storage/app/public/".$file;
header("Cache-Control: public");
header("Content-Type: ".mime_content_type($filepath));
header('Content-Length: '.filesize($filepath));
readfile($filepath);
die();
}
);
Maybe not best practice, but it will solve your problem. It's will do same as symlink.

Laravel and React Upload image via Storage Facades fail

I want to upload a logo for my reports.
This is a snippet from my uploadLogo function
$file = $request->file;
Storage::disk('logo')->put('logo.png', $file);
I've created a logo profile in filesystems.php like this.
'logo' => [
'driver' => 'local',
'root' => public_path() . '/img',
'url' => env('APP_URL').'/public',
'visibility' => 'public',
],
But it eventually created the file in a 'random' ( or misunderstood ) location with a random name.
public\img\logo.png\M4FGLpZzAsyxn8NHiJLxo95EoP7I3CkIWvqkiQsv.png
What am I missing in my setup here?
You can store the file directly of the request's file (UploadedFile) object. And use storeAs to save by the name you supply. The Storage::put and UploadedFile::store` methods generate random names for the filed being stored.
$path = $request->file->storeAs('img', 'logo.png', 'logo');
More info https://laravel.com/docs/8.x/filesystem#storing-files and https://laravel.com/docs/8.x/requests#storing-uploaded-files

Illuminate\Contracts\Filesystem\FileNotFoundException in Laravel 5.6 using Storage facade

This code is returning an strange error:
$file = Storage::get(Storage::disk('notas_xml')->path('') . 't.txt');
As you can see the file does exist.
Get the file directly from the disk
$exists = Storage::disk('notas_xml')->exists('t.txt');
if ($exists) {
$file = Storage::disk('notas_xml')->get('t.txt');
}
And if you didn't setup notas_xml disk in filesystems.php
$file = Storage::get('public/arquivos/notas_xml/t.txt');
And to use your code, you need to setup a disk like so in config/filesystems.php
'notas_xml' => [
'driver' => 'local',
'root' => storage_path('app/public/arquivos/notas_xml'),
'url' => env('APP_URL') . '/storage',
'visibility' => 'public',
],
And get the file simply like this
$file = Storage::disk('notas_xml')->get('t.txt');
You need to get the file as below code:
Storage::disk('notas_xml')->has('t.txt');
Above has method may be used to determine if a given file exists on the disk:
Please read documentation https://laravel.com/docs/5.1/filesystem#retrieving-files
To better understand all this... The trick is in: config/filesystems.php
If you have this code (which is the default value of Laravel in Github)
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
This Facade Storage will act in the folders
root_laravel_project/storage/app
So if you want to check if an "israel.txt" file exists
if( Storage::exists('israel.txt') ){
echo "File found. And it exists on the path: root_laravel_project/storage/app/israel.txt";
}
Remember that up to this point it has nothing to do with the symbolic link php artisan storage: link
This symbolic link is only to make a folder called "public" within the "storage" folder be part of the public access through HTTP
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
Then at the time of doing the sym. You can access the files by http (which are public for any user)
This example assumes that you are using a virtual host (and if not, you must do it as a recommendation for work better locally)
http:// root_laravel_project.test/storage/israel-alvarez.txt
Or so that it is better understood as in the old school without a virtual host
http:// localhost/public/storage/israel-alvarez.txt
Then these urls will look inside your folder
root_laravel_project/storage/app/public/israel-alvarez.txt
Laravel's documentation is somewhat brief and can be confusing regarding this issue. But you just have to remember that one thing is to access through the "storage Facade" (which is the correct way to upload and verify if there are files) and another thing is to access through the http (through url) which is the symbolic link (which is the treatment you already give to users to download files or see if it is a PDF for example).
I hope it helps. Good day

Resources