Tạo tập tin php trong một thư mục
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ụ PHPThe file path doesn't exists in GeeksforGeeks directory7 The file path doesn't exists in GeeksforGeeks directory8 The file path doesn't exists in GeeksforGeeks directory9 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 The file path doesn't exists in GeeksforGeeks directory7______16 The file path doesn't exists in GeeksforGeeks directory0 The file path doesn't exists in GeeksforGeeks directory10 The file path doesn't exists in GeeksforGeeks directory11 The file path doesn't exists in GeeksforGeeks directory12 The file path doesn't exists in GeeksforGeeks directory13 The file path doesn't exists in GeeksforGeeks directory3 The file path doesn't exists in GeeksforGeeks directory15 The file path doesn't exists in GeeksforGeeks directory16 The file path doesn't exists in GeeksforGeeks directory17 The file path doesn't exists in GeeksforGeeks directory11 The file path doesn't exists in GeeksforGeeks directory12 The file path doesn't exists in GeeksforGeeks directory10 The file path doesn't exists in GeeksforGeeks directory3 The file path doesn't exists in GeeksforGeeks directory15 The file path doesn't exists in GeeksforGeeks directory4 The file path doesn't exists in GeeksforGeeks directory14 Đầ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 The file path doesn't exists in GeeksforGeeks directory1 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ụ PHPThe file path doesn't exists in GeeksforGeeks directory15 The file path doesn't exists in GeeksforGeeks directory16 The file path doesn't exists in GeeksforGeeks directory17 The file path doesn't exists in GeeksforGeeks directory1____219 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 directory16 The file path doesn't exists in GeeksforGeeks directory63 The file path doesn't exists in GeeksforGeeks directory5 The file path doesn't exists in GeeksforGeeks directory6____366______16 The file path doesn't exists in GeeksforGeeks directory17 The file path doesn't exists in GeeksforGeeks directory69 The file path doesn't exists in GeeksforGeeks directory11 The file path doesn't exists in GeeksforGeeks directory12 The file path doesn't exists in GeeksforGeeks directory6____463 The file path doesn't exists in GeeksforGeeks directory64 The file path doesn't exists in GeeksforGeeks directory16 The file path doesn't exists in GeeksforGeeks directory11 The file path doesn't exists in GeeksforGeeks directory12 The file path doesn't exists in GeeksforGeeks directory6____469 The file path doesn't exists in GeeksforGeeks directory64 The file path doesn't exists in GeeksforGeeks directory4 The file path doesn't exists in GeeksforGeeks directory14 Đầu ra The file path doesn't exists in GeeksforGeeks directory1 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 The file path doesn't exists in GeeksforGeeks directory6 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ụ PHPThe file path doesn't exists in GeeksforGeeks directory7 The file path doesn't exists in GeeksforGeeks directory4 The file path doesn't exists in GeeksforGeeks directory65 The file path doesn't exists in GeeksforGeeks directory66 The file path doesn't exists in GeeksforGeeks directory67 The file path doesn't exists in GeeksforGeeks directory68 The file path doesn't exists in GeeksforGeeks directory6____670 The file path doesn't exists in GeeksforGeeks directory71 The file path doesn't exists in GeeksforGeeks directory4 The file path doesn't exists in GeeksforGeeks directory14 đầu ra The file path doesn't exists in GeeksforGeeks directory6 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 tệp trong thư mục cụ thể 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).
PHP là thư mục hay tệp tin?Hàm is_dir() trong PHP dùng để kiểm tra xem tệp được chỉ định có phải là thư mục hay không . Tên của tệp được gửi dưới dạng tham số cho hàm is_dir() và nó trả về True nếu tệp là một thư mục khác, nó trả về Sai.
Làm cách nào tôi có thể lấy tệp thư mục trong PHP?Hàm scandir() trả về một mảng các tệp và thư mục của thư mục đã chỉ định.
__ TRỰC TIẾP __ trong PHP là gì?Có thể sử dụng __DIR__ để lấy thư mục làm việc của mã hiện tại . Nó đã được giới thiệu trong PHP bắt đầu từ phiên bản 5. 3. Nó tương tự như sử dụng dirname(__FILE__). Thông thường, nó được sử dụng để bao gồm các tệp khác có trong một tệp được bao gồm. |