Hướng dẫn which function displays the contents of a file without needing to open it php? - chức năng nào hiển thị nội dung của một tập tin mà không cần mở nó php?

Trong hướng dẫn này, bạn sẽ tìm hiểu cách tạo, truy cập (hoặc đọc) và thao tác các tệp một cách linh hoạt bằng các chức năng hệ thống tệp của PHP.

Show

Làm việc với các tệp trong PHP

Vì PHP là ngôn ngữ lập trình phía máy chủ, nó cho phép bạn làm việc với các tệp và thư mục được lưu trữ trên máy chủ web. Trong hướng dẫn này, bạn sẽ tìm hiểu cách tạo, truy cập và thao tác các tệp trên máy chủ web của bạn bằng các chức năng hệ thống tệp PHP.

Mở tệp có chức năng Php <?php $file = "data.txt"; // Check the existence of file if(file_exists($file)){ // Attempt to open the file $handle = fopen($file, "r"); } else{ echo "ERROR: File does not exist."; } ?>3

Để làm việc với một tệp bạn trước tiên cần mở tệp. Hàm Php

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Attempt to open the file
    $handle = fopen($file, "r");
} else{
    echo "ERROR: File does not exist.";
}
?>
3 được sử dụng để mở tệp. Cú pháp cơ bản của chức năng này có thể được đưa ra với:

Tham số đầu tiên được chuyển đến

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Attempt to open the file
    $handle = fopen($file, "r");
} else{
    echo "ERROR: File does not exist.";
}
?>
3 Chỉ định tên của tệp bạn muốn mở và tham số thứ hai chỉ định trong chế độ mà tệp sẽ được mở. Ví dụ:

<?php
$handle = fopen("data.txt", "r");
?>

Tệp có thể được mở ở một trong các chế độ sau:

Chế độNhững gì nó làm
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Attempt to open the file
    $handle = fopen($file, "r");
} else{
    echo "ERROR: File does not exist.";
}
?>
6
Mở tệp chỉ để đọc.
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Attempt to open the file
    $handle = fopen($file, "r");
} else{
    echo "ERROR: File does not exist.";
}
?>
7
Mở tệp để đọc và viết.
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Attempt to open the file
    $handle = fopen($file, "r");
} else{
    echo "ERROR: File does not exist.";
}
?>
8
Mở tệp chỉ để viết và xóa nội dung của tệp. Nếu tệp không tồn tại, PHP sẽ cố gắng tạo nó.
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Attempt to open the file
    $handle = fopen($file, "r");
} else{
    echo "ERROR: File does not exist.";
}
?>
9
Mở tệp để đọc và viết và xóa nội dung của tệp. Nếu tệp không tồn tại, PHP sẽ cố gắng tạo nó.
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    /* Some code to be executed */
        
    // Closing the file handle
    fclose($handle);
} else{
    echo "ERROR: File does not exist.";
}
?>
0
Nối. Mở tập tin chỉ để viết. Bảo tồn nội dung tệp bằng cách ghi vào cuối tệp. Nếu tệp không tồn tại, PHP sẽ cố gắng tạo nó.
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    /* Some code to be executed */
        
    // Closing the file handle
    fclose($handle);
} else{
    echo "ERROR: File does not exist.";
}
?>
1
Đọc/nối. Mở tập tin để đọc và viết. Bảo tồn nội dung tệp bằng cách ghi vào cuối tệp. Nếu tệp không tồn tại, PHP sẽ cố gắng tạo nó.
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    /* Some code to be executed */
        
    // Closing the file handle
    fclose($handle);
} else{
    echo "ERROR: File does not exist.";
}
?>
2
Mở tệp chỉ để viết. Trả về
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    /* Some code to be executed */
        
    // Closing the file handle
    fclose($handle);
} else{
    echo "ERROR: File does not exist.";
}
?>
3 và tạo lỗi nếu tệp đã tồn tại. Nếu tệp không tồn tại, PHP sẽ cố gắng tạo nó.
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    /* Some code to be executed */
        
    // Closing the file handle
    fclose($handle);
} else{
    echo "ERROR: File does not exist.";
}
?>
4
Mở tệp để đọc và viết; Nếu không, nó có hành vi tương tự như 'x'.

