Laravel: Storage::url() returning wrong bucket - laravel

I am uploading a file to S3 and specifying a bucket. When I check my S3 dashboard, it's in the right place and right bucket (s3-legacy-users). But when I go to retrieve the URL, the bucket is not s3-legacy-users but a different bucket. Is there a way to specify the bucket when using Storage::url() ?
$file = $request->file('file');
$fileName = $file->hashName();
$image = Image::make($file->getRealPath());
$newPath = '/'.shop_id().'/'.$imageType.'-'.$fileName;
$fileUpload = Storage::disk('s3-legacy-users')->put($newPath, $image->stream()->__toString());
$filePath = Storage::url($newPath);
dd($filePath);

You need to specify your disk if you have several storage drivers:
$filePath = Storage::disk('s3-legacy-users')->url($newPath);

Related

php-zip save extracted files in s3 laravel

If you are familiar with Ne-Lexa/php-zip, I want to upload all my extracted files in s3 bucket. I tried this code but instead, it is storing in my public folder instead of s3.
$file = $request->file('filename');
$filename = $file->getClientOriginalName();
$zipFile = new \PhpZip\ZipFile();
$zipFile->openFile($file)
->extractTo(Storage::disk('s3')->makeDirectory($directory));
$zipFile->close();

Writing temp files

I am currently trying to display the sharepoint thumbnail in a picturebox when i click a button on the form. What appears to be happening is the file is locked and will not let me replace the file or anything. I even created a counter so the file name is always different. When I run the first time everything works, after that I believe it cant write over the file. Am I doing something wrong is there a better method??
$User=GET-ADUser $UserName –properties thumbnailphoto
$Filename='C:\Support\Export'+$Counterz+'.jpg'
#$img = $Filename.Open( [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read )
[System.Io.File]::WriteAllBytes($Filename, $User.Thumbnailphoto)
$Picture = (get-item ($Filename))
$img = [System.Drawing.Image]::Fromfile($Picture)
$pictureBox.Width = $img.Size.Width
$pictureBox.Height = $img.Size.Height
$pictureBox.Image = $img
$picturebox.dispose($Filename)
Remove-Item $Filename
You should be able to do this without creating a temporary file.
Just create $img like:
$img = [System.Drawing.Image]::FromStream([System.IO.MemoryStream]::new($User.thumbnailPhoto))
$pictureBox.Width = $img.Width
$pictureBox.Height = $img.Height
$pictureBox.Image = $img
Don't forget to remove the form from memory after closing with $form.Dispose()
If you insist on using a temporary file, then be aware that the $img object keeps a reference to the file untill it is disposed of.
Something like:
# get a temporary file name
$Filename = [System.IO.Path]::GetTempFileName()
[System.IO.File]::WriteAllBytes($Filename, $User.thumbnailPhoto)
# get an Image object using the data from the temporary file
$img = [System.Drawing.Image]::FromFile($Filename)
$pictureBox.Width = $img.Width
$pictureBox.Height = $img.Height
$pictureBox.Image = $img
$form.Controls.Add($pictureBox)
$form.ShowDialog()
# here, when all is done and the form is no longer needed, you can
# get rid of the $img object that still has a reference to the
# temporary file and then delete that file.
$img.Dispose()
Remove-Item $Filename
# clean up the form aswell
$form.Dispose()

How to store Excel file after generation on Laravel storage?

I use this code to generate binary file. How to store it in local storage?
$path = 'events/';
$filename = 'visitors_list_' . date('d-m-y') . '.xlsx';
return Excel::import(new VisitorsExport($request), $path.$filename);
I use library
The method is Excel::store().
According to this
Beware your return keyword will end the execution

Laravel : Store full image path in database

I want to store my image upload path in table.Currently it returns
/storage/images/user/business_card/1524811791.jpg"
I want to store full access URL for example abc.com/storage/images/user/business_card/1524811791.jpg
How can i do this ?
Image Upload Code :
if(Input::file('profile_picture'))
{
$profilepic = Input::file('profile_picture');
$filename = time() . '.' . $profilepic->getClientOriginalExtension();
$request->file('profile_picture')->storeAs('public/images/user/profile', $filename);
$accessUrl = Storage::url('images/user/profile'). '/' . $filename;
$url = Storage::put('public/user/profile/', $filename);
$saveUserInformation->profile_picture = $accessUrl;
$saveUserInformation->save();
}
First of all it is bad practice. Imagine a problem when your domain changes, then you need to make huge work in database. Therefore the relative path is better.
But If you realy need absolute path you can take domain:
{{ Request::root() }}
Or
{{ Request::server ("SERVER_NAME") }}
and add it before storage path.
Good luck!
$saveUserInformation->profile_picture = URL::to('/').$accessUrl;

How do I set the individual upload file names?

I have a Laravel 5.3 app that has a form which users can upload multiple files using multiple file fields. The form work in that the files can be uploaded and moed to the destinationPath as I expect but I can't seem to change each of the files 'filename' values. It keeps saving the filename value as the php**.tmp.
Here is the foreach in my controller;
$files = $request->files;
foreach($files as $file){
$destinationPath = 'images/forms'; // upload path
$filename = $file->getClientOriginalName(); // get image name
$file->move($destinationPath, $filename); // uploading file to given path
$file->filename = $filename;
}
If I dd($filename) and dd($file->filename) within the foreach I do get the value (original name) I am looking for but if I dd($files) outside that foreach, the filename is set as the temp php value.
What am I missing? Thanks.
EDIT
The file object looks like this;
-test: false
-originalName: "sample_header_1280.png"
-mimeType: "image/png"
-size: 51038
-error: 0
path: "C:\xampp\tmp"
filename: "php7240.tmp"
basename: "php7240.tmp"
pathname: "C:\xampp\tmp\php7240.tmp"
extension: "tmp"
realPath: "C:\xampp\tmp\php7240.tmp"
I am trying to save the originalName to the db but it seems to default to saving the filename.
Turns out using a foreach for Input::file is not he approach here. If uploading multiple files from the same field - then you'd use a foreach to loop, move and save.
To upload files from multiple file inputs on the same form all you need to do is treat each input individually - as you might with any other form.
In my example I did this in my controller;
$data['image1'] = Input::file('image1')->getClientOriginalName();
Input::file('image1')->move($destinationPath, $data['image1']);
$data['image2'] = Input::file('image2')->getClientOriginalName();
Input::file('image2')->move($destinationPath, $data['image2']);
Not sure this is the best approach (there's always another way) but it worked for me.

Resources