You are on page 1of 36

File And Directory Manipulation In PHP (part 2)

By icarus 2003-08-21 Printed from DevShed.com URL: http://www.devshed.com/Server_Side/PHP/File_Manipulation2/ The Road Ahead In the first part of this article, I introduced you to the basic filesystem functions available in PHP, showing you (among other things) how to read and write files on the file system and obtain detailed status information on any file, including data on attributes like file sizes and permissions. That, however, was just the tip of the iceberg. PHP's filesystem API includes a number of specialized file and directory functions, which let you copy, delete and rename files; scan directories; work with uploaded files over HTTP; perform pattern matches on file names; and read and write to processes instead of files. So keep reading we've got a long and interesting journey ahead of us!

Stripping It To The Bone You may remember, from the first part of this article, how I used the fgets() function to read the contents of a file and print it to the browser. In case you don't, here's a quick reminder:

    

     !"     # $ % " &   

 !   '      ( "  " )    ' " 01 2     3  " ) &    

4  ' 5  '  ) )

   ! 7 '  ) &

#      &

8 #    

#   '  ) &

9 @ A B C DE F 9 G H I D @ G BP Q Q R1S S TVUW3XYQ Q P I `a

PHP also offers the fgetss() function, which works just like the regular fgets() function, except that it also strips out HTML and PHP code from the lines it reads. So, for example, if you had a file containing intermingled HTML code and ASCII text (as most HTML files are), like in the following example,

bc d e Sa

b c X RfT a

b g c X R1T a b h UT i a b d R h S X h UpfTfX pVq P r P s3X S S tVu RVsv WVwq P r P s3X S S u R1T1T3v W1w3q P r P a

bd p a

bd T a

U W y b x d x RVsX q P VD Rf A P t1v VX q P P sfUS U pVq P s1s1r r P a

p X x q P @ A D G ` q P a b v e wt pVsfq P g  D C g C 3 B d3 e P bhabR c f f v T dVc q r c XVv w cVd q rR1S d q P P h U p1TfX p1q rRfS v wWVq P R h t e v T1T1S X P c tVu RVsX q g a S D B t e vS D b gR a b g h a b g x  U W d a b gd T a b gd p a b d p a d  b d T a b x UW dyx RVsX q P VD Rf A PVtVv X q P P s1US U pVq P h A @ h P a

i C Dt e v S B j k @ h A l m k A  D d f D m Q m C D  k A D A @ B Cn rTVD @3 r r

U W d a b gx 

b gd T a

o p qVrVs

o q rVs

o q tvufwVx y u q z { | s

}f~ 

o p qVtVs

o p qVrVs

o q rVs

o q tVs

