...

View Full Version : What is this file handling error ?



jeddi
01-26-2010, 07:41 PM
Hi,
I am trying to download a file and unzip it
but I get this error

Could not open /home/guru54gt5/public_html/sys/clickbank.zip
Error # 11

I keep a log which opens at the start
of the code (not shown).

This is my code:



$target_url = "http://www.clickbank.com/feeds/marketplace_feed_v1.xml.zip";
$userAgent = 'EasyDL/3.xx';

$file_zip = "clickbank.zip";


$cef = "curl_err.txt";
$ceh = fopen($cef, 'w');

// write to log
$content = "Target_url: $target_url\r\n";
fwrite($handle, $content);

// make the cURL request to $target_url
$ch = curl_init();
$fp = fopen("$file_zip", "wb");
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_STDERR, $ceh);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER,0);
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) {
$content = "CURL error number: curl_errno($ch)\r\n CURL error: curl_error($ch)\r\n";
fwrite($handle, $content);
exit;
}

curl_close($ch);

$fsz = filesize($file_zip);

$content = "Downloaded file: $target_url\r\n Saved as file: $file_zip\r\n File size: $fsz bytes \r\n";
fwrite($handle, $content);

// Un zip the file

// After curl code:
fclose($fp);
fclose($ceh);

$file_zip = '/home/guru54gt5/public_html/sys/'.$file_zip;

if (!class_exists('ZipArchive')) {
$content = "Class ZipArchive not found\r\n";
fwrite($handle, $content);
exit;
} else {
$zip = new ZipArchive;
}

if (($err = $zip->open($file_zip)) !== true) {
$content = "Could not open $file_zip\r\n Error # $err\r\n";
fwrite($handle, $content);
exit;
} else {
$content = "File opened\r\n";
fwrite($handle, $content);
}

if (!$zip->extractTo($file_dir)) {
$content = "Extraction error\r\n";
fwrite($handle, $content);
}

$zipSS = $zip->getStatusString();
$content = "StatusString: $zipSS\r\n";
fwrite($handle, $content);

if (!$zip->close()) {
$content = "Error on close\r\n";
fwrite($handle, $content);
} else {
$content = "Unzipped file to: $file_dir\r\n";
fwrite($handle, $content);
}


$file = $file_dir.'clickbank.xml';

exit;


My log file shows this:


Notice that the filesize() doesn't appear to work - probably no file ?


New record - Time Stamp: 11:06:33 Tuesday, 26 January 2010
This script: sys/auto_cb_update.php, This file: /home/guru54gt5/public_html/sys/a_log_cb_update.txt
Target_url: http://www.clickbank.com/feeds/marketplace_feed_v1.xml.zip
Downloaded file: http://www.clickbank.com/feeds/marketplace_feed_v1.xml.zip
Saved as file: clickbank.zip
File size: filesize(clickbank.zip) bytes
Could not open /home/guru54gt5/public_html/sys/clickbank.zip
Error # 11


And the error file (curl_err.txt) shows:



