Zip file download system laravel - php

I'm using Laravel 8
I've uploaded a zip file then want to download it. But here is a problem. After file download, it's not extracting.
Here is my code for download zip file:
$product = Product::where('id',1)->first();
$file = $product->main_file;
return response()->download($file,'filename.zip');
But, if I manually copy this zip file from my project then extract it, it's extracting fine.
Or, if I doing this: return Redirect()->to($file); It also downloads the file and extracts well without any problem. But, it's not a proper way to download. Isn't it?
So, What can I do now?

if you're trying to download via a link try this:
Download
but file name should be saved somewhere.

Related

Laravel Streamed Response From Azure (Multiple Files)

I've been working on this setup but I cannot save the downloaded streamed response to my zip file. I am using ZipArchive package and currently when I use return on the one with the yellow arrow. It returns the correct pdf but when I try to put it in the zip folder. It doesn't recognize it as a pdf file and return null. I need to save multiple pdf file in the add Zip File but right now I'm trying with only one pdf for now.
Got it. What I did is convert it to a raw pdf file and read it using AddFromString.

error while downloading zip archive from server

php ZipArchive can't open zip file that gets downloaded from a server.
I have a zip file that I created with winrar and uploaded to my server, file location is here: http://myserver.com/uploads/test.zip It's a valid zip file
Here is my code
When I run this code all I get is
error: Not a zip archive.
It opens the file and extracts it but complains that file already exists even if I delete file.
the file that I get when I access http://myserver.com/update/test.zip get corrupted and I have no idea why, this same code that I pasted above worked just a week ago.
The problem is actually the .zip file. I downloaded it and I cannot open the file with WinZip. Please try to make a new archive and upload it again on your server.
The code looks okay so I think it just the file that is the problem.
UPDATE #1:
The .zip file is now correct. Try to delete the files before you download and extract the new files. You can use unlink("uploads/update.zip") maybe you also need to clear the uploads/temp directory first.
UPDATE #2:
The download worked now for me. Try to add this header:
header("Content-Transfer-Encoding: Binary");
Also don't forget to close the ZipArchive after extracting:
$zip->close();

Rename and saving remote image to server

i'm looking for a simple way to download remote images to my sever, rename and save image name to database.
I know how to download the file easily to my sever
copy('http://example.com/file.jpeg', '/tmp/file.jpeg');
But how to i rename the image while downloading or after download?
I would really appropriate if anyone can tell me a simple way to do this using php
In copy function just change file name:
copy('http://example.com/file.jpeg', '/tmp/any_file_name.jpeg');
I think this should do the Job
rename("/tmp/tmp_file.jpg", "/home/user/login/docs/my_file.jpg");
you have to do this after you downloaded it
or just change the file name while you dwonload it e.g.
copy('http://example.com/file.jpeg', '/tmp/some_name.jpg');

Copy a zip file from an external website to my server

I just want to copy this zip file to my server which is in an external website.
A newfile.zip is created each time but this file is empty.
I guess it's because of the format of the url, can i do something ?
<?php
$thezipfile = "http://www.tyre24.fr/user/login/userid/test/password/test/page/L2V4cG9ydC9kb3dubG9hZC90L01RPT0vYy9NVEU9Lw==";
file_put_contents('newfile.zip',file_get_contents($thezipfile));
?>
The link works.
You could always just upload the file and then put the link to the direct download in your site:
Download Project 1 (zip-file, ...kB)
Download Project 2 (zip-file, ...kB)

adding remote files to a zip file

Is there a way to add files to a zip file from another server with php's zip extension? ie.
addFile(array('localfile.txt,'http://www.domain.com/remotefile.txt'))
//(that obviously does not work)
I suppose I can download the files to a temporal folder and then add them to the zip file, but I was looking for a more automated solution or a function already made
use file_get_contents() and ZipArchive::addFromString()
$zipArchiveInstance->addFromString($filename, file_get_contents($mediaUrl));
This writes the contents fetched remotely straight into your php object (no need to write/read temp file)
It's not hard to read remote files in PHP.
file_get_contents("http://example.com/remote.txt");
Or to copy them locally:
copy("http://example.com/remote.txt", "/tmp/local.txt");
Whichever way you do it, the contents are going to have to be transferred to a local temp folder or memory before you can do anything with them.
Fetch them with cURL, add them from TEMP directory.

Categories