A PHP file upload class sharing_php example
This article mainly introduces a classic PHP file upload class sharing. This article explains in detail and comprehensively the requirements analysis and function implementation related to PHP file upload, and shares it at the same time. PHP code, friends who need it can refer to this article
File upload is a common function in project development, but the file upload process is more cumbersome, as long as there are files uploaded Wherever you need to write these complex codes. In order to reduce the difficulty of writing functions in each development and to save development time, we usually encapsulate these repeatedly used pieces of code into a class. Help developers in future development to implement complex file upload functions by writing a few simple codes. For readers with weak foundation, they only need to be able to use this class. For those who like challenges, they can try to understand it and develop a file upload class of their own.
1. Requirements Analysis
It is necessary to customize the file upload class, which can complete the following functions under the premise that it is very easy to use:
①Support single file upload.
②Support multiple file uploads.
③You can specify the storage location of the uploaded file yourself, you can set the size and type allowed for the uploaded file, you can rename the uploaded file by the system, and you can also set the original name of the uploaded file to be retained.
(Note: It is required that single file upload and multiple file upload must use the same operation method, and some settings for upload must also use the same method).
2. Programming
According to the requirements of the program, we can declare 4 visible member attributes for the file upload class, so that users can You can also set some behaviors. The required member attributes are as shown in the following table:
In order to prevent the attribute values from being assigned some illegal values, these member attributes need to be encapsulated and cannot be accessed outside the object. Then assign values to the above four member attributes through the set() method declared in the class. The set() method has two parameters. The first parameter is the name of the member attribute (not case-sensitive), and the second parameter is the value corresponding to the attribute in the previous parameter. After the set() method is called, the object ($this) is returned. Therefore, in addition to assigning values to each attribute individually, you can also perform consecutive operations to assign values to multiple attributes together. In this example, in addition to the set() method, the most important thing is to implement the function of uploading files, so the system mainly provides the following public methods to implement file upload operations, as shown in the following table:
In order to prevent the attribute values from being assigned some illegal values, these member attributes need to be encapsulated and cannot be accessed outside the object, and then the above four member attributes can be assigned values through the set() method declared in the class. The set() method has two parameters. The first parameter is the name of the member attribute (not case-sensitive), and the second parameter is the value corresponding to the attribute in the previous parameter. After the set() method is called, the object ($this) is returned. Therefore, in addition to assigning values to each attribute individually, you can also perform consecutive operations to assign values to multiple attributes together. In this example, in addition to the set() method, the most important thing is to implement the function of uploading files, so the system mainly provides the following public methods to implement file upload operations, as shown in the following table:
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
|
4. Application process of file upload class
The file upload class FileUpload in this example supports single file upload and multiple files at the same time. There is no difference in the processing method when uploading to the server. However, when writing an upload bid, multiple file uploads must be passed to the server in an array. The single file upload form is as follows:
1 2 3 4 5 6 7 8 9 10 11 |
|
The above forms all point the submission location to the same file upload.php, so it is not difficult to see that the single and Multiple file uploads are processed in the same way. The upload.php code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
1 2 3 4 |
|
In the upload.php file, it must first be loaded File upload FileUpload class is located in the file fileupload.class.php. Then instantiate the object of the file upload class, and then upload the file by calling the upload() method. If the upload is successful, you can get the uploaded file name through the getFileName() method. If the upload fails, you can also get the error through the getErrorMsg() method. Report. If you need to change some upload behaviors, you can complete the setting of some attributes by calling the set() method. The set() method can be used alone to set the value of an attribute. If you need to change the values of multiple attributes, you can call the set() method continuously to set it, or you can set multiple attributes at the same time in a continuous operation.
related suggestion:
PHP encapsulated multi-file upload class examples and usage details
php Sample code sharing for file upload
The above is the detailed content of A PHP file upload class sharing_php example. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.
