Forum Moderators: coopster

Message Too Old, No Replies

zip_entry_read returning false

         

Nutter

3:10 am on Jun 17, 2006 (gmt 0)

10+ Year Member



Windows server running Apache 2.0 and PHP5 and PHP4 both installed through the WAMP setup I did so that I can switch back and forth between the two versions. In PHP5 the following function works correctly. In PHP4 zip_entry_read returns false.

$zip_file = zip_open($_FILES['filename']['tmp_name']); 
while ($zip_entry = zip_read($zip_file))
{
if (preg_match('/jpg$/i', zip_entry_name($zip_entry))>0)
{
echo "File Name: ".zip_entry_name($zip_entry)."<br>";
echo "File Size: ".number_format(zip_entry_filesize($zip_entry))."<br>";
$image_data = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
echo "strlen(imagedata): ".strlen($image_data)."<br>";
echo "Image Data: ".$image_data;
die();
$fhandle = fopen($tmp_directory.zip_entry_name($zip_entry), 'wb');
fwrite($fhandle, $image_data);
fclose($fhandle);
}
}
zip_close($zip_file);
}

PHP4 Output:

File Name: 20060401-0067.jpg 
File Size: 66,368
strlen(imagedata): 0
Image Data:

PHP5 Output:

File Name: 20060401-0067.jpg 
File Size: 66,368
strlen(imagedata): 66368
Image Data: (jpeg data)

Any thoughts on why it might work on PHP5 and not PHP4?

ArthurDent

10:43 am on Jun 17, 2006 (gmt 0)

10+ Year Member



quote from php.net: "When reading from a zip file if the initial call to zip_entry_read() returns false it would be advisable to run mkdir with the file name since it it very likely that the zero length file was a directory and attempts to save the files within the directory will fail if you dont create it first "

if you're working with binary data you should always use the mb functions for string functions (like mb_strlen etc).

Nutter

11:41 am on Jun 17, 2006 (gmt 0)

10+ Year Member



It actually wound up being caused by not calling zip_entry_open() first. I just missed a step :-) For whatever reason it worked fine in PHP5 without it but wouldn't work without it in PHP4.