Solved

What am I missing in this curl and unzip?

Posted on 2014-01-21
11
625 Views
Last Modified: 2014-01-21
Here is a very simple program to download and unzip a file

<?PHP
$appNumber = "12102391";
$payload = file_get_contents('http://storage.googleapis.com/uspto-pair/applications/'.$appNumber.'.zip'); //grab the file from the remote server
$target = "."; // this is the destination for the unzipped files

openZip($payload,$appNumber); 

function openZip($file_to_open,$appNumber) { 
    global $target;
    $file = $appNumber.'.zip';
    $client = curl_init($file_to_open);
    curl_setopt($client, CURLOPT_RETURNTRANSFER, 1);  
    $fileData = curl_exec($client);

echo("---\n".$file."-".$fileData."\n---\n");

    file_put_contents($file, $fileData);

    @unlink($file);
    touch($file);
    $zip = new ZipArchive(); 
    $f = $zip->open($file);
    var_dump($f);
    $zip->extractTo($appNumber);
    $zip->close();
    var_dump(glob($appNumber.'/*'));
}
?>

Open in new window


It creates a file and a dir but the file is empty and so is the dir

If I echo $file_to_open I actually see the file contents but I want to write the zip - actually I do not need to write the zip, I just want the unzipped contents in a dir named after the file so if I can unzip without saving the file, I would be quite happy too.
0
Comment
Question by:Michel Plungjan
  • 5
  • 4
  • 2
11 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39797608
Michel: PHP ZipArchive is full of gotchas.  I found it very buggy with cross-release failures.

If you can get the file downloaded to a Windows7 desktop, you can copy the contents of the zip file to another directory and Windows will unzip it for you in the process.

The comments on this page and the linked pages tell something of a tale of woe.
http://php.net/manual/en/class.ziparchive.php
0
 
LVL 58

Expert Comment

by:Gary
ID: 39797631
The zip file has to be saved, you cannot use ZipArchive with an in memory object.
Here's a clue to why you have a blank file...
@unlink($file);
0
 
LVL 75

Author Comment

by:Michel Plungjan
ID: 39797632
Thanks for the reply

I need to download any zip on demand from that source. Later I will produce an HTML file with links to the pdfs in the zip

I can easily download and unzip using windows

The problem does not seem to be the unzipping (yet) but the download and saving.

I am doing something wrong in the Curl for now
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 75

Author Comment

by:Michel Plungjan
ID: 39797636
I was suspicious of the unlink but I have now commented it out. No change to the results
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39797659
Michel, please describe the app in layman's terms.  You read from USPTO, but what are you trying to do with cURL?
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39797674
Have a look at the output from just this script.  It looks like some of the data is ZIP and some is clear text.

<?php // RAY_temp_mplungjan.php
error_reporting(E_ALL);

$appNumber = "12102391";
$payload = file_get_contents('http://storage.googleapis.com/uspto-pair/applications/'.$appNumber.'.zip'); //grab the file from the remote server
var_dump($payload);
die();

Open in new window

0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39797692
<?PHP
$appNumber = "12102391";
$payload = file_get_contents('http://storage.googleapis.com/uspto-pair/applications/'.$appNumber.'.zip');

$file = $appNumber.'.zip';
file_put_contents($file,$payload);

openZip($file,$appNumber);

function openZip($file_to_open,$appNumber) {
    $zip = new ZipArchive();
    $f = $zip->open($file_to_open);
    var_dump($f);
    $zip->extractTo($appNumber);
    $zip->close();
}
?>
0
 
LVL 75

Author Closing Comment

by:Michel Plungjan
ID: 39797760
Perfect - thanks
0
 
LVL 75

Author Comment

by:Michel Plungjan
ID: 39797776
Ray: I thought I needed CURL to get the file. I was mixing my tiny knowledge of get_contents, thinking I needed curl for binary contents

The program is supposed to - given an application number, access the zipfiles from google, unzipping and presenting a clickable list of links to them
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39798039
Where cURL is more useful than file_get_contents() is when you do not want to have a dependency on the remote resource.  If the remote resource hangs, your script hangs until PHP times out and produces a fatal error - maybe 30 seconds in the future.  With cURL you can set a much shorter timeout and you can add error handling to degrade gracefully.
0
 
LVL 75

Author Comment

by:Michel Plungjan
ID: 39798131
ah! thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
paypal ipn to mysql 3 36
PHP and google maps 13 44
How to init a part of Javascript based on PHP value for Datatables & Yadcf 8 48
Curl & PHP Command Help 4 23
This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question