* About to connect() to www.clickbank.com port 80
* Trying 74.63.153.33... * connected
* Connected to www.clickbank.com (74.63.153.33) port 80
> GET /feeds/marketplace_feed_v1.xml.zip HTTP/1.1
User-Agent: EasyDL/3.xx
Host: www.clickbank.com
Accept: */*

< HTTP/1.1 200 OK
< Date: Tue, 26 Jan 2010 18:34:58 GMT
< Server: Apache
< ETag: W/"2325066-1264507973000"
< Last-Modified: Tue, 26 Jan 2010 12:12:53 GMT
< Content-Length: 2325066
< Content-Type: application/zip
* Connection #0 to host www.clickbank.com left intact
* Closing connection #0


So does this mean that it did not download the zip file ?

If if didn't - what could be the problem ?

If you can see any obvious erros in my
code, I would appreciate any advice,
thanks.



.

Dormilich
01-26-2010, 07:56 PM
from first glance I’d say the file is not where you expect it to be.

tomws
01-26-2010, 08:05 PM
Maybe different than a location error. See the manual page (http://php.net/manual/en/function.ziparchive-open.php). One of the comments lists these return values:

ZIPARCHIVE::ER_EXISTS - 10
ZIPARCHIVE::ER_INCONS - 21
ZIPARCHIVE::ER_INVAL - 18
ZIPARCHIVE::ER_MEMORY - 14
ZIPARCHIVE::ER_NOENT - 9
ZIPARCHIVE::ER_NOZIP - 19
ZIPARCHIVE::ER_OPEN - 11
ZIPARCHIVE::ER_READ - 5
ZIPARCHIVE::ER_SEEK - 4Which doesn't really seem very helpful, but maybe it'll offer some direction.

jeddi
01-26-2010, 08:55 PM
Thanks for your reply.

So you think it is a problem with the unzipping ?

It seems to me that there is a problem with the download
of the zip file.

I slightly changed the script
- I have added a couple of lines to check
if the zip file exists:


if(file_exists($file_dir.$file_zip)) {
$fsz = filesize($file_dir.$file_zip);
}
else {
$fsz = "No file";
}

$content = "Downloaded file: $target_url\r\n Saved as file: $file_zip\r\n File size: $fsz bytes \r\n";
fwrite($handle, $content);


The output from this in my log file is:


This script: sys/auto_cb_update.php, This file: /home/guru54gt5/public_html/sys/a_log_cb_update.txt
Target_url: http://www.clickbank.com/feeds/marketplace_feed_v1.xml.zip
Downloaded file: http://www.clickbank.com/feeds/marketplace_feed_v1.xml.zip
Saved as file: clickbank.zip
File size: No file bytes
Could not open /home/guru54gt5/public_html/sys/clickbank.zip
Error # 11


I think File size: No file bytes shows that
there is no file ?

What of the curl error file ?
Does it imply that the download was OK ?

But, then if there was any download error
shouldn't I get an error code from this line: if (!$page) { ?



.

tomws
01-26-2010, 09:13 PM
That certainly seems to imply that the file doesn't exist. Maybe you should test the result of the fopen for $fp (at the top of the curl_opts). You may also want to look at testing the response code (for 200/success). I noticed that possibility in the comments for curl_exec.

jeddi
01-26-2010, 09:21 PM
Not quite sure what you mean by:


test the result of the fopen for $fp

and how do I do this ?


look at testing the response code (for 200/success).

Thanks

tomws
01-26-2010, 09:25 PM
I meant to test the return value from fopen (http://php.net/manual/en/function.fopen.php). It return FALSE on error.

For testing the response code, check out this comment (http://us.php.net/manual/en/function.curl-exec.php#87015) for a start. I think there were others there, too.

jeddi
01-27-2010, 07:48 AM
Thanks for your suggestions and the link.

I have now added some more tests:


if ($fp === FALSE ) {
$content = "Problem opening $file_zip\r\n";
fwrite($handle, $content);
exit;
}

and


$output = curl_exec($ch);
$info = curl_getinfo($ch);

if ($output === false || $info['http_code'] != 200) {
$content = "No CURL data returned for $target_url [". $info['http_code']. "]\r\n ";
if (curl_error($ch))
$content = $output."CURL error number: curl_errno($ch)\r\n CURL error: curl_error($ch)\r\n";
fwrite($handle, $content);
exit;
}
else {
$content = "CURL completed successfully.\r\n";
fwrite($handle, $content);
}


Hopefully that should help.

( Do you see anything wrong there ? )


.

jeddi
01-27-2010, 08:28 AM
Hmm .. I am still getting a file open error...





( OPEN the log file code here )

$file_zip = "clickbank.zip";


$cef = "curl_err.txt";
$ceh = fopen($cef, 'w');

$content = "Target_url: $target_url\r\n";
fwrite($handle, $content);

// make the cURL request to $target_url
$ch = curl_init();
$fp = fopen($file_zip, "wb");

if ($fp === FALSE ) {
$content = "Problem opening $file_zip\r\n";
fwrite($handle, $content);
exit;
}


I get the "Problem opening clickbank.zip"
in my log file.

Now why would that happen ?

I even tried deleting the file with unlink first as well
by placing this at the top of the script:


if(file_exists($file_dir.'clickbank.zip')) unlink($file_dir.'clickbank.zip');


But I still get the same error !

According t the Manual: fopen("filename", "w");

Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

So if the file exists or not, it should be OK


Is this line:
if ($fp === FALSE ) {
the correct thing to do ?


BTW
I also tried: $fp = fopen($file_zip, "wb"); // took quotes off
but no difference :(


.

tomws
01-27-2010, 03:38 PM
I think you're testing the fopen result correctly. Is your PHP in safe mode? There's a note on the fopen page:

Note: When safe mode is enabled, PHP checks whether the directory in which the script is operating has the same UID (owner) as the script that is being executed.

Does the web server user have write access to the directory? Besides a simple look-see, you can also test this with is_writable (http://php.net/manual/en/function.is-writable.php).

jeddi
01-27-2010, 09:20 PM
Thanks Tom,
I appreciate your help (as always).

When I added the full path to the file name, the problem
went away. It is still a bit weird because I did not do that before and
it worked. Any way, it works norks now :)

BTW
do you know ...

is if($fp === FALSE ) a valid test ?

and is it the same as if(!$fp) ?




.

tomws
01-27-2010, 09:47 PM
Since the fopen return value is either a file pointer or false, then if ($fp === FALSE) is a good test. The second would work, too.

It's good practice to test for an exact value (FALSE, in this case) if you know what will be returned, so I'd use the first one. There are some functions besides fopen where just testing the NOT would yield inconsistent results because of all of the values that are equivalent in loose-typing (0, '', etc.). That's the reason it's good practice - when you get in the habit of testing exact return values where applicable, you can save a few headaches.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum