Heim  >  Artikel  >  Backend-Entwicklung  >  PHP-Ziparchiv

PHP-Ziparchiv

PHPz
PHPzOriginal
2024-08-29 13:00:23380Durchsuche

ZipArchive-Methode in PHP wird verwendet, um die Datei und ein neues Verzeichnis hinzuzufügen und die ZIP-Datei in PHP lesen zu können. ZipArchive ist eher eine Klasse als eine Methode. ziparchive enthält mehrere Methoden; Mithilfe dieser Funktion können wir verschiedene Operationen an der ZIP-Datei in PHP ausführen. ZipArchive-Methoden können mehrere Vorgänge ausführen, nämlich das Hinzufügen einer neuen Datei, das Hinzufügen eines neuen Verzeichnisses, das Schließen des Archivs, das Extrahieren des ZIP-Archivinhalts und das Öffnen eines ZIP-Archivs in PHP. Im kommenden Abschnitt werden wir im Detail sehen, wie wir die Ziparchive-Methode in PHP verwenden können.

WERBUNG Beliebter Kurs in dieser Kategorie HIVE – Spezialisierung | 7-Kurs-Reihe

Starten Sie Ihren kostenlosen Softwareentwicklungskurs

Webentwicklung, Programmiersprachen, Softwaretests und andere

Syntax

Jetzt besprechen wir die verschiedenen Syntaxen für ziparchive in PHP. Wie ich Ihnen bereits sagte, gibt es in PHP keine Methode wie ziparchive; Dies ist stattdessen eine Klasse, die mehrere Ziparchive-Methoden enthält, um auf die Zip-Datei zuzugreifen und die Zip-Datei zu lesen. Sehen wir uns zum besseren Verständnis der Methode unten etwas zur Syntax an;

1. So öffnen Sie ziparchive: Unten finden Sie die Syntax

ziparchive->open('your_file_name');

Wie Sie sehen, müssen wir nur den Dateinamen übergeben, den wir öffnen möchten. Diese Methoden können auch mithilfe der Ziparchive-Instanz aufgerufen werden.

2. So schließen Sie eine Datei: Nachfolgend finden Sie die Syntax

ziparchive->close();

Wie Sie sehen können, müssen wir nur die Methode close auf der Ziparchive-Instanz aufrufen, um eine beliebige Datei in PHP zu schließen.

Wie funktioniert die Ziparchive-Methode in PHP?

Wie wir wissen, wissen wir, dass die ZipArchive-Methode zum Lesen der Zip-Datei in PHP verwendet wird, aber ZipArchive ist keine Klasse, sondern eine Methode, sondern enthält mehrere Methoden, mit denen Vorgänge für ZipArchive ausgeführt werden können. Um mit ziparchive umzugehen, verwenden wir die Klasse ziparchive in PHP. Um eine Methode verwenden zu können, müssen wir die Instanz der Ziparchive-Klasse erstellen, aber mit ihrer Verwendung können wir problemlos jede Methode aufrufen. Wir können beliebige Vorgänge ausführen, indem wir eine Ziparchive-Instanz verwenden, z. B. das Öffnen einer Datei. Schließen Sie eine Datei, erstellen Sie eine neue Datei, erstellen Sie ein neues Verzeichnis, lesen Sie ZIP-Archive-Inhalte usw.

Lassen Sie uns jede Methode im Detail besprechen, um sie in PHP zu verwenden, siehe unten;

  1. statIndex: This method is used to get the entry of the file defined by using the index.
  2. setCompressionIndex: This method is used to define the comparison method.
  3. renameName: This method is used to rename the file.
  4. open: This method is used to open a zip file in PHP.
  5. setCommentName: This method is used to comment.
  6. unchangeAll: This method is used to revert all the changes that have been performed to the zip archive.
  7. setPassword: This method is issued to set the password for the zip archive.
  8. close: This method is used to close the zip archive but is currently being used.
  9. addEmptyDir: This method is used to add a new empty directory to the zip archive.
  10. addFromString: This method is used to a file to the zip archive.
  11. count: This method is used to count the number of files present in the archive.
  12. deleteName: This method is used to delete an entry from the zip archive based on the name.
  13. addFile: This method is issued to add a file in the zip archive. But we have to specify the path as well.
  14. statName: This method is used to get the details of the entry based on the name.
  15. replaceFile: This method is used to replace the file in the zip archive based on the path.
  16. locateName: This method is used to get the index of the entry in the zip archive.
  17. getNameIndex: This method is used to get the name of the entry from the zip archive based on the index.
  18. getCommentName: This method is used to get the comment of the entry based on the name.
  19. getFromIndex: This method is used to get the contents of the entry by sing its index in a zip archive.
  20. extractTo: This method is used to get or extract the contents of the zip archive in PHP.
  21. deleteIndex: This method is used to delete the entry from the zip archive by using the index.
  22. setEncryptionIndex: This method is used to set the encryption for the entry in the zip archive using its index.
  23. setArchiveComment: This method is used to set comments for the zip archive in PHP.
  24. setExternalAttributesName: This method is used to set the external attribute of the entry based on its name.
  25. setEncryptionName: This method is used to set the encryption of the entry of the zip archive based on the name.

To use this all this method, we need to have a ziparchive instance created in our program. After that, only we can call this method to perform any operations on the zip archive in PHP. Let’s see how to create a ziparchive instance in PHP see below;

Example:

$myzip = new ZipArchive();

This is s sample for beginners to use ziparchive in php; we will see some examples in the next section to better understand the ziparchive method in PHP.

Example of PHP ziparchive

In this example, we create one file inside the zip archive; after this, we are closing this file. A sample program for beginners to understand this better.

Code:

<?php
echo 'demo for ziparchive method in PHP !!!';
$zip = new ZipArchive;
echo 'opening zip file !!';
if ($zip->open('https://cdn.educba.com/path/mydem.zip') === TRUE) {
//adding file
$zip->addFile('https://cdn.educba.com/path/myzip/demofile.txt', 'mynewdemofile.txt');
//closing file
$zip->close();
echo 'file added successfully !!';
echo 'file close successfully !!'
} else {
echo 'error occured while cerating the file.'
echo 'process failed !!'
echo 'failed while creation !!';
}
?>

Output:

PHP-Ziparchiv

Conclusion

By using ziparchive methods, we can perform many operations on the ziparchive. This is the class with so many different methods available. With the help of this, we can create a file, directory, delete, open, and close our ziparchive in PHP.

Das obige ist der detaillierte Inhalt vonPHP-Ziparchiv. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:PHP-NamespaceNächster Artikel:PHP-Namespace