13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

446 Chapter 19 Interacting with the File System <strong>and</strong> the Server<br />

The fileowner() <strong>and</strong> filegroup() functions return the user ID (uid) <strong>and</strong> group ID<br />

(gid) of the file.These IDs can be converted to names using the functions posix_<br />

getpwuid() <strong>and</strong> posix_getgrgid(), respectively, which makes them a bit easier to read.<br />

These functions take the uid or gid as a parameter <strong>and</strong> return an associative array of<br />

information about the user or group, including the name of the user or group, as we<br />

have used in this script.<br />

The fileperms() function returns the permissions on the file.We reformatted them<br />

as an octal number using the decoct() function to put them into a format more familiar<br />

to Unix users.<br />

The filetype() function returns some information about the type of file being<br />

examined.The possible results are fifo, char, dir, block, link, file, <strong>and</strong> unknown.<br />

The filesize() function returns the size of the file in bytes.<br />

The second set of functions—is_dir(), is_executable(), is_file(), is_link(),<br />

is_readable(), <strong>and</strong> is_writable()—all test the named attribute of a file <strong>and</strong> return<br />

true or false.<br />

Alternatively, you could use the function stat() to gather a lot of the same information.When<br />

passed a file, this function returns an array containing similar data to these<br />

functions.The lstat() function is similar, but for use with symbolic links.<br />

All the file status functions are quite expensive to run in terms of time.Their results<br />

are therefore cached. If you want to check some file information before <strong>and</strong> after a<br />

change, you need to call<br />

clearstatcache();<br />

to clear the previous results. If you want to use the previous script before <strong>and</strong> after<br />

changing some of the file data, you should begin by calling this function to make sure<br />

the data produced is up to date.<br />

Changing File Properties<br />

In addition to viewing file properties, you can alter them.<br />

Each of the chgrp(file, group), chmod(file, permissions), <strong>and</strong> chown(file,<br />

user) functions behaves similarly to its Unix equivalent. None of these functions will<br />

work in Windows-based systems, although chown() will execute <strong>and</strong> always return true.<br />

The chgrp() function changes the group of a file. It can be used to change the group<br />

only to groups of which the user is a member unless the user is root.<br />

The chmod() function changes the permissions on a file.The permissions you pass to<br />

it are in the usual Unix chmod form.You should prefix them with a 0 (a zero) to show<br />

that they are in octal, as in this example:<br />

chmod(‘somefile.txt’, 0777);<br />

The chown() function changes the owner of a file. It can be used only if the script is<br />

running as root, which should never happen, unless you are specifically running the<br />

script from the comm<strong>and</strong> line to perform an administrative task.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!