php-zip save extracted files in s3 laravel - 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();

Related

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()

Laravel: Storage::url() returning wrong bucket

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);

How to write csv file and download in browser?

In laravel 5.7 app I need to write csv file and download in in browsers download function using method :
\Response::download(
I try to upload it to tmp directory and checking value I see
$sys_get_temp_dir= sys_get_temp_dir();
line above has /tmp value
Next I save it as :
$dest_csv_file= $sys_get_temp_dir . '/box_rooms_'.time().'.csv';
$header = ["Id",... ];
$fp = fopen($dest_csv_file, "w");
fputcsv($fp, $header);
foreach ($storageSpacesCollection as $line) {
fputcsv($fp, $line);
}
fclose($fp);
But searching on my local ubuntu 18(on server I also have ubuntu) I found generated file as
/tmp/systemd-private-6a9ea6844b9c4c94883d23e4fb3e2215-apache2.service-shqCeo/tmp/box_rooms_1576324869.csv
That was very strange, as I do not know how read it from tmp path. Can it be done?
I think you need to pass Content-Type properly
$filePath= public_path(). "/upload/example.doc";
$headersContent = array('Content-Type: application/pdf',);
return Response::download($filePath, 'example.doc', $headersContent)

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.

how to zip multiple folders in codeigniter

I am trying to zip multiple folders in codeigniter.
here is my code
public function zip_files()
{
$this->load->library('zip');
$this->zip->compression_level = 0;
$path1 = '/my folder path';
$this->zip->read_dir($path1);
$path2 = '/my another folder path';
$this->zip->read_dir($path2);
$this->zip->download('my_upoads.zip');
}
but the above code zips only the $path2 folder.
how to zip multiple folders? this is my question
Thankyou,
syed
you can use clean data
$this->zip->clear_data();
example :
$name = 'my_bio.txt';
$data = 'I was born in an elevator...';
$this->zip->add_data($name, $data);
$zip_file = $this->zip->get_zip();
$this->zip->clear_data();
$name = 'photo.jpg';
$this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents
$this->zip->download('myphotos.zip');
You can use $this->zip->read_dir()
refer:
https://ellislab.com/codeigniter/user-guide/libraries/zip.html
$this->load->library('zip');
$path = '/path/to/your/directory/';
$this->zip->read_dir($path);
$this->zip->download('my_backup.zip');
you have to supply base folder then it will zip all folders, files within it in single zip file.

Resources