I am getting the response by Third party API call. They providing me a file_url in response. If I hitting the file_url in browser URL got the zip file on local machine.
stdClass Object
(
[start_date] => 2018-06-01 08:00:00
[report_date] => 2018-07-02
[account_name] => Maneesh
[show_headers] => 1
[file_url] => https:examplefilename
[date_range] => 06/01/2018 - 07/03/2018
)
How can I download the zip file on public/phoneList folder on server and unzipped the file?
$fileUrl = $rvmDetails->file_url;
$zip = realpath('/phoneList/').'zipped.zip';
file_put_contents($zip, file_get_contents($fileUrl));
$zip = new \ZipArchive();
$res = $zip->open('zipped.zip');
if ($res === TRUE) {
$zip->extractTo('/phoneList/'); // phoneList is folder
$zip->close();
} else {
echo "Error opening the file $fileUrl";
}
The above code works. but getting issue while unzipp the folder.
ZipArchive::extractTo(): Permission denied
With the PHP
build-in system you can open/extract zips in a certain path and download it with CURL (you should create a filename too), like:
$fileUrl = $obj->file_url;
$fileName = date().".zip"; //create a random name or certain kind of name here
$fh = fopen($filename, 'w');
$ch = curl_init()
curl_setopt($ch, CURLOPT_URL, $fileUrl);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // this will follow redirects
curl_exec($ch);
curl_close($ch);
fclose($fh);
// get the absolute path to $file
$path = pathinfo(realpath($filename), PATHINFO_DIRNAME);
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
$zip->extractTo($path);
$zip->close();
} else {
echo "Error opening the file $file";
}
You can find more info here: download a zip file from a url and here: Unzip a file with php
Hope it helps!