o Vqy Vw z V ~ f 1x Vw z { f rVz 11 s

o s o vu1rfwV z z s o x y rVfz p  p 3 3 x t qVuVz { ufwVx yuVq z { 1 q z r1tfw r1z f x yVz 1V x t1t1 w u1V Vw z s ~ v  ~ o p 1s o p so p  q so p q t1so p q rVs o q rVsYo q t1so qy 13w z V ~ 1 Vx w z f rVz sx ~ 3 ~  ~   {

t1 3{ {

o p  q s

o p qVtVs

o p qVrVs

1 V

you could retrieve just the ASCII from it with the following example script:

 

 

  1  3 

 

  

 

  1 V3Y

Here's what the output looks like:

Learning To SMILe Use SMIL to quickly build new Web-based multimedia applications | 20 Dec 2002   Writing A User Manual (part 1) It ain't easy writing a user manual...but our cheat sheet should help | 20 Dec 2002

Fertile Fields In addition to the simple fgets() function, PHP also offers the more-sophisticated fgetcsv() function, which not only reads in data from a file, but also parses each line and, using the comma (,) symbol as delimiter, splits the data on each line into fields for further processing. The return value from every call to fgetcsv() is an array containing the fields found. An example might make this clearer. Consider the following CSV file,

 1 V 

  v

! " f!

and this PHP script, which reads it and displays the information within it as an XML document:

#$%&% ' ' ( % ) 02143!56 7 8 9

( A # $ B C!D E ) F 9 G ( 0 H I P Q R I $ S A T )@&!

( A # @ ( DD) @ 6 G( 0 S A T )@&! ' ' 9 ) 6 U G D )26 ( F ) 7 V

W U G D ) 0 7 C!)!H A X 9 ) F 9 Q 6 B 6 A T ' ' ( % ) 02U G D )

W U &2H!U ( % ) 0!Y W U G D ) 0 7 C2) ` A F A a ( F V G ) Y A b4( X D V0 ( 6 ( % ) 0U G D ) A a T ' ' F ) 7 V2U G D )

c2& G D )Y d U ) ( U Y W U & a a

' ' @ F ) 7 6 )21f3!529 6 F X @ 6 X F )

) @ & (!A # X 9 ) F S A T

W U G ) D V 9H!U 8 ) 6 @ 9 E Y W U & ` P R R R a T

) @ & (!A # X 9 ) F 0 7 C2) S A Q W U G ) D V 9 g R h Q A # ' X 9 ) F 0 7 C2) S A T

) @ & (!A # U X D D 0 7 C!) S A Q W U G ) D V 9 g i h Q A # ' U X D D 0 7 C2) S A T

) @ & (!A # 7 8 ) S A Q W U G ) D V 9 g P h Q A # ' 7 8 ) S A T

( A # @ G6 p S A Q W U G) DV 9 g q h Q A # ' @ G6 p S A T )@&!

r s t u!v w x y r v

x x s u r2 r

s u r t r s t uv w x s u r s u v

In this case, the comma-separated values in the input file are automatically parsed into an array, and can then be processed, or reassembled in any order you like, to create different output. Here's what the script above results in:

<?xml version='1.0'?> <collection><user><username>john</username><fullname>John Doe</fullname><age>67</age><city>India</city></user><user><username>sue</use rname><fullname>Sue Me</fullname><age>32</age><city>New York</city></user><user><username>sarah</username><fullname>Sarah Whu</fullname><age>10</age><city>Korea</city></user><user><username>ramu</us ername><fullname>R Amulet</fullname><age>23</age><city>London</city></user></collection>

Configuring The System If what you're really after involves reading configuration variables in from a standard .INI file, you don't need to write custom code to parse the file and read in the variable-value pairs. Instead, just use PHP's parse_ini_file() function, which automatically takes care of this for you. Consider the following sample .INI file,

!d e

e r y s r d u ffr

u r d x g x u x h! x u i j h

s  h!r d x r s x s

k!l m!n o pq r s tuwv x

y o k2l r z!k2l q { t | u} t q

y z q!~ | o m }2uwz o  { t k!~

o kk2t z { u! t k!~ o | l | } r n t!q ~ l t

~ l { 2u2 { k!~

| t l y o z n }2uwz o

~ n r 2u!} t q

| t l { tk2l q ux

o | t!p | o ~2uwz o o y }

o | t q t | uwz o o y }

and the PHP code to parse it:

~~ q t { r n t2{ o| t l y

r n t z l k!t!u q l k! l r z r | t l yw f r n twr z { ol | | l }

y l { luw~ l | q t r z r r n t r n t z l k2t ~ | rz { l | | l }

A quick glance at the output shows that PHP has, indeed, read the file, parsed its contents, and converted the variable-value pairs into an associative array.

Array ( [printing] => bsd [default case] => lower [log file] => /var/log/samba/log.%m [printcap name] => /etc/printcap [max log size] => 50 [domain master] => 1 [dns proxy] => [comment] => Temporary file space [path] => /tmp [read only] => [public] => 1 [create mask] => 0777 [force group] => nobody [force user] => nobody ) The only problem with the approach, however, is that variables with the same name from different sections will override each other; if there are multiple configuration variables with the same name, the output array will always contain only the last value. In order to illustrate, look what happens when I add a new section to the sample file above which repeats some of the variables from a previous section:

!w

 ! 2

!  w

2 !2 

! 2w !

2 ! !  !

22 !

! 2w

2 

! 2w

 w

2 w 

22

!w

2 

!2 !w

!!

Here's the output:

Array ( [printing] => bsd [default case] => lower [log file] => /var/log/samba/log.%m [printcap name] => /etc/printcap [max log size] => 50 [domain master] => 1 [dns proxy] => [comment] => Shared area [path] => /shared [read only] => [public] => 1 [create mask] => 0777 [force group] => nobody [force user] => nobody [printable] => [create mode] => 0770 [directory mode] => 0770 [writeable] => 1 ) As you can see, some of the variable-value pairs (from the "temp" section of the file) have been lost. PHP offers a solution to this problem by allowing a second, optional argument to parse_ini_file() - a Boolean indicating whether the namespaces of the various sections should be respected. When I add that to the script above,

2 

!! ! w f w 

 2

 2

look how drastically the output changes:

Array ( [global] => Array ( [printing] => bsd [default case] => lower [log file] => /var/log/samba/log.%m [printcap name] => /etc/printcap [max log size] => 50 [domain master] => 1 [dns proxy] => ) [temp] => Array ( [comment] => Temporary file space [path] => /tmp [read only] => [public] => 1 [create mask] => 0777 [force group] => nobody [force user] => nobody ) [shared] => Array ( [comment] => Shared area [path] => /shared [printable] => [create mode] => 0770 [directory mode] => 0770 [writeable] => 1 ) ) With the addition of the second argument to parse_ini_file(), PHP now creates a nested array, with the outer array referencing the sections, and each inner one referencing the variables in each section.

The Right Path In addition to functions that allow you to obtain information on file sizes, permissions and modification times, PHP also offers a number of functions designed to manipulate file and path names, and split a file path into its constituent components. The two most commonly-used ones here are the basename() function, which returns the filename component of a path, and the dirname() function, which returns the directory name component of a path The following example demonstrates the basename() and dirname() components in action, by splitting a file path into its constituents:

       

    

        ! " # #         $ 

% % & ' ( ) 0 1 ( ' 2 3 0 4 ' 5) 6 782

2 3 9 4@ AB( ' 2 3 0 4 ' 5( CD@ E 1 ( ' ) 6 782 F G & 6 0 9 H E @ I ' I ) @ P QR

Here's the output: File is httpd Directory is /usr/local/apache/bin You can also use the pathinfo() function to obtain this information - this function returns an associative array containing keys for directory name, file name and file extension. Take a look at this next script, which returns this information for the directory holding the currently executing script.

SQ&9& % % & 6 ' C 23 T ' ' 2 ) 0 U ( V 2& 6 0 9

G ( ) U 4WX& 6 0 9 ( ) U 4 F G Y `ba c dBa cfe g h iBh Y `ba p q g r H P %% & ' () 0 () U 4

& ' () 0Y ' F G () U 4 H P QR

Here's the output:

Array ( [dirname] => /dev/php [basename] => fs.php [extension] => php ) Finally, you can use the realpath() function to translate relative paths into absolute ones, as below:

SQ&9& % % ' 2 0 T ' ) CB% T C ' % V 4 3 6 V % V ( s

2 3 9 4' 2 6 V & 6 0 9 F @ % T C ' % V 4 3 6 V % 6 & 6 3 9 2 % s ( ) % E E % E E % V ( s % @ H P % % ' 2 0 T ' ) CB% V ( s

t t u v w x u y Bt

v u v w t t

You can obtain the script's current working directory by combining the dirname() function with the special $_SERVER['SCRIPT_FILENAME'] variable,

v Bx u u v y w u v w u D u y dv e f gbh i jDh ifk l g BiDm n o f p m q h r sBth l u

or with the alternative getcwd() function, as below,

t t v v w x u u v y w wB u x y v u v w u

v Dx u u v y w u v w u 8 v v w wB

or even through creative use of the realpath() function, as below:

v Bx u u v y w u v w u D u v w

Move It In addition to offering you path information, PHP comes with a whole bunch of functions designed to simplify the task of moving, copying, renaming and deleting files on the filesystem. The first of these is the copy() function, which accepts two arguments, a source file and a destination, and copies the former to the latter. Here's an example which demonstrates:

yz{|{ } } ~ |  ~ f   

} } ~ {

 

~ {  ~ { D  z

Note that if the destination file already exists, copy() will usually overwrite it. A failure to copy the file will cause copy() to return false; success returns true. A corollary to the copy() function is the rename() function, which can be used to both rename and move files. Like copy(), it too accepts two arguments, a source file and a destination file. Consider the following example, which renames a file,

yz{|{ } } ~ |  ~ f   

} }  8

 

   B   8 8  z

and this one, which simultaneously renames and moves a file.

X 8 8

It's possible to rename directories in the same manner as files - as illustrated in this next snippet:

f 8

The rename() function comes in handy when you need to update files which are constantly being used by multiple

The rename() function comes in handy when you need to update files which are constantly being used by multiple processes. Instead of directly updating the target file with new data, rename() allows you to copy the original file contents into a new file, make your changes and then, once you're happy with the result, simply rename() the new file to the old one. The following example demonstrates:

D 8

B D D 8 8

B D

D X D 8

Note my use of the tempnam() function above - this function generates a unique file name, given a directory and a

Note my use of the tempnam() function above - this function generates a unique file name, given a directory and a filename prefix, and can help to avoid filename collisions between different processes. When it comes time to delete files, PHP offers the unlink() function, which can be used to erase a file from the filesystem. Consider the following example, which demonstrates by deleting a specific file:

         8 




You can also use the unlink() function to iterate over a directory and remove all the files within it - this is demonstrated in an example coming up shortly. Note that the unlink() function (and indeed, all other file manipulation functions) will fail if the user ID under which the Web server is running does not have adequate permissions to delete or otherwise modify the named file(s).

Beam Me Up Most often, you'll find yourself using rename(), copy() and unlink() functions in the context of files uploaded to PHP via a Web browser - so-called "HTTP file uploads". Consider the following example, which demonstrates:

 



   

!"#$#

% & ' ( ) 0 1 243 5 6 7 8 9 @ AB% C 7 D E

"G

! & H I AQP R S C T # P U V AB9 W C % # X I C Y & H I AB` a X C X V X S C % H R U V ! " U ) 0 3cb dcefb dg6 7 1 hg1 0 3cb i p 7 D " G V

ABP C $ H a U V # H 8 C V G

qgP 8 S I % # C % H R r

! @ IG

! % R # 9 C C T # P U V 5 P s C V R X A4P U V a P 8 S V 8 % t P U V u v V G

!#G

p % WP r

! @ IG

! % R # 9 C C T # P U V & % W P V R X A4P U V & % W P V G

!#G

! % R # 9 C C T # P U V 8 9 @ AB% C V R X A4P U V 8 9 @ AB% C V w X W 9 P U V xga ay A X P V G

! Y&H IA G

!"

d e f g h i j k l m m n l m o m n p pq r d s t u v w s r x d e f g h i j kcl m m n l m y v m n w ps z z s { {|e f g h i j k l m m n l m y v m n w ps z } v z s { {|e f g h i j k l m m n l m y v m n w p

s z v } v z s r d s h ~ 4 4 w s r x d w e f  Bk l m m n r d s gB v ~ w s r x v ~4 ~ ~ ~ B

e ~ ~ ~4p y f ~ ~ d s s s s s s r d s g~ B ~ ~ w s r x

y f f d s s r d s f~ B w s r x Bu ~ B u  u y

e u y  p s h fkcj |h z y d r fi fj kd m s e f  4k l m m n s m r s x

e u p y f u y d e u y r d s j ~4 u y s y f d r r x

e Bp y f ~ f d e ~ ~ ~ r x z y v ~  ~ B4  h g

d e f g h i j k l m m n l m y v m n p ps z z s r e ps z s x

d e f g h i j k l m m n l m y v m n p ps z } v z s r e  p s } v z s x

 4  4 c BB  4 Q

 B 4 c B B 4 B  4

4 g cc Bc 4B 4 c 4 f B

In this case, when a file is uploaded, it is automatically stored in a temporary directory by PHP, and its temporary filename is exposed via the "tmp_name" key of the $_FILES array. A copy() function can then be used to copy the uploaded file from its temporary location to its new location, and it can also be renamed along the way if needed (as in the above example). Once the script finishes executing, the temporary file is automatically deleted by PHP. Since file uploads are fairly common in PHP, the language also offers two specialized functions designed specifically to assist you in the process of handling such uploaded files: the is_uploaded_file() function, which tests if a file was uploaded via the HTTP POST method, and the move_uploaded_file() function, which is used to move an uploaded file to a new location after verifying its integrity. Here is a rewrite of the previous example using these functions, in order to better illustrate how they can be used:

!@HaTG !"#$#

% & ' ( ) 0 1 243 5 6 7 8 9 @ AB% C 7 D E

"G

! & H I AQP R S C T # P U V AB9 W C % # X I C Y & H I AB` a X C X V X S C % H R U V ! " U ) 0 3cb dcefb dg6 7 1 hg1 0 3cb i p 7 D " G V

ABP C $ H a U V # H 8 C V G

qgP 8 S I % # C % H R r

! @ IG

! % R # 9 C C T # P U V 5 P s C V R X A4P U V a P 8 S V 8 % t P U V u v V G

!#G

p % WP r

! @ IG

! % R # 9 C C T # P U V & % W P V R X A4P U V & % W P V G

!#G

! % R # 9 C C T # P U V 8 9 @ AB% C V R X A4P U V 8 9 @ AB% C V w X W 9 P U V xga ay A X P V G

! Y&H IA G

d e f g h i j k l m m n l m o m n p pq r d s t u v w s r x d e f g h i j kcl m m n l m y v m n w ps z z s { {|e f g h i j k l m m n l m y v m n w ps z } v z s { {|e f g h i j k l m m n l m y v m n w p

s z v } v z s r d s h ~ 4 4 w s r x d w e f  Bk l m m n r d s gB v ~ w s r x d w f u v f d e f g h i j kcl m m n l m Bv f ~ 4 m n r r d s t 4 w s r x v ~4 ~ ~ ~ B

e ~ ~ ~4p y f ~ ~ d s s s s s s r d s g~ B ~ ~ w s r x

y f f d s s r d s f~ B w s r x Bu ~ B u  u y

e u y ps h fkcj |h z y d r fi fj kd m s e f  4k l m m n s m r s x

e u p y f u y d e u y r d s j ~4 u y s y f d r r x

e Bp y f ~ f d e ~ ~ ~ r x z y v ~  ~ B 4  h g

d e f g h i j k l m m n l m y v m n p ps z z s r e  p s z s x

 4 

 4  4 c BB  4 4 4  Q

B B 4 c

4 g 4 g  4 4 B 4 B  4

4 g c Bc 4B 4 c 4 f B

Diving Into Directories Thus far, most of the examples you've seen have dealt with individual files. However, you often find yourself faced with the task of iterating over one or more directories and processing the file list within each. In order to meet this requirement, PHP offers a comprehensive set of directory manipulation functions, which allow developers to read and parse an entire directory listing. In order to demonstrate, consider the following simple example, which lists all the files in the directory "/bin":

             !#" $ %   ! &

' " $ %   (0) 1   2 !  3  & ! "  $ & 4#  56!

' 3  & (87  9   7 1   $  ! #3  & ! "  $ & 46  3   & 2 !6@   !0  2 

@ A 2 B 3  & A ' 3  & C C

 @ A ' 3  (0$  !  3  & A ' 3  & C C

    ! &   !#$ E ! & @   !8  2 

   &    @   !   56! 2

F6   !0A A ' @   !   56!0(8& !  3 3  & A ' 3  C CG ( (#@   2 ! C

 @ A A ' @   !   56!8G (07 H 7 C I IA ' @   !   56!8G (07 H H 7 C C

' "$ % P P 1

! "  $ ' 3  & H 7  7H ' @   !   56!0H 7 Q  7 1

T T U V W X Y#` a b Y U c W b d

U VW X Y ` ab e f ` g h i

S Y U g W0p q q f U W r s c t u v w xyt 6yq q p i

You can combine the script above with the getcwd() function discussed earlier to obtain a list of all the files in the current working directory at any time - I'll leave that to you to try out for yourself.

A Pattern Emerges Want to list just the files matching a specific pattern? Use the neat little fnmatch() function, new in PHP 4.3, which matches strings against wildcard patterns. Here's a quick example:

g T T X Y c ` a b Y U c W b d#s 6Y

f ` a b 8p T a s p i TT X Y c ccY bs

f c c Y b s68p Y p i T T W Y s#` a b Y U c W b d6 s `8 b X Y6 a V Y0V a X c

a e aX ` a b e f ` a b h h

d e f g6h0i j k l f m d e f m n n

p p q k m r q k#i s k m t k8t u q

v g t k0d d e t k l r w6k0h8m k r f f m d e f g n nx h h# r t u k n

p p t k l r w6k8w6r q y g k uzu k r m y g#j r q q k m l { j m l q q

d l w#r q y g d e j r q q k m l { e t k l r w6k n n

k y g i#e f m | } p }| e t k l r w6k0| } ~ l } 

p p y t i u k#f m k y q i m

y ti u k f m d e f g n 

Here's an example of the output:

Here's an example of the output: /bin/ed /bin/egrep /bin/echo /bin/env /bin/ex The * wildcard matches one or more characters; if this is not what you want, you can also use the ? wildcard to match a single character. An alternative to using fnmatch() with the opendir() and readdir() functions is the glob() function, also new to PHP 4.3 - this function searches the current directory for files matching the specified pattern and returns them as an array. Consider the following rewrite of the example above, which demonstrates:

# 6

68 # 0 # #

6 66

z0 # 6 6 #

Since glob() looks in the current directory for files, remember to always chdir() to the correct directory before executing glob().

Purging The Dead Not only does PHP let you read directory contents, it also allows you to create and delete directories with the mkdir() and rmdir() functions respectively. Consider the following example, which creates a directory,

0 0 8 0 #

and its mirror image, which removes the newly-created directory.

# 0 0 0

As with the other filesystem manipulation functions, these functions too will fail if the user the Web server is running as lacks sufficient privileges to create and delete directories on the disk. Directories created with mkdir() will be owned by the process running the Web server. It's interesting also to note that the rmdir() function operates only if the directory to be deleted is completely empty. Try running it on a directory containing existing files, as below,

0 0 8 0 # #

0 0 8 0 #

0 0 8 0 6 # 6 0

6 # 0 0 # #

and you'll be rewarded with the following error:

Warning: mkdir(): File exists in /home/web/rmdir.php on line 4 Could not remove directory Since it's unlikely that you'll find empty directories just waiting for your rmdir() in the real world, you'll normally need to empty the directory manually prior to calling rmdir() on it. The following example demonstrates, by combining the unlink() function discussed previously with some recursive logic to create a function designed specifically to erase the contents of a directory (and its sub-directories) in one fell swoop:

6 ## 6 # # 6

6 # 6 #

00

6 0 8 8 #8

8 0 y 8 0

Size Does Matter If you're looking for information on the total size of a partition or mount point, PHP offers the relatively-new disk_total_space() and disk_free_space() functions, which return the total available space and total free space, in bytes, on a particular partition. Consider the following example, which demonstrates:

           

 "!$#  # %   &   '  () 0   '  1 '    &32   &4  5 

 5 4# 6    '  0   '  1 ' $  5  7 # 8   2  & 9 &   @ A     ' A   5  9   B  9 C D E F G C D E F B B 8 # H4I P  P  # %  5 4# 6    '   4  5  7 # 8   2  & 9 &   @ A    A   5  9   B  9 C D E F G C D E F B B 8 # H4I P  P  # %   5  ' 5 2 '   $2   4 & 5

 &   @ A 2   & A   5 4!   2  & 9 9 &   @ A     ' A   5  9   B Q &   @ A    A   5  9   B B  9 C D E F G C D E F B B %  5 4# 6    ' 2   &4  5  7 # 8  &   @ A 2   & A   5 $8 # H4I P  P  # %   5  ' 5 2 '   4RS2   4 & 5

 5 4# RS2   4 &   5  7 # 8   2  & 9 9 &  @ A     'A   5  9   B Q

&   @ A    A   5  9   B B  &   @ A     ' A   5  9   B GTC D D B 8 # R3# % U

Here's an example of what the output might look like:

Total available space: 7906 MB Total free space: 4344 MB Total used space: 3562 MB % used space: 45 %

In Process Just as PHP offers the fopen() and fclose() functions to open and close file handles, there's also the popen() and pclose() functions, which can be used to open uni-directional handles to processes. Once a process handle has been created, data can be read from it or written to it using the standard fgets(), fputs(), fread() and fwrite() file functions. Consider the following example, which demonstrates by opening a pipe to the "cat" command:

V V W X Y `$a b ` c d Y)e W$X f W g Y h h

i X a)pqX W X Y ` r s V t u ` V g b e V Y e g V X b h h v"c s w s f s x y V V f Y b c$X f W g Y h h"W e X e u ` e W) b f u b t d Y

i g W ` e Y ` e h"p4 f Y b c r i X a w x y V V c u h X d b )g W ` e Y ` e h

Y g a W i g W ` eY ` eh y V V g d W h Y$X f W g Y h h

X g dW h Y r i X a x y

As you can see, opening a pipe to a process and reading from it is very similar to opening and reading a file. As with files, the first step is to obtain a handle to the process with popen() - this handle serves as the foundation for all future communication. Once a handle has been obtained, data can be read from, or written to, the handle using the file input/output functions you're already familiar with. The handle can be closed at any time with the pclose() function. If you need bi-directional communication, PHP 4.3 also offers the new proc_open() and proc_close() functions, which offers a greater degree of control over process communication.

Disk Full And that's about all I have. Over the course of the last few pages, I took you ever deeper into the waters of PHP's filesystem API, demonstrating a number of its more arcane features and functions. I showed you how to strip out program code from a file while reading it, how to parse comma-separated data from a file into PHP structures, and how to read configuration variables into a PHP associative array. Next, I introduced you to PHP's file copy, move and delete functions, and demonstrated them in the context of a file upload application. I also showed you how to read and display the contents of a directory, and how to recursively iterate through a series of nested directories. Finally, I wrapped things up with a brief look at how to obtain disk usage reports for a mount point or partition, and explained how you could read data from processes just as you do with files. While this tutorial did cover many of the common uses of PHP's file and directory manipulation functions, it is by no means exhaustive. There are many more things you can do with PHP's file functions - and you can get some great ideas (and learn a number of interesting things as well) by perusing the PHP manual pages for these functions, at the links below: PHP's file manipulation functions, at http://www.php.net/manual/en/ref.filesystem.php PHP's directory manipulation functions, at http://www.php.net/manual/en/ref.dir.php And while you busy yourself with those links, I'm off on a short break. See you soon! Note: Examples are illustrative only, and are not meant for a production environment. Melonfire provides no

Note: Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!

This article copyright Melonfire 2000-2003. All rights reserved.

You might also like