Hướng dẫn php if exists - php nếu tồn tại

Hàm file_exists() sẽ kiểm tra xem file hoặc thư mục có tồn tại hay không.

Hướng dẫn php if exists - php nếu tồn tại

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.freetuts.net, không được copy dưới mọi hình thức.

Cú pháp

Cú pháp: file_exists( $filename);file_exists( $filename);

Trong đó::

  • $filename là đường dẫn tới file hoặc thư mục cần kiểm tra.

Kết quả trả về

Hàm sẽ trả về True nếu file, thư mục truyền vào tồn tại. Ngược lại hàm sẽ trả về False.True nếu file, thư mục truyền vào tồn tại. Ngược lại hàm sẽ trả về False.

Bài viết này được đăng tại [free tuts .net]

Ví dụ

Ví dụ về cách sử dụng hàm file_exists():

Code

file_put_contents('test.txt', 'this is a test');

if (file_exists('test.txt')) {
	echo "file tồn tại!";
}else{
	echo "file không tồn tồn tại!";
}

Tham khảo: php.net

(PHP 4, PHP 5, PHP 7, PHP 8)

defined — Checks whether a given named constant existsChecks whether a given named constant exists

Description

defined(string $constant_name): bool(string $constant_name): bool

Note: :

If you want to see if a variable exists, use isset() as defined() only applies to constants. If you want to see if a function exists, use function_exists(). isset() as defined() only applies to constants. If you want to see if a function exists, use function_exists().

Parameters

constant_name

The constant name.

Return Values

Returns true if the named constant given by constant_name has been defined, false otherwise. true if the named constant given by constant_name has been defined, false otherwise.

Examples

Example #1 Checking Constants

file_exists()0

See Also

  • define() - Defines a named constant
  • constant() - Returns the value of a constant
  • get_defined_constants() - Returns an associative array with the names of all the constants and their values
  • function_exists() - Return true if the given function has been defined
  • The section on Constants

daniel at neville dot tk ¶

14 years ago

file_exists()1

file_exists()2

file_exists()3

file_exists()4

ASchmidt at Anamera dot net ¶

5 years ago

file_exists()5

file_exists()6

file_exists()7

file_exists()8

file_exists()9

file_exists( $filename);0

tris+php at tfconsulting dot com dot au ¶

13 years ago

file_exists( $filename);1

file_exists( $filename);2

file_exists( $filename);3

file_exists( $filename);4

file_exists( $filename);5

file_exists( $filename);6

file_exists( $filename);0

r dot hartung at roberthartung dot de ¶

12 years ago

file_exists( $filename);8

file_exists( $filename);9

$filename0

$filename1

Lars Lernestal ¶

10 years ago

$filename2

$filename3

file_exists( $filename);0

Shaun H ¶

14 years ago

$filename5

$filename6

file_exists( $filename);0

ASchmidt at Anamera dot net ¶

5 years ago

$filename8

$filename9

file_exists( $filename);0

tris+php at tfconsulting dot com dot au ¶

13 years ago

file_exists()1

file_exists()2

file_exists()3

file_exists( $filename);0

r dot hartung at roberthartung dot de ¶

13 years ago

file_exists()5

file_exists()2

file_exists()7

file_exists()8

r dot hartung at roberthartung dot de ¶

12 years ago

file_exists()9

$constant_name0

$constant_name1

$constant_name2

Lars Lernestal ¶

10 years ago

$constant_name3

$constant_name4

$constant_name5

file_exists( $filename);0

Shaun H ¶

5 years ago

$constant_name7

$constant_name8

$constant_name9

tris+php at tfconsulting dot com dot au ¶

13 years ago

constant_name0

constant_name1

r dot hartung at roberthartung dot de ¶

constant_name3

constant_name4

file_exists( $filename);0

12 years ago

12 years ago

constant_name6

constant_name7

constant_name8

constant_name9

true0

Lars Lernestal ¶

10 years ago

true1

true2

file_exists( $filename);0