0

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
Maneesh Rao
  • 184
  • 4
  • 23
  • Refer to this question to unzip in PHP, worked for me: https://stackoverflow.com/questions/8889025/unzip-a-file-with-php – yomisimie Jul 03 '18 at 14:36
  • But, How can I store the file first on public folder? – Maneesh Rao Jul 03 '18 at 14:38
  • Your code does not work because the `$zip` variable needs to have a name too, not just only the path. Try somthing like `$zip = realpath('phonelist/').'zipped.zip'` Also, try with `fopen` if `file_get_contents` does not work – yomisimie Jul 03 '18 at 14:40
  • Yes, this is working but getting permission issue while unzip the folder. – Maneesh Rao Jul 03 '18 at 15:14
  • Check what permissions does the file has when PHP is saving it. It should be 755. – yomisimie Jul 03 '18 at 15:16
  • When file is created it is read only showing. ZipArchive::extractTo(): Permission denied – Maneesh Rao Jul 03 '18 at 15:20
  • Maybe the path for the extraction is protected or not written correctly. That seems to be a server permissions issue. – yomisimie Jul 03 '18 at 15:26
  • I have given the permission 777 on public folder. – Maneesh Rao Jul 03 '18 at 15:26
  • Not a good thing to do. PHP should run fin with 755 permissions. Are you sure the folder you wrote into the extractTo() is correct? – yomisimie Jul 03 '18 at 15:28
  • Ok, I have update the code. please see the updated code above on question section. – Maneesh Rao Jul 03 '18 at 15:40
  • What does "but nothing work" mean? Can you add to the question what happens instead and what you've tried to debug this? – Nico Haase Jul 03 '18 at 15:41
  • @NicoHaase Hi Nico, I have update the question and getting issue. ZipArchive::extractTo(): Permission denied – Maneesh Rao Jul 03 '18 at 15:42
  • Please add every relevant information to the question, not to the comment section. And what have you tried to get around that error message? It's obvious that your permissions don't make up – Nico Haase Jul 03 '18 at 15:43
  • @NicoHaase I have update the question. Hope you will understand now. – Maneesh Rao Jul 03 '18 at 15:46
  • So, you still got that error about file permissions? What have you tried to resolve this? Does the folder `/phoneList` exist? Does the server process have the permission to write to that folder? – Nico Haase Jul 03 '18 at 19:22

1 Answers1

0

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!

JP. Aulet
  • 4,375
  • 4
  • 26
  • 39