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.
phương pháp
- File tồn tại[]. Nó là một chức năng sẵn có được sử dụng để kiểm tra xem một tệp hoặc thư mục có tồn tại hay không
- is_dir[]. Nó cũng được sử dụng để kiểm tra xem một tập tin hoặc thư mục có tồn tại hay không
- mkdir[]. Chức năng này tạo một thư mục
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 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ụ
PHP
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 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ố
- đường dẫn. Nó là một tham số bắt buộc chỉ định đường dẫn
- cách thức. Đây là một tham số tùy chọn chỉ định quyền. Tham số chế độ bao gồm bốn số và theo mặc định, chế độ là 0777
- Số đầu tiên luôn bằng 0
- Số thứ hai chỉ định quyền cho chủ sở hữu
- Số thứ ba chỉ định quyền cho nhóm người dùng của chủ sở hữu
- Số thứ tư chỉ định quyền cho mọi người khác
- đệ quy. Đây là một tham số tùy chọn có thể được sử dụng để đặt chế độ đệ quy
- định nghĩa bài văn. Đây là một tham số tùy chọn chỉ định hành vi của luồng
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 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[]