Where Laravel Storage save the files? - php

hope you are doing really good today, I'm trying to copy a file using the function copy from Laravel Storage, but I don't know where it is saving them cause the first time looks normal but after the second shot I got an error about the file already exists, but I don't know where is saving it, this is my code, $dirDest should be a folder inside of my project but is not saving it there and I've already searched it in the ftp directory, hope you can help me.
$dirDest = 'temp-docs\temp.pdf';
Storage::disk('custom-ftp')->copy($this->document_path, $dirDest);```
Regards!

Check the path you have configured for your 'custom-ftp' in filesystems.php. This part:
'custom-ftp' => [
...
'root' => 'your/base/path',
...
],
You file copy will be located in this root folder + 'temp-docs\temp.pdf'
The copy function does not changes the driver. So it copies just inside of your ftp server.
To copy the file from one server to another (or your server) use a combination of get and put functions from the storage drivers.
$file = Storage::disk('my_ftp')->get($this->document_path);
Storage::disk('other_ftp')->put('temp-docs\temp.pdf', $file);
After that you will find the file of the other server root folder + 'temp-docs\temp.pdf'
I would also recommend you use the exists function to be sure that the file is there before you copy it.
Storage::disk('my_ftp')->exists($this->document_path)

Related

Copy file from any given path in Laravel

I want to copy a file from outside the root directory of my Laravel application using an artisan command which i made myself. The user will call the command like so:
php artisan crawl:pdf {filepath}
Then the filepath will be processed and on this part:
File::copy($path, storage_path('pdfs') . '/' . $fileName . ".pdf");
it breaks every time i use a path outside of my root directory it says "Failed to open stream: No such file or directory" as error msg".
I've already googled about copying files in plain php using copy() and in Laravel using Storage::copy(). Also reading through articles that all describe creating a disk which can route outside of laravel-root-directory.
All of that is not the solution to my problem. The user should be able to use any path.
C:\dir1\pdf3.pdf ;
E:\dir456\pdf2222.pdf`
...
from his own filesystem to copy files to the laravel-application.
Is their a way todo it ? Thx for all the help in advance!
EDIT 1:
Before anyone asks if i have write and read permission: yes i do. if i use plain php i can write and read everywhere on my working drive. I also checked the user laravel is running on with echo whoami and it gives the same user as my plain php application which works fine.
EDIT 2:
Worked in comments regarding using File-Facad-copy
Ok i found a solution and now it is working as i want it.
But first why is this happening. Laravel is expecting to always work on files using disks. It is required.
the solution is On-demand Disks!!!
This way it is possible to take a path given by a user and create a temporary disk which only works in this instance through facades.
Important is to make sure that the path given is always a directory! Like so:
$diskpath = dirname($path);
$disk = Storage::build([
'driver' => 'local',
'root' => "$diskpath",
]);
In my case I only want to copy one specific file which makes it mandatory to get the file information for correct naming and copying. Like so:
$pathinf = pathinfo($path)
$fileName = time() . '_' . $pathinf['filename']
Next we need to safe it to the new location. Like so:
Storage::disk('pdfs')->putFileAs('/', $path, $fileName . ".pdf");
Notes:
'pdfs' disk is a disk i configured in my filesystems.php config-file
I always asume that the user is giving the full path from the file like C:\...\test.pdf or something similar so watch out for the dirname() function. It will always cut the last part of the path and delete it.
Thanks to apokryfos for helping solving this through his comments and hints.

Laravel 8 Storage move file not found

