To determine a file's existence

# check if file exists:

CODE:
  1. if (-e "filename.txt")
  2. {
  3.   #next action
  4. }

-e = existance
Filename could also be a variable as in this example

CODE:
  1. $filename="test.cgi";
  2. if (-e $filename)
  3. {
  4.   #next action
  5. }

Other switches;

File exists, has a size of zero: -z
File exists, has non-zero size: -s
Readable: -r
Writable: -w
Executable: -x

So, if we want to check whether we can read a file before we try to open it, we could do this:

CODE:
  1. $filename="test.cgi";
  2. if (-r $filename)
  3. {
  4.   #next action
  5. }

Text File: -T
Binary File: -B

They work in the same fasion
Multiple Tests

You can test for two or more things at a time using the "and" (&&) or the "or" ( || ) operators. So, if you want to know if a file exists and is readable before opening it, you could do this:

CODE:
  1. $filename="test.cgi";
  2. if ( (-e $filename) && (-r $filename) )
  3. {
  4.   #next action
  5. }