tailieunhanh - Giải pháp thiết kế web động với PHP - p 19

UPLOADING FILES Figure 6-5. The class now reports errors with invalid size and MIME types. Changing protected properties The $_permitted property restricts uploads to images, but you might want to allow different types. Instead of diving into the class definition file every time you have different requirements, you can create public methods that allow you to make changes to protected properties on the fly. You can find definitions of recognized MIME types at . Table 6-3 lists some of the most commonly used ones. Table 6-3. Commonly used MIME types Category Documents MIME type application/pdf text/plain text/rtf Images image/gif image/jpeg image/pjpeg image/png image/tiff. | UPLOADING FILES Figure 6-5. The class now reports errors with invalid size and MIME types. Changing protected properties The _permitted property restricts uploads to images but you might want to allow different types. Instead of diving into the class definition file every time you have different requirements you can create public methods that allow you to make changes to protected properties on the fly. You can find definitions of recognized MIME types at assignments media-types. Table 6-3 lists some of the most commonly used ones. Table 6-3. Commonly used MIME types Category MIME type Description Documents application pdf PDF document text plain Plain text text rtf Rich text format Images image gif GIF format image jpeg JPEG format includes .jpg files image pjpeg JPEG format nonstandard used by Internet Explorer image png PNG format image tiff TIFF format An easy way to find other MIME types not listed in Table 6-3 is to use and see what value is displayed for _FILES image type . PHP Solution 6-4 Allowing different types and sizes to be uploaded This PHP solution shows you how to add one or more MIME types to the existing _permitted array and how to reset the array completely. To keep the code relatively simple the class checks the validity of only 161 CHAPTER 6 Download from Wow eBook a few MIME types. Once you understand the principle you can expand the code to suit your own requirements. You ll also add a public method to change the maximum permitted size. Continue working with from the previous PHP solution. Alternatively use in the ch06 folder. 1. The Ps2_Upload class already defines four permitted MIME types for images but there might be occasions when you want to permit other types of documents to be uploaded as well. Rather than listing all permitted types again it s easier to add the extra ones. Add the following method definition to the class file public function addPermittedTypes

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.