|
użytkowników online: 59
|
OPINIE UŻYTKOWNIKÓW
|
W takich dniach, jak ten, nie żałuję, że wykupiłem abonament. Korzystam z porad na tych stronach nawet kilkanaście razy w tygodniu i dzięki nim prace nad stronami dla klientów idą mi o wiele szybciej, a strony wyglądają bardziej profesjonalnie. Nie wiem, jak mogłem wcześniej pracować bez dostępu do porad w tym serwisie!
Wojciech Miszkiewicz
|
|
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net
[Spis]
[A]
[B]
[C]
[D]
[E]
[F]
[G]
[H]
[I]
[J]
[K]
[L]
[M]
[N]
[O]
[P]
[Q]
[R]
[S]
[T]
[U]
[V]
[X]
[W]
[Z]
file_exists (PHP 3, PHP 4, PHP 5) file_exists -- Sprawdza czy plik lub katalog istnieje Opisbool file_exists ( string nazwa_pliku )
Zwraca TRUE jeśli podany plik lub katalog w parametrze
nazwa_pliku istnieje; FALSE w przeciwnym wypadku.
Pod Windows użyj //nazwa_komputera/udostepnione/nazwa_pliku lub
\\nazwa_komputera\udostepnione\nazwa_pliku aby sprawdzić pliki
udostępnione przez sieć.
Przykład 1. Sprawdzanie czy plik istnieje |
<?php
$filename = '/sciezka/do/foo.txt';
if (file_exists($filename)) {
echo "Plik $filename istnieje";
} else {
echo "Plik $filename nie istnieje";
}
?>
|
|
Notatka: Wyniki działania tej funkcji są
buforowane. Zobacz opis funkcji clearstatcache() aby uzyskać
więcej informacji.
Podpowiedź: Od wersji 5.0.0 PHP ta funkcja
może być użyta także z niektórymi wrapperami URL. Zobacz
Dodatek L aby uzyskać listę wrapperów które obsługują
funkcjonalność z rodziny stat().
Patrz także is_readable(), is_writable(),
is_file() i file().
User Contributed Notes07-Jan-2006 10:08
I wrote this little handy function to check if an image exists in a directory, and if so, return a filename which doesnt exists e.g. if you try 'flower.jpg' and it exists, then it tries 'flower[1].jpg' and if that one exists it tries 'flower[2].jpg' and so on. It works fine at my place. Ofcourse you can use it also for other filetypes than images.
<?php
function imageExists($image,$dir) {
$i=1; $probeer=$image;
while(file_exists($dir.$probeer)) {
$punt=strrpos($image,".");
if(substr($image,($punt-3),1)!==("[") && substr($image,($punt-1),1)!==("]")) {
$probeer=substr($image,0,$punt)."[".$i."]".
substr($image,($punt),strlen($image)-$punt);
} else {
$probeer=substr($image,0,($punt-3))."[".$i."]".
substr($image,($punt),strlen($image)-$punt);
}
$i++;
}
return $probeer;
}
?>
Fabrizio (staff at bibivu dot com)
22-Dec-2005 03:11
here a function to check if a certain URL exist:
<?php
function url_exists($url) {
$a_url = parse_url($url);
if (!isset($a_url['port'])) $a_url['port'] = 80;
$errno = 0;
$errstr = '';
$timeout = 30;
if(isset($a_url['host']) && $a_url['host']!=gethostbyname($a_url['host'])){
$fid = fsockopen($a_url['host'], $a_url['port'], $errno, $errstr, $timeout);
if (!$fid) return false;
$page = isset($a_url['path']) ?$a_url['path']:'';
$page .= isset($a_url['query'])?'?'.$a_url['query']:'';
fputs($fid, 'HEAD '.$page.' HTTP/1.0'."\r\n".'Host: '.$a_url['host']."\r\n\r\n");
$head = fread($fid, 4096);
fclose($fid);
return preg_match('#^HTTP/.*\s+[200|302]+\s#i', $head);
} else {
return false;
}
}
?>
in my CMS, I am using it with those lines:
<?php
if(!isset($this->f_exist[$image]['exist']))
if(strtolower(substr($fimage,0,4)) == 'http' || strtolower(substr($fimage,0,4)) == 'www.'){
if(strtolower(substr($image,0,4)) == 'www.'){
$fimage = 'http://'.$fimage;
$image = 'http://'.$image;
}
$this->f_exist[$image]['exist'] = $this->url_exists($fimage); } else {
$this->f_exist[$image]['exist'] = ($fimage!='' && file_exists($fimage) && is_file($fimage) && is_readable($fimage) && filesize($fimage)>0);
}
}
?>
flobee
13-Dec-2005 01:24
file_exists overwrites the last access (atime) !
try:
if (@fclose(@fopen( $file, "r"))) {
// true;
} else {
// false;
}
to check if important for you
ihoss dot com at gmail dot com
19-Oct-2005 04:37
The following script checks if there is a file with the same name and adds _n to the end of the file name, where n increases. if img.jpg is on the server, it tries with img_0.jpg, checks if it is on the server and tries with img_1.jpg.
<?php
$img = "images/".$_FILES['bilde']['name'];
$t=0;
while(file_exists($img)){
$img = "images/".$_FILES['bilde']['name'];
$img=substr($img,0,strpos($img,"."))."_$t".strstr($img,".");
$t++;
}
move_uploaded_file($_FILES['bilde']['tmp_name'], $img);
?>
phenbach at phenbach dot com
20-Sep-2005 12:37
I recently had an issue with PLESK and copying file to other directories with the move_uploaded file function.
This would work on every linux server except plesk servers. I could figure it out and have yet to find out. I had to create a work a round and decided to use the exec() function.
As noted above the file_exist() function must need to wait for some time and I found the looking function a waste of resouces and didn't work for me anyway. So this is what I came up with.
function cp($source,$destination){
$cmd="cp -u ".$source ." ".$destination; //create the command string to copy with the update option
exec($cmd); //exec command
$cmd_test="ls -la ".$destination; //list file
exec($cmd_test,$out);
//If the file is present it $out[0] key will contain the file info.
//if it is not present it will be null
if (isset($out[0])){
return true;
}else{
return false;
}
}
davidc at php dot net
13-Sep-2005 10:05
Also while using the file_exists file, please make sure you do not start using stuff like,
<?php
if(file_exists($_GET['file'] . '.php')) {
include($_GET['file'] . '.php';
}
?>
you could use something like this..
<?php
$inrep = "./";
$extfichier = ".php";
$page = $inrep.basename(strval($_REQUEST["page"]),$extfichier).extfichier;
if(file_exist($page)) {
include($page);
}
?>
or even hardcode it.
So since pretty much all commercial server(s) have url_fopen on.. you can imagine that file_exists($_GET['file']. '.php')
is rather .. unsecure :)
-David Coallier
tma at 2p dot cz
24-Aug-2005 10:47
If checking for a file newly created by an external program in Windows then file_exists() does not recognize it immediately. Iy seems that a short timeout may be required.
<?
$file = 'file.tmp';
if ($h = popen("start \"bla\" touch $file", "r")) {
pclose($h);
$start = gettimeofday();
while (!file_exists(trim($file, " '\""))) {
$stop = gettimeofday();
if ( 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec'] > 500000) break; }
if (file_exists($file)) ?>
09-Aug-2005 04:20
That is true feshi. But, if you have your server configured correctly, those access logs will only be accessible by an admin or the root account. The webuser account that runs the php script will be unable to start reading from that file. That's the easiest fix.
feshi
04-Aug-2005 12:34
this code looks inocent
<?php
$filename=$_REQUEST["var"];
$filename .= ".data";
file_exist($filename){
include($filename);
}
?>
but if you pass something like script.php?var=test%00asbs
it should really can do bad things like including accesslog files if you replace "test" with something like "../logs/accesslog"
replayproducts@aliceposta --dot-- it
28-May-2005 06:26
<?
$file = "../file.php"
if ( file_exists ("$file")) {
echo"The page is locked!Remove $file to unlock";
exit();
}
rest of PHP script
?>
joe dot knall at gmx dot net
16-Mar-2005 12:59
concerning file_exists and safe_mode:
if safe_mode=ON and $file (in safe_mode_include_dir) is not owned by the user who executes file_exists($file), file_exists returns FALSE but still $file can be included;
I could handle this by setting safe_mode_gid=On and appropriate group-ownership
07-Mar-2005 11:00
Nathaniel, you should read the manual carefuly next time prior to posting anything here, as all you indicated is the fact you missed the idea of the include_path. To remind - include_path is for some functions only, mainly intended for include and require to simpify include/require operations (kinda way the #include works). It is NOT for any filesystem function, which would be damn annoying than helpful, which is quite understandable and obvious.
andrewNOSPAMPLEASE at abcd dot NOSPAMERSca
11-Feb-2005 02:29
file_exists will have trouble finding your file if the file permissions are not read enabled for 'other' when not owned by your php user. I thought I was having trouble with a directory name having a space in it (/users/andrew/Pictures/iPhoto Library/AlbumData.xml) but the reality was that there weren't read permissions on Pictures, iPhoto Library or AlbumData.xml. Once I fixed that, file_exists worked.
Nathaniel
09-Feb-2005 06:08
I spent the last two hours wondering what was wrong with my if statement: file_exists($file) was returning false, however I could call include($file) with no problem.
It turns out that I didn't realize that the php include_path value I had set in the .htaccess file didn't carry over to file_exists, is_file, etc.
Thus:
<?PHP
if ( file_exists('includes/config.php') )
{
include('includes/config.php');
}
if ( file_exists('/home/user/public_html/includes/config.php') )
{
include('includes/config.php');
}
?>
Just goes to show that "shortcuts for simplicity" like setting the include_path in .htaccess can just cause more grief in the long run.
ceo at l-i-e dot com
03-Dec-2004 12:44
Prior to 4.3.2, and 4.3.3 with open_basedir, this function generated an error/warning message when the file/directory in question did not exist.
aidan at php dot net
10-Apr-2004 06:46
alex at raynor nospam dot ru
05-Apr-2004 05:16
If you use open_basedir in php.ini and use file_exists for file outside open_basedir path, you will not be warned at log and file_exists returns false even if file really exists.
12-Feb-2004 03:32
On nix systems file_exists will report a link that doesn't point to a valid file as non-existant, ie: the link itself exists but the file it points to does not. Using is_link will report true whether the link points to a valid file or not.
|