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.  

phương pháp

  1. 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
  2. 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
  3. 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 directory
7

The file path doesn't exists in GeeksforGeeks directory
8

The file path doesn't exists in GeeksforGeeks directory
9

The file path doesn't exists in GeeksforGeeks directory
0
The file path doesn't exists in GeeksforGeeks directory
1
The file path doesn't exists in GeeksforGeeks directory
2
The file path doesn't exists in GeeksforGeeks directory
3

The file path doesn't exists in GeeksforGeeks directory
4

The file path doesn't exists in GeeksforGeeks directory
5
The file path doesn't exists in GeeksforGeeks directory
6
The file path doesn't exists in GeeksforGeeks directory
7______16
The file path doesn't exists in GeeksforGeeks directory
0
The file path doesn't exists in GeeksforGeeks directory
10

The file path doesn't exists in GeeksforGeeks directory
11
The file path doesn't exists in GeeksforGeeks directory
12
The file path doesn't exists in GeeksforGeeks directory
13
The file path doesn't exists in GeeksforGeeks directory
3

The file path doesn't exists in GeeksforGeeks directory
15

The file path doesn't exists in GeeksforGeeks directory
16
The file path doesn't exists in GeeksforGeeks directory
17

The file path doesn't exists in GeeksforGeeks directory
11
The file path doesn't exists in GeeksforGeeks directory
12
The file path doesn't exists in GeeksforGeeks directory
10
The file path doesn't exists in GeeksforGeeks directory
3

The file path doesn't exists in GeeksforGeeks directory
15

The file path doesn't exists in GeeksforGeeks directory
4

The file path doesn't exists in GeeksforGeeks directory
14

Đầ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 directory
1

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 directory
15

The file path doesn't exists in GeeksforGeeks directory
16

The file path doesn't exists in GeeksforGeeks directory
17
The file path doesn't exists in GeeksforGeeks directory
1____219
The file path doesn't exists in GeeksforGeeks directory
3

The file path doesn't exists in GeeksforGeeks directory
4

The file path doesn't exists in GeeksforGeeks directory
16

The file path doesn't exists in GeeksforGeeks directory
63

The file path doesn't exists in GeeksforGeeks directory
5
The file path doesn't exists in GeeksforGeeks directory
6____366______16
The file path doesn't exists in GeeksforGeeks directory
17
The file path doesn't exists in GeeksforGeeks directory
69

The file path doesn't exists in GeeksforGeeks directory
11
The file path doesn't exists in GeeksforGeeks directory
12
The file path doesn't exists in GeeksforGeeks directory
6____463
The file path doesn't exists in GeeksforGeeks directory
64

The file path doesn't exists in GeeksforGeeks directory
16

The file path doesn't exists in GeeksforGeeks directory
11
The file path doesn't exists in GeeksforGeeks directory
12
The file path doesn't exists in GeeksforGeeks directory
6____469
The file path doesn't exists in GeeksforGeeks directory
64

The file path doesn't exists in GeeksforGeeks directory
4

The file path doesn't exists in GeeksforGeeks directory
14

Đầu ra

The file path doesn't exists in GeeksforGeeks directory
1

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 directory
6

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 directory
      7

      The file path doesn't exists in GeeksforGeeks directory
      4

      The file path doesn't exists in GeeksforGeeks directory
      65

      The file path doesn't exists in GeeksforGeeks directory
      66

      The file path doesn't exists in GeeksforGeeks directory
      67

      The file path doesn't exists in GeeksforGeeks directory
      68
      The file path doesn't exists in GeeksforGeeks directory
      6____670
      The file path doesn't exists in GeeksforGeeks directory
      71

      The file path doesn't exists in GeeksforGeeks directory
      4

      The file path doesn't exists in GeeksforGeeks directory
      14

      đầu ra

      The file path doesn't exists in GeeksforGeeks directory
      6

      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.

Chủ Đề