open($filename, ZipArchive::CREATE)!==TRUE) { exit("cannot open $filename\n"); }"> open($filename, ZipArchive::CREATE)!==TRUE) { exit("cannot open $filename\n"); }">
Download presentation
Presentation is loading. Please wait.
Published byAndrea Allison Douglas Modified over 9 years ago
1
15 – PHP(5) Informatics Department Parahyangan Catholic University
2
Sometimes we need to work with zip files, for example: compressing several files to be downloaded at once uncompressing a zip file that the user uploaded etc. PHP version 5.2 and later comes with ZipArchive class
3
To create a new archive, first create an object of ZipArchive class: Then we open the new archive with the open() method: $zip = new ZipArchive(); $filename = "abc.zip"; if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) { exit("cannot open $filename\n"); }
4
There are 4 modes available when opening an archive: ZipArchive::OVERWRITE Always start a new archive, this mode will overwrite the file if it already exists. ZipArchive::CREATE Opens an existing zip file, or create a new archive if it does not exist. ZipArchive::EXCL Error if archive already exists. ZipArchive::CHECKCONS Perform additional consistency checks on the archive, and error if they fail (eg. broken zip, corrupted upload, etc.)
5
The addFile() method adds a file to an opened archive The close() method closes the active archive and saves changes $zip->addFile("Document1.txt"); $zip->addFile("Document2.txt"); $zip->addFile("Document3.txt"); $zip->close();
6
The addFile() method can give a new name for the added file by providing the second argument Example: $zip->addFile("Document1.txt", "File 1.txt"); $zip->addFile("Document2.txt", "File 2.txt"); $zip->addFile("Document3.txt", "File 3.txt");
7
Several useful attributes/methods: numFiles attribute the number of files contained in the active archive file filename attribute the complete path of the active archive file statIndex() method Get the details of an entry defined by its index statName() method Get the details of an entry defined by its name setPassword() method Set a password for opening an archive
8
Example: $zip = new ZipArchive(); $filename = "abc.zip"; if ($zip->open($filename) != TRUE) { exit("cannot open \n"); } echo "numFiles: ". $zip->numFiles. " "; echo "filename: ". $zip->filename. " "; for ($i=0; $i numFiles;$i++) { $current = $zip->statIndex($i); echo "index $i : "; printf("%s (size: %d byte(s)) ", $current["name"], $current["size"]); } when open() is used without the second parameter, it tries to open an existing zip file, returning FALSE when the file doesn’t exist
9
Example:
10
The extractTo() method extracts the complete archive or the given files to the specified destination. Syntax: bool extractTo(string $destination [, mixed $entries]) single entry name or array of names
11
Example: extracts the complete archive extracts one file extracts an array of files $destination = "extracted/abc"; $zip->extractTo($destination); $destination = "extracted/abc"; $zip->extractTo($destination, "Document1.txt"); $destination = "extracted/abc"; $zip->extractTo($destination, array("Document1.txt", "Document2.txt"));
12
The addEmptyDir() method adds an empty directory in the archive Example: $zip->addEmptyDir("newDir1"); $zip->addFile("Document1.txt", "newDir1/Document1.txt"); $zip->addEmptyDir("newDir2"); $zip->addFile("Document2.txt", "newDir2/Document2.txt"); Unix / Windows Compatibility When specifying a path on Unix platforms, a forward slash (/) is used as directory separator. On Windows platforms, both forward slash (/) and backslash (\) can be used. Unix / Windows Compatibility When specifying a path on Unix platforms, a forward slash (/) is used as directory separator. On Windows platforms, both forward slash (/) and backslash (\) can be used.
13
The Directory class allow us to retrieve information about directories and their contents. Instances of Directory are created by calling the dir() function, not by the new operator. The getcwd() function returns a string containing the current working directory.
14
The read() method returns the name of the next entry in the directory. The entries are returned in the order in which they are stored by the file system. The close() method closes the previously opened directory handle
15
Example: $d = dir(getcwd()); echo "Path: ". $d->path. " "; while (($file = $d->read()) !== false){ echo "filename: ". $file. " "; } $d->close();
16
Example:
17
Example: processing.txt files only $d = dir(getcwd()); echo "Path: ". $d->path. " "; while (($file = $d->read()) !== false){ if(pathinfo($file, PATHINFO_EXTENSION) == "txt") echo $file. " "; } $d->close();
18
The is_dir() function tells whether the given filename is a directory Example: processing directory only $d = dir(getcwd()); echo "Path: ". $d->path. " "; while (($file = $d->read()) !== false){ if(is_dir($file)) echo $file. " "; } $d->close();
19
The addFile() method adds a single file to a zip archive. To zip an entire directory, we need to recursively iterate all the directories and adds all the files one by one. One implementation by Ramui can be found here: http://ramui.com./articles/php-zip-files-and- directory.html http://ramui.com./articles/php-zip-files-and- directory.html
21
Class definitions contain the class name (which is case-sensitive), its attributes, and its methods. Example: class User{ public $name; public $password; function User($param1, $param2){ $this->name=$param1; $this->password=$param2; } function save_user(){ echo "Save User code goes here"; }
22
To create an object with a specified class, use the new keyword Example: $object = new User; $temp = new User('name', 'password');
23
To access object’s attribute: $object->attribute To access object’s method: $object->method() The property and method names do not have dollar signs ( $ ) in front of them.
24
Once you have created an object, it is passed by reference when you pass it as a parameter. In other words, making object assignments does not copy objects in their entirety. Example: this prints “Wombaty” $u1 = new User("Wombat", "womwom"); $u2 = $u1; $u1->name = "Wombaty"; echo $u2->name;
25
The clone operator creates a new instance of the class and copies the attribute values from the original instance to the new instance. Example: This prints “Wombat” $u1 = new User("Wombat", "womwom"); $u2 = clone $u1; $u1->name = "Wombaty"; echo $u2->name;
26
Starting from PHP5, a method can be declared as static, which means that it is called on a class and not on an object To call a static method, write the class’ name, followed by a double colon (::), followed by the method’s name
27
Example: class User{ public $name; public $password; function User($param1, $param2){ $this->name=$param1; $this->password=$param2; } static function pwd_string(){ echo "Please enter your password"; } User::pwd_string();
28
An attribute can be given a default value It must be a constant and not the result of a function or expression Example: class User{ public $name = "John Smith"; //valid public $password = time();//not valid }
29
A constant is declared using const keyword. Its value, once declared, cannot be changed. A constant’s name is usually written in all UPPERCASE letter. Constants can be referenced directly, using the self keyword and the double colon operator (::)
30
Example: Translate::lookup(); class Translate{ const ENGLISH = 0; const SPANISH = 1; const FRENCH = 2; const GERMAN = 3; //... static function lookup(){ echo self::SPANISH; }
31
PHP 5 provides three keywords for controlling the scope of attributes and methods: public, protected, private. The default scope is public
32
public can be referenced from anywhere protected can be referenced only by the object’s class methods and those of any subclasses. private can be referenced only by methods within the same class—not by subclasses.
33
Once we have written a class, we can derive subclasses from it. This is achieved using the extends operator. class Subscriber extends User{ public $phone, $email; function display(){ echo "Name: ". $this->name. " "; echo "Pass: ". $this->password. " "; echo "Phone: ". $this->phone. " "; echo "Email: ". $this->email; }
34
The parent keyword is used to access the superclass’ attribute/methods Example: accessing parent’s constructor class Subscriber extends User{ public $phone, $email; function Subscriber($p1, $p2, $p3, $p4){ parent::User($p1, $p2); $this->phone = $p3; $this->email = $p4; } When extending a class, PHP does not automatically calls the parent class’ constructor
35
If you write a method in a subclass with the same name as one in its parent class, its statements will override those of the parent class. However, the parent class’ method is still accessible by using parent keyword.
36
In cases in which we wish to prevent a subclass from overriding a superclass’ method, we can use the final keyword. Example: class User{ final function copyright(){ echo "This class was written by Joe Smith"; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.