Nếu bạn cố gắng mở một tệp không tồn tại, PHP sẽ tạo thông báo cảnh báo. Vì vậy, để tránh các thông báo lỗi này, bạn nên luôn thực hiện kiểm tra đơn giản xem tệp hoặc thư mục có tồn tại hay không trước khi cố gắng truy cập nó, với chức năng PHP

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    /* Some code to be executed */
        
    // Closing the file handle
    fclose($handle);
} else{
    echo "ERROR: File does not exist.";
}
?>
5.

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Attempt to open the file
    $handle = fopen($file, "r");
} else{
    echo "ERROR: File does not exist.";
}
?>

Mẹo: Hoạt động trên các tập tin và thư mục dễ bị lỗi. Vì vậy, đó là một thực tế tốt để thực hiện một số hình thức kiểm tra lỗi để nếu xảy ra lỗi, tập lệnh của bạn sẽ xử lý lỗi một cách duyên dáng. Xem hướng dẫn về xử lý lỗi PHP. Operations on files and directories are prone to errors. So it's a good practice to implement some form of error checking so that if an error occurs your script will handle the error gracefully. See the tutorial on PHP error handling.


Đóng một tệp với chức năng PHP <?php $file = "data.txt"; // Check the existence of file if(file_exists($file)){ // Open the file for reading $handle = fopen($file, "r") or die("ERROR: Cannot open the file."); /* Some code to be executed */ // Closing the file handle fclose($handle); } else{ echo "ERROR: File does not exist."; } ?>6

Khi bạn đã làm việc xong với một tệp, nó cần phải đóng. Hàm

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    /* Some code to be executed */
        
    // Closing the file handle
    fclose($handle);
} else{
    echo "ERROR: File does not exist.";
}
?>
6 được sử dụng để đóng tệp, như được hiển thị trong ví dụ sau:

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    /* Some code to be executed */
        
    // Closing the file handle
    fclose($handle);
} else{
    echo "ERROR: File does not exist.";
}
?>

Lưu ý: Mặc dù PHP tự động đóng tất cả các tệp đang mở khi tập lệnh chấm dứt, nhưng đó là một thông lệ tốt để đóng một tệp sau khi thực hiện tất cả các hoạt động. Although PHP automatically closes all open files when script terminates, but it's a good practice to close a file after performing all the operations.


Đọc từ các tệp có chức năng Php <?php $file = "data.txt"; // Check the existence of file if(file_exists($file)){ // Open the file for reading $handle = fopen($file, "r") or die("ERROR: Cannot open the file."); /* Some code to be executed */ // Closing the file handle fclose($handle); } else{ echo "ERROR: File does not exist."; } ?>8

Bây giờ bạn đã hiểu cách mở và đóng các tập tin. Trong phần sau, bạn sẽ tìm hiểu cách đọc dữ liệu từ một tệp. PHP có một số chức năng để đọc dữ liệu từ một tệp. Bạn có thể đọc chỉ từ một ký tự đến toàn bộ tệp với một thao tác duy nhất.

Đọc số lượng ký tự cố định

Hàm

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    /* Some code to be executed */
        
    // Closing the file handle
    fclose($handle);
} else{
    echo "ERROR: File does not exist.";
}
?>
8 có thể được sử dụng để đọc một số ký tự được chỉ định từ một tệp. Cú pháp cơ bản của chức năng này có thể được đưa ra với.

fread (tay cầm tập tin, độ dài tính bằng byte)(file handle, length in bytes)

Hàm này có hai tham số - một tay cầm tệp và số byte để đọc. Ví dụ sau đọc 20 byte từ tệp "data.txt" bao gồm cả khoảng trắng. Chúng ta hãy giả sử tệp "data.txt" chứa một đoạn văn bản "con cáo màu nâu nhanh chóng nhảy qua con chó lười biếng."

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    // Read fixed number of bytes from the file
    $content = fread($handle, "20");
        
    // Closing the file handle
    fclose($handle);
        
    // Display the file content 
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>

Ví dụ trên sẽ tạo ra đầu ra sau:

Chú cáo nâu nhanh nhẹn

Đọc toàn bộ nội dung của một tệp

