[PHP] Zip extracten

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Hallo,

Ik wil via een php script een "geuploade" zip-file uitpakken in een map. Ik probeer echter eerst voor de makkelijkheid een zip uit te pakken die ik al op de server heb staan. Heel basic php, maar op de een of andere manier krijg ik het niet voor elkaar :? Wat doe ik fout?

Ik krijg echter al meteen de melding "Zip-bestand kan niet geopend worden." Het volledige pad invullen werkt ook niet. De zip-file is goed gechmod, dus ALS het goed is, moet php wel de rechten tot dit bestand hebben.

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$zip = new ZipArchive(); 
if ($zip->open('test.zip') !== TRUE) { 
    die ("Zip kan niet geopend worden"); 
} 

// get number of files in archive 
$numFiles = $zip->numFiles; 

// iterate over file list 
// print details of each file 
for ($x=0; $x<$numFiles; $x++) { 
    $file = $zip->statIndex($x); 
    printf("%s (%d bytes)", $file['name'], $file['size']); 
    print " 
";    
} 

// close archive 
$zip->close();

Acties:
  • 0 Henk 'm!

  • Osiris
  • Registratie: Januari 2000
  • Niet online
Je zou de return value van de open()-functie kunnen afvangen/echo-en/whatever, dan heb je wat meer duidelijkheid wát er mis gaat.

Acties:
  • 0 Henk 'm!

  • Borizz
  • Registratie: Maart 2005
  • Laatst online: 24-08 20:35
staat je zip bestand wel in dezelfde map als je php script (of de map van waaruit je het php script aanroept indien je het vanaf de command line draait)? Verder inderdaad wat Osiris al zegt kijken wat de exacte error is die je terug krijgt.

[ Voor 22% gewijzigd door Borizz op 27-08-2008 21:47 ]

If I can't fix it, it ain't broken.


Acties:
  • 0 Henk 'm!

  • TheDane
  • Registratie: Oktober 2000
  • Laatst online: 22:34

TheDane

1.618

Kun je vanaf de commandline de zip wel openen? (dus gewoon met unzip, gunzip, tar, of-weet-ik-wat)

[ Voor 36% gewijzigd door TheDane op 27-08-2008 21:52 ]


  • r0bert
  • Registratie: September 2001
  • Laatst online: 30-07 02:32
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class cUploadTypeZip extends cUploadType
        {       private $__maxSizeBytes = 104857600; // 100MB upload size
                private $__archiveFile,
                                                $__archive;

                public function __handle($oFile)
                {       // Check the maximum upload size
                        if ($oFile['size'] > $this->__maxSizeBytes)
                                return $this->setError('File is too big');
                        else
                        {       $this->__archiveFile = zip_open($oFile['tmp_name']);

                                        // Make sure it's a valid ZIP-archive
                                if ($this->__archiveFile)
                                {               // Foreach file in the archive ..
                                        while ($oArchive = zip_read($this->__archiveFile))
                                        {       $this->__archive[count($this->__archive)] = $oArchive;

                                                        // Try to open the ZIP-archive. If succesfull, proceed with constructing the filename ..
                                                if (zip_entry_open($this->__archiveFile, $oArchive, 'r'))
                                                {
                                                                if ($oOutput = fopen ($sFile, 'wb'))
                                                                {
                                                                        // If no file is written, an error should be added
                                                                        if (!fwrite($oOutput, zip_entry_read($oArchive, zip_entry_filesize($oArchive))))
                                                                                $this->setError(zip_entry_name($oArchive).': Kon bestand niet overschrijven naar outputbestand');
                                                                       fclose($oOutput);
                                                                        @chown($sFile, 'spider');
                                                                        @chgrp($sFile, 'psacln');
                                                                        @chmod($sFile, 0777);
                                                                }

                                                                        // If no file could be created
                                                                else
                                                                        $this->setError('Kon outputbestand niet aanmaken.');
                                                        }
                                                                // Close the ZIP-archive
                                                        zip_entry_close($oArchive);

                                                                // Try to remove it
                                                        unlink ($oFile['tmp_name']);
                                                }
                                        }
                                }
                        }
                }
        }

Een cut uit bestaande code, dus moet je nog wel even checken.

Edit: $sOuput bijvoorbeeld.
Het extended object btw:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 class cUploadType
        {       private $__error;

                public function __construct()
                {
                }

                protected function setError($sError = 'Uknown error')
                {       $this->__error[count($this->__error)] = $sError;
                        return false;
                }

                public function getError()
                {
                }

                public function getErrors()
                {       if (count($this->__error) <= 0)
                                return false;
                        else
                                return $this->__error;
                }
        }

[ Voor 12% gewijzigd door r0bert op 28-08-2008 10:44 ]