tailieunhanh - Giải pháp thiết kế web động với PHP - p 23
USING PHP TO MANAGE FILES Figure 7-1. PHP makes light work of creating a drop-down menu of images in a specific folder. When incorporated into an online form, the filename of the selected image appears in the $_POST array identified by the name attribute of the element—in this case, $_POST['pix']. That s all there is to it! You can compare your code with in the ch07 folder. PHP Solution 7-4: Creating a generic file selector The previous PHP solution relies on an understanding of regular expressions. Adapting it to work with other filename extensions isn t difficult, but you need to. | USING PHP TO MANAGE FILES Figure 7-1. PHP makes light work of creating a drop-down menu of images in a specific folder. When incorporated into an online form the filename of the selected image appears in the _POST array identified by the name attribute of the select element in this case _POST pix . That s all there is to it You can compare your code with in the ch07 folder. PHP Solution 7-4 Creating a generic file selector The previous PHP solution relies on an understanding of regular expressions. Adapting it to work with other filename extensions isn t difficult but you need to be careful that you don t accidentally delete a vital character. Unless regexes or Vogon poetry are your specialty it s probably easier to wrap the code in a function that can be used to inspect a specific folder and create an array of filenames of specific types. For example you might want to create an array of PDF document filenames or one that contains both PDFs and Word documents. Here s how you do it. 1. Create a new file called in the filesystem folder. The file will contain only PHP code so delete any HTML inserted by your editing program. 201 CHAPTER 7 Download from Wow eBook 2. Add the following code to the file function buildFileList dir extensions if is_dir dir is_readable dir return false else if is_array extensions extensions implode extensions This defines a function called buildFileList which takes two arguments dir The path to the folder from which you want to get the list of filenames. extensions This can be either a string containing a single filename extension or an array of filename extensions. To keep the code simple the filename extensions should not include a leading period. The function begins by checking whether dir is a folder and is readable. If it isn t the function returns false and no more code is executed. If dir is OK the else block is executed. It also begins with a conditional statement that checks whether .
đang nạp các trang xem trước