PHP’s file() function allows you to read files into an array with ease. Learn through examples how to remove newlines, handle errors, and skip empty lines efficiently.
The file(“example.txt”) function looks for example.txt in the same directory as the PHP file.
If your script is located at:
Windows (XAMPP/WAMP): C:\xampp\htdocs\yourproject\script.php
What if the file is in a different location?
Use the full path:
$lines = file("C:/xampp/htdocs/data/example.txt");
// Windows
PHP file() Function – Read, Remove Newlines, Handle Errors
Step 1: Define the File Path
$filePath = "C:/xampp/env-files/project1/.env";
Here, we define the path of the file we want to work with.
Step 2: Check If the File is Readable
if (!is_readable($filePath)) {
die("Error: File not found or not readable!");
}
Why not use file_exists() here?
While file_exists() checks if a file exists, it doesn’t guarantee that the file is accessible (i.e., readable). Using is_readable() directly checks if the file can be read by the PHP script, which is more specific and efficient in scenarios where file access permissions are a concern.
Using is_readable() avoids unnecessary function calls (like file_exists()) and directly checks the accessibility of the file, streamlining the file validation process.
Step 3: Read the File and Skip Empty Lines
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
file(): This reads the contents of the file into an array where each element corresponds to a line in the file. If the file cannot be read, it returns false.
FILE_IGNORE_NEW_LINES: Removes the newline characters from the end of each line.
FILE_SKIP_EMPTY_LINES: Skips any empty lines, ensuring the output is clean and does not contain unnecessary blank lines.
Step 4: Process Each Line and Escape HTML Entities
foreach ($lines as $line) {
echo htmlspecialchars($line, ENT_QUOTES, 'UTF-8') . "
";
}
htmlspecialchars(): This function is used to convert special characters (like <, >, and &) into their corresponding HTML entities, which helps prevent XSS (Cross-Site Scripting) attacks by ensuring any HTML or JavaScript code embedded in the file content is rendered as plain text.
Create a .env File
Place the .env file outside your project root directory to keep it secure.
For example, you can create the .env file in a directory like C:/xampp/env-files/.
This will keep your credentials safe, as they won’t be publicly accessible.
.env
# Database Configuration
DB_HOST=localhost
DB_NAME=mvc_db
DB_USER=root
DB_PASS=
# App Settings
DEBUG=true
WEBSITE_TITLE=My Website
<script>alert('Hacked!');</script>
Full Optimized Example with Explanation:
<?php
// Define the file path
$filePath = "C:/xampp/env-files/project1/.env";
try {
if (!is_readable($filePath)) {
throw new Exception("Error: File not found or not readable!");
}
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines === false || empty($lines)) {
throw new Exception("Error: Unable to read the file or file is empty!");
}
foreach ($lines as $line) {
echo htmlspecialchars($line, ENT_QUOTES, 'UTF-8') . "
";
}
} catch (Exception $e) {
echo $e->getMessage(); // Display the error message
}
?>