PHP : File Handling

File handling in PHP involves working with files on the server, including reading from files, writing to files, and performing various operations such as creating, deleting, and modifying files. Here's an overview of how to handle files in PHP:

1. Opening and Closing Files:

Opening a File:


You can open a file using fopen(), which returns a file pointer resource.


$myfile = fopen("example.txt", "r") or die("Unable to open file!");
 

  • r: Opens the file for reading.
  • w: Opens the file for writing. Creates a new file or truncates an existing file to zero length.
  • a: Opens the file for writing. Creates a new file or appends to an existing file.
  • x: Creates a new file for writing. Returns FALSE and an error if the file already exists.

Closing a File:


Always remember to close the file when you're done with it.


fclose($myfile);

2. Reading from Files:

Reading Entire File:


Use file_get_contents() to read the entire contents of a file into a string.


$content = file_get_contents("example.txt");
echo $content;

 

Reading Line by Line:


Use fgets() to read a single line from a file.


$myfile = fopen("example.txt", "r") or die("Unable to open file!");
while(!feof($myfile)) {
    echo fgets($myfile) . "<br>";
}
fclose($myfile);

 

3. Writing to Files:

Writing Text:


Use fwrite() to write to a file.


$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Smith\n";
fwrite($myfile, $txt);
fclose($myfile);

 

Appending to Files:


To append to an existing file, use a mode.


$myfile = fopen("existingfile.txt", "a") or die("Unable to open file!");
$txt = "New data appended.\n";
fwrite($myfile, $txt);
fclose($myfile);

 

4. Checking File Existence:

You can check if a file exists using file_exists().


if (file_exists("example.txt")) {
    echo "File exists.";
} else {
    echo "File does not exist.";
}

 

5. Deleting Files:

To delete a file, use unlink().


$file = "file-to-delete.txt";
if (file_exists($file)) {
    unlink($file);
    echo "File deleted.";
} else {
    echo "File does not exist.";
}

 

6. Error Handling:

Always handle errors properly, especially when working with files. You can use feof(), feof(), and other file functions within if conditions to check for errors and handle them gracefully.

Example - Reading and Displaying File Contents:


$filename = "example.txt";
if (file_exists($filename)) {
    $file = fopen($filename, "r") or die("Unable to open file!");
    while (!feof($file)) {
        echo fgets($file) . "<br>";
    }
    fclose($file);
} else {
    echo "File not found!";
}

 

This code reads the contents of example.txt line by line and displays them. It also checks if the file exists before attempting to open it.

AI car modification · buy quality backlinks · guest post service · link building service · link building service · buy backlinks · SEO, GEO, AEO, AIO and LLMO · Bedeutung von Backlinks für Google · winandoffice · aiswapface photo face swap · dofollow backlinks · link building service · guest post service · quality backlinks · guest post service · dofollow backlinks · link building service · dofollow backlinks · buy backlinks · buy quality backlinks · dofollow backlinks · dofollow backlinks · link building service · hacklink satın al · Contextual backlinks: judge the paragraph and destination together · hacklink · hacklink satın al · hacklink panel satın al · jojobet · hacklink satın al · jojobet · jojobet · jojobet · jojobet · jojobet · jojobet · jojobet · jojobet · jojobet · jojobet · jojobet giriş · jojobet güncel · jojobet · jojobet · jojobet · jojobet