Hàm

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    /* Some code to be executed */
        
    // Closing the file handle
    fclose($handle);
} else{
    echo "ERROR: File does not exist.";
}
?>
8 có thể được sử dụng trong liên hợp với hàm
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    // Read fixed number of bytes from the file
    $content = fread($handle, "20");
        
    // Closing the file handle
    fclose($handle);
        
    // Display the file content 
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>
1 để đọc toàn bộ tệp cùng một lúc. Hàm
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    // Read fixed number of bytes from the file
    $content = fread($handle, "20");
        
    // Closing the file handle
    fclose($handle);
        
    // Display the file content 
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>
1 trả về kích thước của tệp trong byte.

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    // Reading the entire file
    $content = fread($handle, filesize($file));
        
    // Closing the file handle
    fclose($handle);
        
    // Display the file content
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>

Ví dụ trên sẽ tạo ra đầu ra sau:

Chú cáo nâu nhanh nhẹn

Đọc toàn bộ nội dung của một tệp

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Reads and outputs the entire file
    readfile($file) or die("ERROR: Cannot open the file.");
} else{
    echo "ERROR: File does not exist.";
}
?>

Ví dụ trên sẽ tạo ra đầu ra sau:

Chú cáo nâu nhanh nhẹn

Đọc toàn bộ nội dung của một tệp

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Reading the entire file into a string
    $content = file_get_contents($file) or die("ERROR: Cannot open the file.");
        
    // Display the file content 
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>

Thêm một phương pháp đọc toàn bộ dữ liệu từ một tệp là hàm

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    // Read fixed number of bytes from the file
    $content = fread($handle, "20");
        
    // Closing the file handle
    fclose($handle);
        
    // Display the file content 
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>
5 của PHP. Nó thực hiện một công việc tương tự như hàm
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    // Read fixed number of bytes from the file
    $content = fread($handle, "20");
        
    // Closing the file handle
    fclose($handle);
        
    // Display the file content 
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>
4, nhưng nó trả về nội dung tệp dưới dạng một mảng các dòng, thay vì một chuỗi duy nhất. Mỗi phần tử của mảng được trả về tương ứng với một dòng trong tệp.

Để xử lý dữ liệu tệp, bạn cần lặp lại trên mảng bằng cách sử dụng vòng lặp foreach. Dưới đây là một ví dụ, đọc một tệp vào một mảng và sau đó hiển thị nó bằng cách sử dụng vòng lặp:

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Reading the entire file into an array
    $arr = file($file) or die("ERROR: Cannot open the file.");
    foreach($arr as $line){
        echo $line;
    }
} else{
    echo "ERROR: File does not exist.";
}
?>


Viết các tệp bằng hàm Php <?php $file = "data.txt"; // Check the existence of file if(file_exists($file)){ // Open the file for reading $handle = fopen($file, "r") or die("ERROR: Cannot open the file."); // Read fixed number of bytes from the file $content = fread($handle, "20"); // Closing the file handle fclose($handle); // Display the file content echo $content; } else{ echo "ERROR: File does not exist."; } ?>7

Tương tự, bạn có thể ghi dữ liệu vào một tệp hoặc nối vào một tệp hiện có bằng hàm PHP

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    // Read fixed number of bytes from the file
    $content = fread($handle, "20");
        
    // Closing the file handle
    fclose($handle);
        
    // Display the file content 
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>
7. Cú pháp cơ bản của chức năng này có thể được đưa ra với:

fwrite (xử lý tệp, chuỗi)(file handle, string)

Hàm

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    // Read fixed number of bytes from the file
    $content = fread($handle, "20");
        
    // Closing the file handle
    fclose($handle);
        
    // Display the file content 
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>
7 có hai tham số - một tay cầm tệp và chuỗi dữ liệu sẽ được viết, như đã trình bày trong ví dụ sau:

<?php
$file = "note.txt";
    
// String of data to be written
$data = "The quick brown fox jumps over the lazy dog.";
    
// Open the file for writing
$handle = fopen($file, "w") or die("ERROR: Cannot open the file.");
    
// Write data to the file
fwrite($handle, $data) or die ("ERROR: Cannot write the file.");
    
// Closing the file handle
fclose($handle);
    
echo "Data written to the file successfully.";
?>

