PHP and MySQL Web Development - P76 pdf

5 217 0
PHP and MySQL Web Development - P76 pdf

Đang tải... (xem toàn văn)

Thông tin tài liệu

347 Using Directory Functions An associated and sometimes useful function is rewinddir($dir),which resets the reading of filenames to the beginning of the directory. As an alternative to these functions, you can use the dir class provided by PHP.This has the properties handle and path, and the methods read(), close(), and rewind(), which perform identically to the non-class alternatives. Getting Info About the Current Directory We can obtain some additional information given a path to a file. The dirname($path) and basename($path) functions return the directory part of the path and the filename part of the path, respectively.This could be useful for our directory browser, particularly if we began to build up a complex directory structure of content based on meaningful directory names and filenames. We could also add to our directory listing an indication of how much space is left for uploads by using the disk_free_space($path) function. If you pass this function a path to a directory, it will return the number of bytes free on the disk (Windows) or the file system (UNIX) that the directory is on. Creating and Deleting Directories In addition to passively reading information about directories, you can use the PHP functions mkdir() and rmdir() to create and delete directories.You will only be able to create or delete directories in paths that the user the script runs as has access to. Using mkdir() is more complicated than you might think. It takes two parameters, the path to the desired directory (including the new directory name), and the permis- sions you would like that directory to have, for example, mkdir("/tmp/testing", 0777); However, the permissions you list are not necessarily the permissions you are going to get.The current umask will be ANDed (like subtraction) with this value to get the actu- al permissions. For example, if the umask is 022, you will get permissions of 0755. You might like to reset the umask before creating a directory to counter this effect, by entering $oldumask = umask(0); mkdir("/tmp/testing", 0777); umask($oldumask); This code uses the umask() function, which can be used to check and change the cur- rent umask. It will change the current umask to whatever it is passed and return the old umask, or if called without parameters, it will just return the current umask. Note that the umask() function has no effect on Windows systems. The rmdir() function deletes a directory, as follows: rmdir("/tmp/testing"); 21 525x ch16 1/24/03 3:41 PM Page 347 348 Chapter 16 Interacting with the File System and the Server or rmdir("c:\\tmp\\testing"); The directory you are trying to delete must be empty. Interacting with the File System In addition to viewing and getting information about directories, we can interact with and get information about files on the Web server.We’ve previously looked at writing to and reading from files. A large number of other file functions are available. Get File Info We can alter the part of our directory browsing script that reads files as follows: while ($file = $dir->read()) { echo '<a href="filedetails.php?file='.$file.'">'.$file.'</a><br />'; } We can then create the script filedetails.php to provide further information about a file.The contents of this file are shown in Listing 16.4. One warning about this script: Some of the functions used here are not supported under Windows, including posix_getpwuid(), fileowner(),and filegroup(),orare not supported reliably. Listing 16.4 filedetails.php—File Status Functions and Their Results <html> <head> <title>File Details</title> </head> <body> <?php $current_dir = '/uploads/'; $file = basename($file); // strip off directory information for security echo '<h1>Details of file: '.$file.'</h1>'; $file = $current_dir.$file; echo '<h2>File data</h2>'; echo 'File last accessed: '.date('j F Y H:i', fileatime($file)).'<br />'; echo 'File last modified: '.date('j F Y H:i', filemtime($file)).'<br />'; $user = posix_getpwuid(fileowner($file)); echo 'File owner: '.$user['name'].'<br />'; 21 525x ch16 1/24/03 3:41 PM Page 348 349 Interacting with the File System $group = posix_getgrgid(filegroup($file)); echo 'File group: '.$group['name'].'<br />'; echo 'File permissions: '.decoct(fileperms($file)).'<br />'; echo 'File type: '.filetype($file).'<br />'; echo 'File size: '.filesize($file).' bytes<br />'; echo '<h2>File tests</h2>'; echo 'is_dir: '.(is_dir($file)? 'true' : 'false').'<br />'; echo 'is_executable: '.(is_executable($file)? 'true' : 'false').'<br />'; echo 'is_file: '.(is_file($file)? 'true' : 'false').'<br />'; echo 'is_link: '.(is_link($file)? 'true' : 'false').'<br />'; echo 'is_readable: '.(is_readable($file)? 'true' : 'false').'<br />'; echo 'is_writable: '.(is_writable($file)? 'true' : 'false').'<br />'; ?> </body> </html> The results of one sample run of Listing 16.4 are shown in Figure 16.4. Listing 16.4 Continued Figure 16.4 The File Details view shows file system information about a file. Note that permissions are shown in an octal format. 21 525x ch16 1/24/03 3:41 PM Page 349 350 Chapter 16 Interacting with the File System and the Server Let’s talk about what each of the functions used in Listing 16.4 does. As mentioned previously, the basename() function gets the name of the file without the directory. (You can also use the dirname() function to get the directory name with- out the filename.) The fileatime() and filemtime() functions return the time stamp of the time the file was last accessed and last modified, respectively.We’ve reformatted the time stamp using the date() function to make it more human-readable.These functions will return the same value on some operating systems (as in the example) depending on what infor- mation the system stores. The fileowner() and filegroup() functions return the user ID (uid) and group ID (gid) of the file.These can be converted to names using the functions posix_getp- wuid() and posix_getgrgid(),respectively, which makes them a bit easier to read. These functions take the uid or gid as a parameter and return an associative array of information about the user or group, including the name of the user or group, as we have used in this script. The fileperms() function returns the permissions on the file.We have reformatted them as an octal number using the decoct() function to put them into a format more familiar to UNIX users. The filetype() function returns some information about the type of file being examined.The possible results are fifo, char, dir, block, link, file, and unknown. The filesize() function returns the size of the file in bytes. The second set of functions—is_dir(), is_executable(), is_file(), is_link(), is_readable(), and is_writable()—all test the named attribute of a file and return true or false. We could alternatively have used the function stat() to gather a lot of the same information.When passed a file, this returns an array containing similar data to these functions.The lstat() function is similar, but for use with symbolic links. All the file status functions are quite expensive to run in terms of time.Their results are therefore cached. If you want to check some file information before and after a change, you need to call clearstatcache(); in order to clear the previous results. If you wanted to use the previous script before and after changing some of the file data, you should begin by calling this function to make sure the data produced is up-to-date. Changing File Properties In addition to viewing file properties, we can alter them. Each of the chgrp(file, group), chmod(file, permissions), and chown(file, user) functions behaves similarly to its UNIX equivalent. None of these will work in Windows-based systems, although chown() will execute and always return true. 21 525x ch16 1/24/03 3:41 PM Page 350 351 Interacting with the File System The chgrp() function is used to change the group of a file. It can only be used to change the group to groups of which the user is a member unless the user is root. The chmod() function is used to change the permissions on a file.The permissions you pass to it are in the usual UNIX chmod form—you should prefix them with a "0" to show that they are in octal, for example, chmod('somefile.txt', 0777); The chown() function is used to change the owner of a file. It can only be used if the script is running as root, which should never happen. Creating, Deleting, and Moving Files You can use the file system functions to create, move, and delete files. First, and most simply, you can create a file, or change the time it was last modified, using the touch() function.This works similarly to the UNIX command touch.The function has the following prototype: int touch (string file, [int time [, int atime]]) If the file already exists, its modification time will be changed either to the current time, or the time given in the second parameter if it is specified. If you want to specify this, it should be given in time stamp format. If the file doesn’t exist, it will be created.The access time of the file will also change: by default to the current system time or alterna- tively to the time stamp you specify in the optional atime parameter. You can delete files using the unlink() function. (Note that this function is not called delete—there is no delete.) You use it like this: unlink($filename); This is one of the functions that doesn’t work with some older Windows versions. However, if it doesn’t work on your setup, you can delete a file in Windows with system("del filename.ext"); You can copy and move files with the copy() and rename() functions, as follows: copy($source_path, $destination_path); rename($oldfile, $newfile); You might have noticed that we used copy() in Listing 16.2. The rename() function does double duty as a function to move files from place to place because PHP doesn’t have a move function.Whether you can move files from file system to file system, and whether files are overwritten when rename() is used is operat- ing system dependent, so check the effects on your server.Also, be careful about the path you use to the filename. If relative, this will be relative to the location of the script, not the original file. 21 525x ch16 1/24/03 3:41 PM Page 351 . can use the dir class provided by PHP. This has the properties handle and path, and the methods read(), close(), and rewind(), which perform identically to the non-class alternatives. Getting Info. directory is on. Creating and Deleting Directories In addition to passively reading information about directories, you can use the PHP functions mkdir() and rmdir() to create and delete directories.You. uses the umask() function, which can be used to check and change the cur- rent umask. It will change the current umask to whatever it is passed and return the old umask, or if called without parameters,

Ngày đăng: 07/07/2014, 03:20

Từ khóa liên quan

Mục lục

  • PHP and MySQL Web Development

  • Copyright

  • Table of Contents

  • Introduction

  • Part I: Using PHP

    • Chapter 1: PHP Crash Course

    • Chapter 2: Storing and Retrieving Data

    • Chapter 3: Using Arrays

    • Chapter 4: String Manipulation and Regular Expressions

    • Chapter 5: Reusing Code and Writing Functions

    • Chapter 6: Object-Oriented PHP

    • Part II: Using MySQL

      • Chapter 7: Designing Your Web Database

      • Chapter 8: Creating Your Web Database

      • Chapter 9: Working with Your MySQL Database

      • Chapter 10: Accessing Your MySQL Database from the Web with PHP

      • Chapter 11: Advanced MySQL

      • Part III: E-commerce and Security

        • Chapter 12: Running an E-commerce Site

        • Chapter 13: E-commerce Security Issues

        • Chapter 14: Implementing Authentication with PHP and MySQL

        • Chapter 15: Implementing Secure Transactions with PHP and MySQL

        • Part IV: Advanced PHP Techniques

          • Chapter 16: Interacting with the File System and the Server

Tài liệu cùng người dùng

Tài liệu liên quan