PHP and Directories

I l @ ve RuBoard

PHP also gives you the power to work with your file directories.

Opening and Closing a Directory

You can open a directory using the opendir function:

 <?php  //directory to read  $directoryinfo = "C:\temp\";  //open directory for reading  $dirtoread =  opendir  ($directoryinfo);  ?> 

As with files, PHP uses a pointer to indicate its position in a directory. To do this, PHP must first open a directory. In order to preserve system resources, you must ensure that your directory is closed when your script is finished. As with files, PHP automatically closes a directory at the end of the script. You can also set this in your code:

 <?php  //directory to read  $directoryinfo = "C:\temp\";  //open directory for reading  $dirtoread = opendir($directoryinfo);  //close directory  closedir  ($dirtoread);  ?> 

Listing Directory Contents

You can list a directory's contents by looping through them and printing what you find, as shown in Listing 4.11.

Listing 4.11 dircontents.php
 <?php  //directory to read  $directoryinfo = "C:\temp\";  //open directory for reading  $dirtoread = opendir($directoryinfo);  //loop through directory contents  while (false !== ($info = readdir($dirtoread))) {       //print directory contents        print("$info<br>");  }  //close directory  closedir($dirtoread);  ?> 

PHP lets you read entries from the directory opened with opendir using the readdir function:

 while (false !== ($info = readdir($dirtoread))) { 

This function reads a directory line by line. You can then store each line it returns in a variable for further processing (in this case, displaying).

If you run the script, you should see the contents of the C:\temp\ directory printed to the browser, as shown in Figure 4.9.

Figure 4.9. The contents of the C:\temp\ directory.

graphics/04fig09.gif

Creating and Deleting Directories

You can easily add and delete directories in PHP, as shown in Listing 4.12. Adding and deleting use a single PHP function for each action.

Listing 4.12 deletingdirectories.php
 <?php  //directory to read  $directoryinfo = "C:\temp\";  //directory to create  $directorytomake = "C:\temp\temp2\";  //create directory  mkdir($directorytomake, 0700);  //open directory for reading  $dirtoread = opendir($directoryinfo);  //loop through directory contents  while ($info = readdir($dirtoread)) {       //print directory contents        print("$info<br>");  }  //close directory  closedir($dirtoread);   //remove directory  rmdir($directorytomake);  ?> 

This script first sets the directory you will list and the directory you will create:

 //directory to read  $directoryinfo = "C:\temp\";  //directory to create  $directorytomake = "C:\temp\temp2\"; 

You then create the directory using the mkdir function (the same command that DOS uses):

 //create directory  mkdir  ($directorytomake, 0700); 

Note that this function does not apply permissions to Windows directories. If you run the script, it creates your directory and then lists the directory contents (using the same method as the previous script):

 //loop through directory contents  while ($info = readdir($dirtoread)) {       //print directory contents        print("$info<br>");  } 

You then close the directory you are searching and delete the directory you created. To delete a directory, you use the rmdir function (also the same command that DOS uses):

 //remove directory  rmdir  ($directorytomake); 

If you run this script, you should see the directory listed in the browser, as shown in Figure 4.10.

Figure 4.10. The contents of the C:\temp\ directory before the temp2 directory is deleted.

graphics/04fig10.gif

If you check your file system, you will see that the directory has been deleted, as shown in Figure 4.11.

Figure 4.11. The C:\temp\ directory with the temp2 directory deleted.

graphics/04fig11.gif

File Work and Directories

PHP lets you work easily with the files in your directories, allowing you to easily copy, move, rename, and delete files.

Moving and copying might sound like similar operations, but moving moves a file to another directory, and copying creates a copy of the file.

To copy a file, use the code shown in Listing 4.13.

Listing 4.13 copyingfiles.php
 <?php  //directory to work from  $sourcedirectory = "C:\temp\";  $filetocopy = "tmp12.tmp";  $copyoffile = "tmp12_COPY.tmp";   $fullname_filetocopy = $sourcedirectory . $filetocopy;  $fullname_copyfile = $sourcedirectory . $copyoffile;  copy($fullname_filetocopy, $fullname_copyfile);  ?> 

This script sets the directory where your files are located:

 //directory to work from  $sourcedirectory = "C:\temp\"; 

You then set the filename you want to copy and the name of the copy of that file:

 $filetocopy = "tmp12.tmp";  $copyoffile = "tmp12_COPY.tmp"; 

Here you set up in your script variables to hold the name of the file to be copied (such as C:\temp\tmp12.tmp) and the name of the file that you will copy to (C:\temp\tmp12_COPY.tmp).

 $fullname_filetocopy = $sourcedirectory . $filetocopy;  $fullname_copyfile = $sourcedirectory . $copyoffile; 

You then copy the file:

  copy  ($fullname_filetocopy, $fullname_copyfile); 

You can also copy a file to a new location using the code shown in Listing 4.14.

Listing 4.14 copyfiles_newdir.php
 <?php  //directory to copy from  $sourcediectory = "C:\temp\";  $copydirectory = "C:\temp\copydir\";  $filetocopy = "tmp12.tmp";  $fullname_filetocopy = $sourcediectory . $filetocopy;  $fullname_copyfile = $copydirectory . $filetocopy;  copy($fullname_filetocopy, $fullname_copyfile);  ?> 

Here you set the directory that contains the file you want to copy:

 //directory to copy from  $sourcediectory = "C:\temp\"; 

The directory that you want to copy the file to is set in the next line:

 $copydirectory = "C:\temp\copydir\"; 

The file you want to copy comes next:

 $filetocopy = "tmp12.tmp"; 

Here you set up in your script variables to hold the name of the file to be copied (such as C:\temp\tmp12.tmp) and the name of the file you will copy to (C:\temp\tmp12_COPY.tmp).

Note that you are using the copy command again, but instead of setting a new filename to copy the file, you set a new directory name to copy the file.

If you run the script, you see that the file has been copied to another directory, as shown in Figure 4.12.

Figure 4.12. The file copied to the copydir directory.

graphics/04fig12.gif

You can also rename files using the code shown in Listing 4.15.

Listing 4.15 renamingfiles.php
 <?php  //directory we are working from  $workdir = "C:\temp\";  //create temp file  $filetorenam = tempnam ($workdir, "tmp");   //newfile name  $newfilename = $workdir . "copyofile.tmp";  //print file message  print("File created: $filetorenam <BR>");  //rename file  rename($filetorenam, $newfilename) or die ("unable to rename file   or file has not been created");  //print file message  print ("file renamed: $newfilename");  ?> 

Here you set the directory you are working from:

 //directory we are working from  $workdir = "C:\temp\"; 

You then create a temporary file in that directory:

 //create temp file  $filetorenam = tempnam ($workdir, "tmp"); 

You then set the filename of the renamed file:

 //newfile name  $newfilename = $workdir . "copyofile.tmp"; 

You then rename the file using the rename function:

 rename($filetorenam, $newfilename) or die ("unable to rename file   or file has not been created"); 

Again, the rename function is very much like the rename command in DOS.

If you run this script, you see that the file has been renamed, as shown in Figure 4.13.

Figure 4.13. Renaming a file.

graphics/04fig13.gif

You use the rename function to move files:

 <?php  //directory we are working from  $workdir = "C:\temp\";  //directory to move to  $copydir = "C:\temp\spare\";  //create temp file  $filetorenam = tempnam ($workdir, "tmp");  //new filename and path  $newfilename = $copydir . "copyofile.tmp";  //print file message  print("File created: $filetorenam <BR>");  //rename file  rename($filetorenam, $newfilename) or die ("unable to rename file   or file has not been created");  //print file message  print ("file moved: $newfilename");  ?> 

This code does the same as the rename code in Listing 4.15. However, when you rename the file, you set its directory path to the directory you are moving the file to.

Last, you can delete files using PHP's unlink function, as shown in Listing 4.16.

Listing 4.16 deletingfiles.php
 <?php  //create temp file  $filetodel = tempnam ("C:\temp\", "tmp");  //print file message  print("File created: $filetodel");  //delete file  unlink($filetodel) or die ("unable to delete file   or file has not been created");  ?> 

Here you create a temp file:

 //create temp file  $filetodel = tempnam ("C:\temp\", "tmp"); 

You then delete the temp file:

 //delete file  unlink($filetodel) or die ("unable to delete file   or file has not been created"); 
I l @ ve RuBoard


PHP Programming for Windows
PHP Programming for Windows (Landmark (New Riders))
ISBN: 0735711690
EAN: 2147483647
Year: 2002
Pages: 99

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net