In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to create the phar of PHP". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to create the phar of PHP" can help you solve the problem.
What is a phar file?
The Jar (Java Archive) file, an application, including all executable and accessible files, is packaged into a JAR file, making the deployment process very simple.
Similar to JAR. The full name of phar for the PHP Archive,phar extension provides a way to put the entire PHP application into a .phar file to facilitate movement and installation. The most important feature of phar files is the convenient way to combine several files into one file. The .phar file provides a way to distribute and run the complete PHP program in a file.
Unlike JAR, Phar can be handled by PHP itself, so you don't need to use additional tools to create or use it, you can create or extract it using php scripts.
There are three formats of phar files: tar archiving, zip archiving and phar archiving. The first two kinds of execution require php installation Phar extension support and use less. Here we mainly talk about the phar archiving format.
2. Creation of phar 1 modify php.ini configuration file
The default state of the PHAR file is read-only and no configuration is required to use the Phar file. Deployment is very convenient. Since we now need to create our own Phar file, we need to allow writing to the Phar file, which requires a modification of the php.ini
In my php.ini file, phar.readonly = On.
[Phar]; http://php.net/phar.readonly;phar.readonly = On
First of all, modify the option phar.readonly in php.ini, remove the previous semicolon, and change the value to off. For security reasons, this option defaults to on. If it is disabled in php.ini (the value is 0 or off), it can be turned on or off in the user script. If it is enabled in php.ini, then the user script cannot be closed, so set it to off to show the example.
Now we can package the PHP application into a Phar file.
2 create our own PHP file project
Here I Copy directly with the help of other people's blog projects, and did not demonstrate, because my original intention of organizing this blog is to ctf to understand phar:// vulnerabilities. So the name of the file is not modified according to the original author. Finally, a reference article will be added.
First of all, I want to create the directory structure of the application according to a rule. The root directory is the directory under project,project, as shown below:
File-yunek.js-yunke.csslib-lib_a.phptemplate-msg.htmlindex.phpLib.php
The file folder contains two empty js and css files, which only demonstrates that phar can contain multiple file formats
The lib_a.php content is as follows:
The index.php content is as follows:
The phar:// that appears here is a way to access phar files, so don't worry too much about it.
Add the contents of a file as a string
Call the class method Phar::addFromString ($localpath,$contents) to add a file as a string
Add empty directory
Call the class method Phar::addEmptyDir ($dirname) to add an empty directory, and use the method Phar::getContent () to get the file structure
Manually select to add an existing directory
Call the class method Phar::buildFromDirectory ($dir,$pattern = "") to add the entire directory
4) it is important to understand the stub file Stub.
In the archive file, there is a stub file stub, which is actually a piece of php execution code, which can be set when making the archive. When the archive file is executed directly, it is actually executed, so it is the startup file. When an archive file is included in a script, it is included and run just like an ordinary php file, but the stub code is not executed when a file in the archive is included directly in phar://. Require often contains other files to be run in the stub file, and the restriction on the stub file is only _ _ HALT_COMPILER (). Finally, the default stub design is to run when there is no phar extension, which extracts the contents of the phar file to a temporary directory before execution, but the extension is enabled by default since php5.3.
Stub is the header of the phar file in the format of... It can be any character, including spaces, and there cannot be more than one space between the php closure and the last semicolon. In addition, php closures can also be omitted. The stub with the shortest omission of closures is
When you run the Phar file, the stub file is treated as a meta file to initialize the Phar and tells the Phar file what to do when called.
In our example, the createDefaultStub () method is used.
Other ways are as follows:
Method 1: call the class method Phar::setStub ($string) to create a custom stub for the instance
Also OK
$phar- > setStub ($phar- > createDefaultStub ("index.php"))
The generated default stub file contains the following code:
If you create a stub,PHP by default, the default stub is used.
4. The operation of phar files
Let's create an index.php file in the server root directory to demonstrate how to use the phar file created above, as follows:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.