Chúng ta có thể dễ dàng tạo thư mục trong PHP, nhưng trước đó bạn phải kiểm tra xem thư mục hoặc thư mục đó đã tồn tại hay chưa. Vì vậy, trong bài viết này, bạn sẽ học cách Kiểm tra và Tạo một thư mục hoặc thư mục trong PHP. Show phương pháp
Phương pháp 1. Sử dụng hàm file_exists(). Hàm file_exists() dùng để kiểm tra một tập tin hoặc thư mục có tồn tại hay không cú pháp file_exists( $path ) Thông số. Hàm file_exists() trong PHP chỉ chấp nhận một tham số $path. Nó chỉ định đường dẫn của tệp hoặc thư mục bạn muốn kiểm tra Giá trị trả về. Nó trả về True khi thành công và sai khi thất bại. Thí dụ PHP
The file path doesn't exists in GeeksforGeeks directory0 The file path doesn't exists in GeeksforGeeks directory1 The file path doesn't exists in GeeksforGeeks directory2 The file path doesn't exists in GeeksforGeeks directory3 The file path doesn't exists in GeeksforGeeks directory4 The file path doesn't exists in GeeksforGeeks directory5 The file path doesn't exists in GeeksforGeeks directory6_______1_______7 The file path doesn't exists in GeeksforGeeks directory6_______1_______0 is_dir( $file )0 is_dir( $file )1 is_dir( $file )2 is_dir( $file )3 The file path doesn't exists in GeeksforGeeks directory3 is_dir( $file )5 is_dir( $file )6 is_dir( $file )7 is_dir( $file )1 is_dir( $file )2 Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal0 The file path doesn't exists in GeeksforGeeks directory3 is_dir( $file )5 The file path doesn't exists in GeeksforGeeks directory4 Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal4 Đầu ra The file path doesn't exists in GeeksforGeeks directory Phương pháp 2. Sử dụng hàm is_dir(). Hàm is_dir() được sử dụng để kiểm tra xem tệp được chỉ định có phải là thư mục hay không cú pháp is_dir( $file ) Thông số. Hàm is_dir() trong PHP chỉ chấp nhận một tham số. Nó chỉ định đường dẫn của tệp hoặc thư mục mà bạn muốn kiểm tra Giá trị trả về. Nó trả về True nếu tệp là một thư mục nếu không trả về false Thí dụ PHPGiven https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal5 Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal6 Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal7 The file path doesn't exists in GeeksforGeeks directory1 Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal9 The file path doesn't exists in GeeksforGeeks directory3 The file path doesn't exists in GeeksforGeeks directory4 Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal6 mkdir(path, mode, recursive, context)3 The file path doesn't exists in GeeksforGeeks directory5 The file path doesn't exists in GeeksforGeeks directory6_______36_______6 The file path doesn't exists in GeeksforGeeks directory6_______21_______7 mkdir(path, mode, recursive, context)9 is_dir( $file )1 is_dir( $file )2 The file path doesn't exists in GeeksforGeeks directory6 13_______46_______4 is_dir( $file )6 is_dir( $file )1 is_dir( $file )2 The file path doesn't exists in GeeksforGeeks directory6 19_______46_______4 The file path doesn't exists in GeeksforGeeks directory4 Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal4 Đầu ra Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal Phương pháp 3. Sử dụng hàm mkdir(). mkdir() tạo một thư mục mới với tên đường dẫn được chỉ định cú pháp mkdir(path, mode, recursive, context) Thông số
Giá trị trả về. Nó trả về true khi thành công hoặc false khi thất bại. Thí dụ PHP
The file path doesn't exists in GeeksforGeeks directory4 15 16 17 18 The file path doesn't exists in GeeksforGeeks directory6 <?PHP 0<?PHP 1The file path doesn't exists in GeeksforGeeks directory4 Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal4 đầu ra 1 Thí dụ. Ví dụ này kiểm tra tệp có tồn tại hay không và nếu tệp không tồn tại thì tạo tệp mới bằng hàm mkdir() Làm cách nào để tạo đường dẫn thư mục trong PHP?Hàm mkdir() được sử dụng để tạo thư mục trong PHP . Nó là một chức năng sẵn có trong PHP. Hàm mkdir() tạo một thư mục mới với tên đường dẫn đã chỉ định. Đường dẫn và chế độ được gửi dưới dạng tham số cho hàm mkdir() và nó trả về TRUE nếu thành công hoặc FALSE nếu thất bại.
Làm cách nào để hiển thị đường dẫn tệp trong PHP?Cú pháp. $filename = pathinfo(path); Ví dụ. Ví dụ này giải thích hàm pathinfo() sẽ trả về thông tin về đường dẫn.
Làm cách nào để tạo một tệp trong một thư mục trong PHP?Tạo tệp PHP - fopen()
. Có thể hơi khó hiểu, nhưng trong PHP, tệp được tạo bằng cách sử dụng cùng chức năng được sử dụng để mở tệp. Nếu bạn sử dụng fopen() trên một tệp không tồn tại, nó sẽ tạo tệp đó, với điều kiện là tệp được mở để ghi (w) hoặc nối thêm (a).
Làm cách nào để có đường dẫn đầy đủ trong PHP?Hàm realpath() trả về tên đường dẫn tuyệt đối. Hàm này loại bỏ tất cả các liên kết tượng trưng (như '/. /', '/. /' và thêm '/') và trả về tên đường dẫn tuyệt đối. |