Trong ví dụ trên, nếu tệp "note.txt" không tồn tại PHP sẽ tự động tạo nó và ghi dữ liệu. Nhưng, nếu tệp "note.txt" đã tồn tại, PHP sẽ xóa nội dung của tệp này, nếu nó có bất kỳ, trước khi viết dữ liệu mới, tuy nhiên nếu bạn chỉ muốn nối lại tệp và bảo tồn các nội dung hiện có chỉ sử dụng chế độ

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    /* Some code to be executed */
        
    // Closing the file handle
    fclose($handle);
} else{
    echo "ERROR: File does not exist.";
}
?>
0 thay vì
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Attempt to open the file
    $handle = fopen($file, "r");
} else{
    echo "ERROR: File does not exist.";
}
?>
8 trong ví dụ trên.

Một cách khác là sử dụng hàm

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    // Reading the entire file
    $content = fread($handle, filesize($file));
        
    // Closing the file handle
    fclose($handle);
        
    // Display the file content
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>
2. Nó là đối tác của hàm
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    // Read fixed number of bytes from the file
    $content = fread($handle, "20");
        
    // Closing the file handle
    fclose($handle);
        
    // Display the file content 
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>
4 và cung cấp một phương thức dễ dàng để ghi dữ liệu vào một tệp mà không cần phải mở nó. Hàm này chấp nhận tên và đường dẫn đến một tệp cùng với dữ liệu sẽ được ghi vào tệp. Đây là một ví dụ:

<?php
$file = "note.txt";
    
// String of data to be written
$data = "The quick brown fox jumps over the lazy dog.";
    
// Write data to the file
file_put_contents($file, $data) or die("ERROR: Cannot write the file.");
    
echo "Data written to the file successfully.";
?>

Nếu tệp được chỉ định trong hàm

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    // Reading the entire file
    $content = fread($handle, filesize($file));
        
    // Closing the file handle
    fclose($handle);
        
    // Display the file content
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>
2 đã tồn tại, PHP sẽ ghi đè lên nó theo mặc định. Nếu bạn muốn bảo quản nội dung của tệp, bạn có thể chuyển cờ
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    // Reading the entire file
    $content = fread($handle, filesize($file));
        
    // Closing the file handle
    fclose($handle);
        
    // Display the file content
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>
5 đặc biệt dưới dạng tham số thứ ba cho hàm
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    // Reading the entire file
    $content = fread($handle, filesize($file));
        
    // Closing the file handle
    fclose($handle);
        
    // Display the file content
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>
2. Nó sẽ chỉ đơn giản là nối dữ liệu mới vào tệp thay vì vượt qua nó. Đây là một ví dụ:

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Attempt to open the file
    $handle = fopen($file, "r");
} else{
    echo "ERROR: File does not exist.";
}
?>
0


Đổi tên các tệp với chức năng PHP <?php $file = "data.txt"; // Check the existence of file if(file_exists($file)){ // Open the file for reading $handle = fopen($file, "r") or die("ERROR: Cannot open the file."); // Reading the entire file $content = fread($handle, filesize($file)); // Closing the file handle fclose($handle); // Display the file content echo $content; } else{ echo "ERROR: File does not exist."; } ?>7

Bạn có thể đổi tên tệp hoặc thư mục bằng hàm

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    // Reading the entire file
    $content = fread($handle, filesize($file));
        
    // Closing the file handle
    fclose($handle);
        
    // Display the file content
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>
7 của PHP, như thế này:

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Attempt to open the file
    $handle = fopen($file, "r");
} else{
    echo "ERROR: File does not exist.";
}
?>
1


Xóa các tệp có chức năng PHP <?php $file = "data.txt"; // Check the existence of file if(file_exists($file)){ // Open the file for reading $handle = fopen($file, "r") or die("ERROR: Cannot open the file."); // Reading the entire file $content = fread($handle, filesize($file)); // Closing the file handle fclose($handle); // Display the file content echo $content; } else{ echo "ERROR: File does not exist."; } ?>9

Bạn có thể xóa các tệp hoặc thư mục bằng hàm

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    // Reading the entire file
    $content = fread($handle, filesize($file));
        
    // Closing the file handle
    fclose($handle);
        
    // Display the file content
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>
9 của PHP, như thế này:

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Attempt to open the file
    $handle = fopen($file, "r");
} else{
    echo "ERROR: File does not exist.";
}
?>
2