I am trying to move a file from a temporary path to the correct path. However no matter what I tried it always sends back this file not found error:
League\Flysystem\FileNotFoundException
File not found at path: var/www/html/storage/app/public/covers/tmp/608c07a082451-1619789728/81-536x354.jpeg
I am using sail thus there are 'var/www/html/' in the path, even though there is no problem with this path in the container since it is possible to reach this path from the frontend fetch API and that is how the temp file was saved.
Here is the code in the controller:
//...
$temp_file = TempFile::where('folder', $cover_folder)->first();
if ($temp_file) {
// retrieve file path and target path
$filepath = storage_path("app/public/covers/tmp/{$temp_file->folder}/{$temp_file->filename}");
$target_path = storage_path("app/public/covers/{$course->id}/");
Storage::move($filepath, $target_path);
$course->cover_img = "{$target_path}/{$temp_file->filename}";
$course->save();
// remove temp directory and temp file record
rmdir(storage_path("app/public/covers/tmp/{$temp_file->folder}/"));
$temp_file->delete();
}
The error traceback highlighted on this line:
Storage::move($filepath, $target_path);
Also tried to use Storage::disk('public')->move(), or public_storage, or Storage::path(), or only pure string, all did not work.
Symlink is set from public/storage to storage by running php artisan storage:link.
I have searched as much as I could find on laracast, stack overflow, or github, but could not reach a solution. Does anyone have any idea on this issue?
Really really appreciate it.
dont use storage_path function because method Storage::move( have prefix path [app_path]/storage/app/
Storage::move("public/covers/tmp/{$temp_file->folder}/{$temp_file->filename}", "public/covers/{$course->id}/");

Where to put my .txt files for PHP to read them in Laravel

I am creating a small application that is going to read .txt files and put each row of text in the database. I am not sure where to put those .txt files so that it's in accordance with Laravel standards. Should I put those files in the /public/ directory and access them from a controller via public_path() helper function?
The documentation seems to omit that information. I'd appreciate any help.
When using the local driver, note that all file operations are relative to the root directory defined in your 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
Laravel Configuration

Laravel - Using allfiles gives back empty array

I've been using laravel for nearly all my projects and I've came across a problem when trying to list all files in a directory.
Here is my code below:
$directory = ("upload/"."$username->username");
$scanned_directory = storage::allfiles($directory);
So as you can see First line is the directory which is in Upload followed by the username of the account.
The second line is meant to scan this directory and list only files. Sadly it always gives me back an empty array. Using Scandir() here works... But it gives me folders which I'm not wanting.
So... Any tips? I checked the API and I really can't see what is wrong here :(
The reason you're getting no results when using the Storage::allFiles('directory') method is this method only looks inside of your application's storage folder, usually storage/app. Hence why modifying the storage path in configuration works as mentioned by #yassine (you shouldn't do this).
If you wish to use files outside of the storage folder, simply use the File facade instead. e.g. File::allFiles('directory')
Me too i had the same issue for local storage. I fixed it by :
project/config/filesystems.php
line 44
changed root folder to from storage_path('app') to changed to
base_path()
then I provided a related path to the root folder in Storage::files() or Storage::allfiles().
Like Storage::allfiles("app") if I want to fetch files withing the sub folder "app" relative to the base path given in the config file.
Good luck
$direcotry has to be an absolute path from your laravel root folder.
When your uploaded folder is in the root folder try this
$directory = base_path("upload/"."$username->username");
$scanned_directory = storage::allfiles($directory);

How do I copy files repository to new folder with PHP

I have a folder named "repository" in my admin folders. This folder holds 2 files: index.html and content.php. When a user creates a new page the php creates a new folder specified by the user then will need to copy the two files into that folder while leaving them in the repository.
copy(file,dest) does not work.
rename(file,dest) moves the files to the new folder but I lose them in the repository.
How do I copy the files in one folder to the new folder without losing the files in the original folder?
$dest = '../'.$menuLocation.'/'.$pageName;
$file1= "repository/index.html";
$file2= "repository/content.php";
mkdir($dest,0777);
rename($file1,$dest.'/index.html');
rename($file2,$dest.'/content.php');
$menuLocation and $pageName are supplied by the user. The files are there, file_exists returns back true. Also, the directory is created with no issues. rename() also works I just lose the files in repository.
For anyone hunting for the solution to this:
when using copy() in php you also need to include the file name.
copy(orginalfile,destination_with_filename);
for example:
wrong:
copy('/temp/sports/basketball.jpg','/images/sports/')
Correct:
copy('/temp/sports/basketball.jpg','/images/sports/basketball.jpg')
Use copy(). Make sure you capture the return value so that your program knows if it worked or not. Also check permission of the files and directory to confirm that the user executing the PHP script is able to create the new file in the place you specified. You might want use is_writable() on the directory to confirm these assumptions.

Categories