13.09.2016 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Interacting with the File System<br />

443<br />

the directory browser, particularly if you begin to build up a complex directory structure<br />

of content based on meaningful directory names <strong>and</strong> filenames.<br />

You could also add to your directory listing an indication of how much space is left<br />

for uploads by using the disk_free_space($path) function. If you pass this function a<br />

path to a directory, it will return the number of bytes free on the disk (Windows) or the<br />

file system (Unix) on which the directory is located.<br />

Creating <strong>and</strong> Deleting Directories<br />

In addition to passively reading information about directories, you can use the <strong>PHP</strong><br />

functions mkdir() <strong>and</strong> rmdir() to create <strong>and</strong> delete directories.You can create or delete<br />

directories only in paths that the user the script runs as has access to.<br />

Using mkdir() is more complicated than you might think. It takes two parameters:<br />

the path to the desired directory (including the new directory name) <strong>and</strong> the permissions<br />

you would like that directory to have. Here’s an example:<br />

mkdir(“/tmp/testing”, 0777);<br />

However, the permissions you list are not necessarily the permissions you are going to<br />

get.The inverse of the current umask will be combined with this value using AND to get<br />

the actual permissions. For example, if the umask is 022, you will get permissions of<br />

0755.<br />

You might like to reset the umask before creating a directory to counter this effect,<br />

by entering<br />

$oldumask = umask(0);<br />

mkdir(“/tmp/testing”, 0777);<br />

umask($oldumask);<br />

This code uses the umask() function, which can be used to check <strong>and</strong> change the current<br />

umask. It changes the current umask to whatever it is passed <strong>and</strong> returns the old<br />

umask, or, if called without parameters, it just returns the current umask.<br />

Note that the umask() function has no effect on Windows systems.<br />

The rmdir() function deletes a directory, as follows:<br />

rmdir(“/tmp/testing”);<br />

or<br />

rmdir(“c:\\tmp\\testing”);<br />

The directory you are trying to delete must be empty.<br />

Interacting with the File System<br />

In addition to viewing <strong>and</strong> getting information about directories, you can interact with<br />

<strong>and</strong> get information about files on the web server.You previously looked at writing to<br />

<strong>and</strong> reading from files. A large number of other file functions are available.

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

Saved successfully!

Ooh no, something went wrong!