Trong chương tiếp theo, chúng tôi sẽ tìm hiểu thêm về các thư mục phân tích cú pháp hoặc thư mục trong PHP.


Hàm hệ thống tập tin PHP

Bảng sau đây cung cấp tổng quan về một số hàm hệ thống tập tin PHP hữu ích khác có thể được sử dụng để đọc và viết các tệp một cách linh hoạt.

Hàm sốSự mô tả
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Reads and outputs the entire file
    readfile($file) or die("ERROR: Cannot open the file.");
} else{
    echo "ERROR: File does not exist.";
}
?>
1
Đọc một ký tự duy nhất tại một thời điểm.
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Reads and outputs the entire file
    readfile($file) or die("ERROR: Cannot open the file.");
} else{
    echo "ERROR: File does not exist.";
}
?>
2
Đọc một dòng duy nhất tại một thời điểm.
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Reads and outputs the entire file
    readfile($file) or die("ERROR: Cannot open the file.");
} else{
    echo "ERROR: File does not exist.";
}
?>
3
Đọc một dòng các giá trị phân tách bằng dấu phẩy.
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Reads and outputs the entire file
    readfile($file) or die("ERROR: Cannot open the file.");
} else{
    echo "ERROR: File does not exist.";
}
?>
4
Trả về loại tệp.
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Reads and outputs the entire file
    readfile($file) or die("ERROR: Cannot open the file.");
} else{
    echo "ERROR: File does not exist.";
}
?>
5
Kiểm tra xem kết thúc của tập tin đã đạt được.
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Reads and outputs the entire file
    readfile($file) or die("ERROR: Cannot open the file.");
} else{
    echo "ERROR: File does not exist.";
}
?>
6
Kiểm tra xem tệp có phải là một tập tin thông thường không.
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Reads and outputs the entire file
    readfile($file) or die("ERROR: Cannot open the file.");
} else{
    echo "ERROR: File does not exist.";
}
?>
7
Kiểm tra xem tệp có phải là một thư mục hay không.
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Reads and outputs the entire file
    readfile($file) or die("ERROR: Cannot open the file.");
} else{
    echo "ERROR: File does not exist.";
}
?>
8
Kiểm tra xem tệp có thể thực thi hay không.
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Reads and outputs the entire file
    readfile($file) or die("ERROR: Cannot open the file.");
} else{
    echo "ERROR: File does not exist.";
}
?>
9
Trả về tên đường dẫn tuyệt đối chính thức.
<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Reading the entire file into a string
    $content = file_get_contents($file) or die("ERROR: Cannot open the file.");
        
    // Display the file content 
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>
0
Xóa một thư mục trống.

Vui lòng kiểm tra tham chiếu hệ thống tập tin PHP cho các chức năng hệ thống tập tin PHP hữu ích khác.

Chức năng PHP nào được sử dụng để đọc nội dung của một tệp?

Php Read File - fread () hàm fread () đọc từ một tệp mở. Tham số đầu tiên của fread () chứa tên của tệp để đọc và tham số thứ hai chỉ định số lượng byte tối đa để đọc.fread() The fread() function reads from an open file. The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.

Chức năng nào được sử dụng để ghi vào một tệp mở trong PHP?

PHP ghi vào tệp - fwrite () hàm fwrite () được sử dụng để ghi vào tệp. Tham số đầu tiên của fwrite () chứa tên của tệp để ghi vào và tham số thứ hai là chuỗi được viết.fwrite() The fwrite() function is used to write to a file. The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.

Điều gì được bao gồm () và yêu cầu () các hàm trong PHP?

Câu lệnh bao gồm (hoặc yêu cầu) lấy tất cả các văn bản/mã/đánh dấu tồn tại trong tệp được chỉ định và sao chép nó vào tệp sử dụng câu lệnh bao gồm.Bao gồm các tệp rất hữu ích khi bạn muốn bao gồm cùng một PHP, HTML hoặc văn bản trên nhiều trang của một trang web.. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

Làm thế nào được mở và đóng trong PHP?

Để tạo một tệp mới hoặc để mở một tệp hiện có, chúng tôi sử dụng hàm fopen () do PHP được cung cấp và để đóng hàm fclose () tài nguyên fclose () được sử dụng..