0
votes

I want to upload a zip file and extract it to a folder using php. I am using this code:

$zip = new ZipArchive;              
if ($zip->open($filename) == TRUE){                     
     $zip->extractTo('Articles/');          
     $zip->close();                     
     $isql="update articles set a_file='$newfile' where a_id='$fpk'"; 
     $urec=mysql_query($isql);                                          
} 
else {                  
     echo 'failed';                 
} 

But it is showing these warnings:

Warning: ZipArchive::extractTo() [ziparchive.extractto]: Invalid or unitialized Zip object in D:\xampp\htdocs\Constructor\adminhome.php on line 36

Warning: ZipArchive::close() [ziparchive.close]: Invalid or unitialized Zip object in D:\xampp\htdocs\Constructor\adminhome.php on line 37...

Shall I include the ziparchive library? If yes then how could I do this?

2
I think here is the mistake $zip = new ZipArchive();Amar Banerjee
Have you handled the file upload in HP and use move_uploaded_file() to move the file into the file system before attempting to open it?user1864610
no,i did not.i thought that it will upload and extract the files automatically...i think it the problem..Sougata Bose
You must have ZipArchive installed or you would have hit an error earlier. Have you checked that $filename actually exists?Robert Seddon-Smith
thanx Mike W.That was the problem.its done successfully.I just forgot to upload the file before extracting it.thats why the problem occured..Sougata Bose

2 Answers

0
votes

you should check if open method is returning TRUE with === (with type check), otherwise some error code returned from open method could evaluate to TRUE.

For instalation please folow this link

0
votes
Basically problem in our open method use bellow code 

$opened = $zip->open( $filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE );

I think you are using php 5.2.8 it does not work and gives this warning: