I am using curl to down load a zip file and I am
then trying to unzip it, but I don't think that I am
using the functions properly because I get warnings
This is my code: -
<?php
-
-
if (!extension_loaded('zip')) {
-
dl('zip.so');
-
}
-
-
$func_path = "/home/guru54gt5/public_html/im/my_functions.php";
-
require_once("$func_path");
-
-
$target_url = "http://www.support-focus.com/test_map.zip";
-
$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
-
-
echo "<br>Starting<br>Target_url: $target_url<br><br>";
-
-
// make the cURL request to $target_url
-
$ch = curl_init();
-
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
-
curl_setopt($ch, CURLOPT_URL,$target_url);
-
curl_setopt($ch, CURLOPT_FAILONERROR, true);
-
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
-
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
-
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
-
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
-
$page = curl_exec($ch);
-
if (!$page) {
-
echo "<br />cURL error number:" .curl_errno($ch);
-
echo "<br />cURL error:" . curl_error($ch);
-
exit;
-
}
-
curl_close($ch);
-
-
// Un zip the file
-
-
$zip = new ZipArchive;
-
if (! $zip) {
-
echo "Could not make ZipArchive object.";
-
exit;
-
}
-
$zip->open("$page");
-
$zip->extractTo('./');
-
$zip->close();
-
echo "Ok!";
-
?>
-
-
The errors I get are in fact warnings:
Warning: ZipArchive::extractTo() [function.ZipArchive-extractTo]: Invalid or unitialized Zip object in /home/guru54gt5/public_html/sys/convert_xml_no1.php on line 40
Warning: ZipArchive::close() [function.ZipArchive-close]: Invalid or unitialized Zip object in /home/guru54gt5/public_html/sys/convert_xml_no1.php on line 41
Ok!
Line 40 and 41 are: -
$zip->extractTo('./');
-
$zip->close();
The script is here: test script
Is the problem because my $page is not a file ?
I am a bit confused about this so would appreciate some help.
Thanks.
3 3463 Atli 5,058
Expert 4TB
Hey.
If you want the curl_exec function to actually return the results, you need to set the CURLOPT_RETURNTRANSFER option. If you do not, the results will be printed to the browser. (Which is why you get all those random characters on your page.)
Two questions tho. - Why do you use Curl for this? Couldn't you just as well use something like file_get_contents? (Assuming your configuration allows it to use URLs)
- Why do you have the request identify itself as Googlebot?
Hi,
Thanks for your reply.
I am using curl because I want to get used to using it and it is more powerful and faster than file_get_contents ( according to what I read)
The Google_bot - well no reason really, it was in the code that
I took as my example.
I have moved forward with my script
but I am still getting errors.
This is what I have: - <?php
-
-
if (!extension_loaded('zip')) {
-
dl('zip.so');
-
}
-
-
$func_path = "/home/guru54gt5/public_html/im/my_functions.php";
-
require_once("$func_path");
-
-
$target_url = "http://www.support-focus.com/test_map.zip";
-
$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
-
$file_zip = "new.zip";
-
$file_txt = "new.txt";
-
-
echo "<br>Starting<br>Target_url: $target_url<br><br>";
-
-
-
// make the cURL request to $target_url
-
$ch = curl_init();
-
$fp = fopen("$file_zip", "w");
-
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
-
curl_setopt($ch, CURLOPT_URL,$target_url);
-
curl_setopt($ch, CURLOPT_FAILONERROR, true);
-
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
-
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
-
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
-
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
-
curl_setopt($ch, CURLOPT_FILE, $fp);
-
-
$page = curl_exec($ch);
-
-
-
if (!$page) {
-
echo "<br />cURL error number:" .curl_errno($ch);
-
echo "<br />cURL error:" . curl_error($ch);
-
exit;
-
}
-
curl_close($ch);
-
-
echo "<br>Downloaded file: $target_url";
-
echo "<br>Saved as file: $file_zip";
-
echo "<br>About to unzip ...";
-
-
// Un zip the file
-
-
$zip = new ZipArchive;
-
if (! $zip) {
-
echo "Could not make ZipArchive object.";
-
exit;
-
}
-
$zip->open("$file_zip");
-
$zip->extractTo("$file_txt");
-
$zip->close();
-
-
echo "<br>Unzipped file to: $file_txt<br><br>";
-
-
?>
-
The result:
Starting
Target_url: http://www.support-focus.com/test_map.zip
Downloaded file: http://www.support-focus.com/test_map.zip
Saved as file: new.zip
About to unzip ...
Warning: ZipArchive::extractTo() [function.ZipArchive-extractTo]: Invalid or unitialized Zip object in /home/guru54gt5/public_html/sys/convert_xml_no2.php on line 52
Warning: ZipArchive::close() [function.ZipArchive-close]: Invalid or unitialized Zip object in /home/guru54gt5/public_html/sys/convert_xml_no2.php on line 53
Unzipped file to: new.txt
Even though it says that it has unzipped the file,
it doesn't. :confused:
The new version is on: New Version Anyone know what I am doing wrong ? Atli 5,058
Expert 4TB
Ok, it seems the $zip->open() call is failing, but because you don't capture and test the return value of the method, the code just moves on and fails when you try to use the unopened zip object.
You need to capture the error to see what is causing the problem.
Try something like: - $zip = new ZipArchive();
-
if($zip) {
-
$result = $zip->open($file_zip);
-
if($result === true) {
-
$zip->extractTo($file_txt);
-
$zip->close();
-
echo "Success!";
-
}
-
else {
-
echo "Failed to open file at '$file_zip'. Error code: " . $result;
-
}
-
}
-
else {
-
echo "Could not make ZipArchive object.";
-
}
-
$zip = null;
P.S.
You can see the error codes in the comments on the ZipArchive:open manual entry.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Haluk Durmus |
last post by:
Hello
I checked out openssl,mm,apr,apr-util,apache 2,curl,libxml and php
from cvs.
php couse an ERROR
I did the following steps:
|
by: Phil Powell |
last post by:
What is the most standardized method of utilizing the CURL functions
in PHP (version 4.3.2) to be able to retrieve the contents of a remote
URL that happens to be dependent upon $_SESSION for its...
|
by: Chuck Anderson |
last post by:
I am writing a Php script to run on my home PC (Windows) that downloads
an Apache access log file and inserts new entries into a database.. The
only way I can access these log files is through a...
|
by: Wescotte |
last post by:
I'm writing a tiny php app that will log into our bank of america
account and retrieve a file containing a list of checks that cleared
the previous day. The problem I'm running into is when I...
|
by: nfhm2k |
last post by:
I've been trying to find a solution to this for quite some time now...
I even took a look at existing scripts...
Including this one......
|
by: BinnyVA |
last post by:
Hi,
I am using PHP 5.1.2 with curl enabled. But whenever I try to use curl
to fetch a url, it fails - 'curl_exec()' returns nothing.
But if I try to execute the same file in CLI - like 'php...
|
by: Terry |
last post by:
I'm using curl to invoke a php script on the same site/server. It works
great, but if I call it again while it's still running, nothing happens.
Why? Can that be fixed?
Why use curl? To make...
|
by: xerc |
last post by:
I am trying to create a generic function I can call to download all files from a single remote FTP directory -- using CURL. I want to multi-thread it, but need to get the single thread functionality...
|
by: Flexor |
last post by:
I have a php script that runs from command line and makes an https
request to paypal, using curl. It works fine if I run it from a web
page. It fails if I run it from CLI. The error I get from...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |