Adding Email Attachments From Local Storage - laravel

I am using laravel/lumen. I am able to save files using Storage::disk("local")->put(); in my storage directory. However now I want to attach a few of those files to an email and send, this is done with a job the error I get is
lumen.ERROR: exception 'Swift_IoException' with message 'Unable to
open file for reading
Now I read something about symbolic linking which I tried but that simply did not change the result, I was still unable to attach files i n my storage folder to my emails.
This is my directory structure:
/home/xxxxxx/:
-example.app
--storage
---app
----public
-public_html
--example.app
---storage
Attaching the file like this:
foreach ($params["attachments"] as $attachment) {
$mail->attach($attachment["file"], [
'as' => $attachment["name"],
'mime' => $attachment["mime"]
]);
}

You've already realized that attach() method expects the full path to the file. However, others may find useful know how to achieve that. So, in case you're using the default settings for local store, you may be able to get the full path by calling the storage_path() helper. This way:
/*
* This will return the full path to the file:
*
* /path/to/laravel/storage/app/attachment/path
*/
storage_path('app/' . $attachment['file']);

Okay so apparently swift mailer fails when you pass a full url instead of the real path to the file. Didn't know this, don't know if it's a bug or it was done by design.

Related

Issue getting a file with Laravel

Hi I do not know what is happening but I want to get a file from the storage folder and it says that it does not existe but it exists I mean I go to the folder and it is there..
I retrieve the file like this:
$path = storage_path('app/public/' . $settlement->support);
$settlement_file = Storage::disk('local')->get($path);
And I get this error:
message: "File not found at path:
home/jysparki/newjis.jisparking.com/storage/app/public/06152617_8-2025.pdf"
and If I go to the folder it exists so I wonder what am I doing wrong? Thanks.
If you look at config/filesystems.php file and find disks array, you may notice that root under the local disk key is set to storage_path('app'). Well it's up to what the value may be in your case you're free to change it, but what does this mean is whenever you call Storage::disk('local') method it will try to find files relative to storage_path('app'), so there is no need to specify full path. Try this instead
$settlement_file = Storage::disk('local')->get('/public/' . $settlement->support);
or
$settlement_file = Storage::disk('public')->get($settlement->support);
as Laravel by default has public disk which pointed into /app/public storage directory

Deleting file with Laravel

i'm having troble deleting a file from a Laravel Project.
The file is in the /storage/exports directory, it's an excel stored on the disk usingthe Laravel Excel library.
This is my code:
$path = $excel->store('xls', false, true)['full'];
...send the xls via mail....
Storage::delete($path);
When i check the existence of the file with file_exist i get true, so Laravel is able to read the file.
I've also checked the permission for the folder and i give all permissions on this folder using chmod 777
Any ideas?
Thanks!
The storage driver already has an awareness of a root directory, so the $path must be relative, not full. So if your file is in:
/this/is/the/full/path.xls, and the config filesystems.disks.local.root is set to /this/is/the/full, you're essentially having it look for a file in /this/is/the/full/this/is/the/full/path.xls.
You have two options.
1) Add a new driver to that config, and reference it directly:
'custom_location' => [
'driver' => 'local',
'root' => '/some/other/root/path',
]
Storage::driver('custom_location')->delete($relativePathFromRoot)
2) Create a one-off:
$rootPath = '/some/other/root/path';
$client = Storage::createLocalDriver(['root' => $rootPath]);
$client->delete($relativePathFromRoot);
Try storing the file within storage/app/exports. The Storage class's default location to store files is storage/app.
It may be a good idea to instead of using the Excel class to store the file, to instead save the file using the Storage class: Storage::put('exports/excelfile.xls', $fileContents);
Even though you are using Laravel, you can use regular PHP functions.
unlink($path);

renaming a file before uploading to digital ocean spaces using laravel

To upload a file I use
Storage::disk('spaces')->putFile('uploads', $request->file, 'public');
The file is saved successfully on digital ocean spaces. But I want to rename it to something like this user_1_some_random_string.jpg. And then save it.
How can I do it?
The move method may be used to rename or move an existing file to a new location:
Storage::move('hodor/oldfile-name.jpg', 'hodor/newfile-name.jpg');
Also:
If you would not like a file name to be automatically assigned to your stored file, you may use the storeAs method, which receives the path, the file name, and the (optional) disk as its arguments:
$path = $request->file('avatar')->storeAs(
'avatars', $request->user()->id
);
More: https://laravel.com/docs/5.6/filesystem
try use rand()
$ext = $request->file('file')->getClientOriginalExtension();
$name = rand(11111,99999).'.'.$ext;
Storage::disk('spaces')->putFile('uploads', $name, 'public');
This is pretty old but I found an answer for anyone still looking.
You need to use the method putFileAs, as far as I can see
the first parameter is the bucket/location. I tested this and it will create a new folder if you use 'uploads/testz' it created the 'testz' in the uploads folder on spaces.
the second parameter is the request file object, in my case $request->file('file')
the third parameter is the filename that you WANT to store the file as. I tested and if you 'testz/<yourspecialfilename.extension>' it will create the same folder as in parameter 1, which suggests to me that the method concats param 1 and 3.
So the full snippet in my controller is
public function create(Request $request){ Storage::disk('spaces')->putFileAs('uploads/testz', $request->file('file'), 'mychosenfilename.mydesiredextension');
return redirect()->back();}

Laravel uploading a file to the right directory

I upload the files alright. Only it turns out that, in my case, the starting directory is Public, and that results that the files are accesible by everybody when they should be in the directories that are protected by previous authentication, that is, under App.
So, when I do this:
$file = Input::file('fichero');
$destinationPath = 'uploads/folder';
$file->move($destinationPath);
// Create model
$model = new Model;
$model->filename = $file->getClientOriginalName();
$model->destinationPath = $destinationPath;
The file is saved in here: localh.../myweb/public/
and that is outside the App parent folder where all my application files are.
I can imagine that the fact that is going to that directory is because it is taking the default in the configuration file, which is localhost/laravel/public/index.php/
so, writing something like
$file->move(base_path().'/uploadfolder/subdirectory/', $newname);
will start from what is fixed as base_path, but the base_path needs to be something like localhost/mywebname/public.
So, how do you write the path so that it goes to directories under App and not under Public?
I hope I am not asking a dumb question, but I have been trying for days and I have read the 3 related queries in here.
thank you
You may use:
app_path() // Path to the 'app' folder
app_path('controllers/admin') // Path to the 'app/controllers/admin' folder
This will return you local file system path, you should put your uploaded images in public/... folder.

How do I get the full local path of a file uploaded with $_FILES

I am uploaded a file through the file input. If I print_r($_FILES), I can get several pieces of info, but it doesn't give me the full LOCAL path of the file (the path on my computer, not the server). How do I get this?
I need this to use the FTP library in CodeIgniter. Documentation is here on how to use it to upload a file.
As you can see, it requires the full local path, although I'm not sure why.
As file upload path is a variable which changes from server to server, I would suggest you to use move_uploaded_file() function:
$target = "uploads/myfile.something";
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target)) {
// do something
}
that way it will always work no matter what the path is even if it changes. The target can be anything you wish, it's relative to the current folder where script is being executed.
I solved this.
public function upload($file, $folder) {
$this->CI->ftp->upload($_FILES['file']['tmp_name'], '/public_html/rest/of/path/' . $_FILES['file']['name'], 'ascii', 0775);
}
The file is being uploaded, so you can access it with $FILES['file']['tmp_name']. This is from within a class, so that's why I'm using $this->CI. Otherwise, I would just use $this.

Resources