Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Filesystem Functions : is_file

is_file

Tells whether the filename is a regular file (PHP 4, PHP 5)
bool is_file ( string filename )

Example 655. is_file() example

<?php
var_dump
(is_file('a_file.txt')) . "\n";
var_dump(is_file('/usr/bin/')) . "\n";
?>

The above example will output:

bool(true)
bool(false)

Related Examples ( Source code ) » is_file




Code Examples / Notes » is_file

rehfeld.us

regarding rlh at d8acom dot com method,
It is incorrect. Well, it works but you are not guaranteed the file extension using that method.
for example :   filename.inc.php
your method will tell you the ext is "inc", but it is in fact "php"
heres a way that will work properly.
<?php
$dh = opendir($dir);
while (false !== ($document = readdir($dh))) {
   $pos = strrpos($document, '.');
   if (false !== $pos && strlen($document) > $pos + 1) {
       $ext = substr($document, $pos + 1);
   }
}
?>


cristian dot ban

regarding note from rehfeld dot us :
In my experience the best( and easiest ) way to find the extension of a file is :
<?php
// use this when you are sure it actually has an extension.
$extension = end(explode(".", $file_name));
?>
or
<?php
// this one will also check if it actually has an extension
$parts = explode(".", $file_name);
if (is_array($parts) && count($parts) > 1)
$extension = end($parts);
?>


jonathan shaltz

Maybe this is a newbie mistake, but note that paths are relative to the filesystem and the location of the script.  This means that MS IIS virtual directories are not available by relative path - use an absolute.
This threw me because virtual directories ARE available for URLs, at least on IIS.


amraam

It seems that is_file doesn't return true for a file that is 0 bytes.  Perhaps it is something with the file system.  I am using IIS 3.0 on an NT4 box.  I worked around it using !is_dir($filename) but that seems a clunky way to do it.

punknroll

is_file returns false if you don't have the permissions for the file or the directory (eg.: you are web34 and the directory belongs to root)!

simoncharest

In rlh's example, "$ext=explode('.',$document);" is only good if you consider that the filename only possesses a single dot (".") and that it is right before the extension. You should get the last dot's position with the strRPos() function instead.
Another note : some files might not even have an extension (i.e.: mostly under Linux/Unix).


quietust

In PHP 4.1.0 under win32, this seems to print out a warning message if the file does not exist (using error_reporting = E_ALL & ~E_NOTICE).

andreas dot stagl

if you're running apache as a service on a win32 machine, an you try to determinate if a file on an other pc in your network exists - ex.: is_file('//servername/share/dir1/dir2/file.txt') - you may return false when you're running the service as LocalSystem. To avoid this, you have to start the Apache-Service as a 'registered' domain user.

guilherme guilherme

If you are trying to get the extension of the file. I have the following piece of code for you:
<?php
$extension = substr($file_name, strrpos($file_name, "."));
// That code must get the correctly extension in any cases.
?>


ludvig dot ericson

I tend to use alot of includes, and I found that the is_file is based on the script executed, not ran.
if you request /foo.php and foo.php looks like this:
<?php
include('foobar/bar.php');
?>
and bar.php looks like this:
<?php
echo (is_file('foo/bar.txt'));
?>
Then PHP (on win32, php 5.x) would look for /foo/bar.txt and not /foobar/foo/bar.txt.
you would have to rewrite the is_file statement for that, or change working directory.
Noting this since I sat with the problem for some time,
cheers, Toxik.


rlh

I do a lot of file parsing and have found the following technique extremely useful:
while (false !== ($document = readdir($my_dir)))
{
$ext=explode('.',$document);
if($document != '.' && $document != '..' && $ext[1])
{
                      'Do something to file...'
             }
}
It gets around the fact that, when working on website pages, the html files are read as directories when downloaded. It also allows you to extend the usefulness of the above method by adding the ability to determine file types e.g.
if($document != '.' && $document != '..' && $ext[1]=='htm')
or
if($document != '.' && $document != '..' && $ext[1]=='doc')


emin sadykhov azdg_nospam

File operations such as is_file (also is_dir, opendir, readdir) work slower with Absolute paths - processing time is increase in 2-3 times.
Current rule is actual only for PHP5 (tested on 5.0.4, 5.1.1, Windows and Linux, 1st and 2nd Apache)
Try to use relative paths in these operators.
Example tested on my machine:
<?php
# note: in the both conditions file really exists!
# WIN XP, PHP4
# processing time: ~ 0.0003 sec.
if(is_file("images/10.jpg")) echo 'file exists';
# processing time: ~ 0.0002 sec. !!!
if(is_file("C:/server/htdocs/mysite/images/10.jpg")) echo 'file exists';
# WIN XP, PHP5
# processing time: ~ 0.0004 sec.
if(is_file("images/10.jpg")) echo 'file exists';
# processing time: ~ 0.0010 sec.
if(is_file("C:/server/htdocs/mysite/images/10.jpg")) echo 'file exists';
?>


bill fumerola

be careful, is_file() fails on files larger than your integer storage (2^32 for most).
Warning: is_file(): Stat failed for bigfile (errno=75 - Value too large for defined data type)


riki1512 / a_t / gmx / d_ot / de

Be careful with big files. I get a
Warning: is_file(): Stat failed for all.rar (errno=75 - Value too large for defined data type) in /.../test.php on line 3
and FALSE as result for a file of 3,5 GB.


tatarynowicz

An easy way not to have to choose between hard-coding full paths and using relative paths is either via this line:
<?php
// in the bootstrap file
define('DIR_ROOT', dirname(__FILE__));
// in other files, prefix paths with the constant
require(DIR_ROOT . '/relative/to/bootstrap.php');
?>
or if you have to use a relative path:
<?php
require(dirname(__FILE__) . '/relative/to/this_file.php');
?>
This way all your paths will be absolute, yet you can move the application anywhere in the filesystem.
BTW, each successive call to dirname takes you one step up in the directory tree.
<?php
echo __FILE__;
// /www/site.com/public/index.php
echo dirname(__FILE__);
// /www/site.com/public
echo dirname(dirname(__FILE__));
// /www/site.com
?>


08-mar-2005 05:02

### Symbolic links are resolved ###
If you pass a symlink (unix symbolic link) as parameter, is_file will resolve the symlink and will give information about the refered file. For example:
 touch file
 ln -s file link
 echo '<? if (is_file("link")) echo "y\n"; ?>' | php -q
will print "y".
is_dir resolves symlinks too.


Change Language


Follow Navioo On Twitter
basename
chgrp
chmod
chown
clearstatcache
copy
delete
dirname
disk_free_space
disk_total_space
diskfreespace
fclose
feof
fflush
fgetc
fgetcsv
fgets
fgetss
file_exists
file_get_contents
file_put_contents
file
fileatime
filectime
filegroup
fileinode
filemtime
fileowner
fileperms
filesize
filetype
flock
fnmatch
fopen
fpassthru
fputcsv
fputs
fread
fscanf
fseek
fstat
ftell
ftruncate
fwrite
glob
is_dir
is_executable
is_file
is_link
is_readable
is_uploaded_file
is_writable
is_writeable
lchgrp
lchown
link
linkinfo
lstat
mkdir
move_uploaded_file
parse_ini_file
pathinfo
pclose
popen
readfile
readlink
realpath
rename
rewind
rmdir
set_file_buffer
stat
symlink
tempnam
tmpfile
touch
umask
unlink
eXTReMe Tracker