Page 1 of 1

Working with local files Rate Topic: -----

#1 PPDP   User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 42
  • Joined: 14-April 09

Posted 10 October 2010 - 08:10 AM

Working with local files
Copying, renaming, and deleting files
I will describe some things you can do with a file, such as copy it or delete it.

You can copy an existing file into a new file.After copying, you have two copies of the file with two different names. Copying a file is often useful for backing up important files. To copy a file, use the copy statement, as follows:

This statement copies fileold.txt, an existing file, into filenew. If a file with the same name as filenew.txt already exists, it is overwritten. If you don't want to overwrite an existing file, you can prevent it by using the following statements:
   echo "File already exists!\n";

You can rename a file by using the rename statement, as follows:

If you attempt to rename a file with the name of a file that already exists, a warning is displayed, as follows, and the file is not renamed.
Warning: rename(fileold.txt,filenew.txt) : File exists in c:test.php on line 5

To remove an unwanted file, use the unlink statement, as follows:

After this statement, the file is deleted. However, if the file doesn't exist to start with, unlink doesn't complain :P . It acts the same if it had deleted the file. PHP does not let you know if the file doesn't exist. So, watch out for typos.

Organizing files

Files are organized into directories, also called folders (for those of you who don't know). I will show you how to create and remove directories and how to get a list of the files in a directory.

Creating a directory

To create a directory, use the mkdir function, as follows:

This statement creates a new directory named testdir in the same directory where the script is located. That is, if the script is /test/test.php the new directory is /test/testdir. If a directory already exists with the same name, a warning is displayed, as follows, and the new directory is not created:
Warning: mkdir(): File exists in d:/test/test.php on line 5

You can check first to see whether the directory already exists by suing the following statements:
  echo "Directory already exists!";

After the directory is created, you can organize it's contents by copying files into and out of the directory.

To create a directory in another directory, use the entire path name, as follows:

You can use a relative path to create a new directory, as follows:

With this statement, if your script is /topdir/test/makedir.php, the new directory is /topdir/mynewdir.

To change to a different directory, use the following statement:


Building a list of all the files in a directory
It's often useful to get a list of the files in a directory. For example, you may want to provide a list of files for users to download or want to display images from files in a specific directory.

PHP provides functions for opening and reading directories. To open a directory, use the opendir statement, as follows:
$dh = opendir("/topdir/testdir");

If you attempt to open a directory that does not exist a warning is displayed.

In the previous statement the variable $dh is a directory handle, a pointer to open the directory that you can use later to read from the directory. To read a filename from the directory, use the readdir function as follows:
$filename = readdir($dh)

After this statement, $filename contains the name of a file. Only the filename is stored in $filename, not the entire path to the file. To read a filename from the directory, you can use a while loop, as follows:
while($filename = readdir($dh))
    echo $filename."\n";

The readdir function does not provide any control over the order in which filenames are read, so you don't always get the filenames in the order you expect.

Suppose you want to create a gallery that displayes in a Web page all the images in a specified directory. You can use the opendir and readdir functions to do this. Below I will show you a script that creates a image gallery.
/* Script name: displayGallery
 * Description: Displays all the image files that are
 *              stored in a specified directory.
echo "<html><head><title>Image Gallery</title></head>
$dir = "../test1/testdir/";
$dh = opendir($dir)
while($filename = readdir($dh))
  $filepath = $dir.$filename;
  if(is_file($filepath) and ereg("\.jpg$",$filename))
    $gallery[] = $filepath;
foreach($gallery as $image)
  echo"<img src='$image'><br>";

  • Line 8: This line stores the name of the directory in $dir for use later in the program. Notice that the / is included at the end of the directory name. Don't use \, even with Windows
  • Line 9: This line opens the directory
  • Line 10: This line starts a while loop that reads in each file name in the directory.
  • Line 12: This line creates the variable $filepath, which is the complete path to the file.
  • Line 13: This line checks to see whether the file is a graphics file by looking for the .jpg extension. If the file has a .jpg extension, the complete file path is added to an array called $gallery.
  • Line 16: This line sorts the array so the images are displayed in alphabetical order.
  • Line 17: This line starts the foreach loop that displays the images in the Web page

Wrap up
So what did you learn in this tutorial?
You learnt to:
and Delete files.
Organize files
Creating a directory
Building a list of all the files in a directory
Create a script that creates a image gallery.

Hope you enjoyed the tutorial ^^
PPDP's Tutorial

Is This A Good Question/Topic? 0
  • +

Page 1 of 1