My Intern Hit a PHP Wall — Here’s How We Fixed It Together
It started with a Slack message from our intern:
“I think I broke the script. It worked yesterday, but now it’s throwing fatal errors!”
I smiled. These are the moments I look forward to — not because things are broken, but because they’re teachable.
The script in question was a PHP cron job. Simple task: send invoice emails automatically. But when our intern tried to run it from the command line, he was greeted with a screen full of errors like:
PHP Warning: include(../../model/InvoiceEmail_model.php): Failed to open stream…
PHP Fatal error: Uncaught Error: Class “Base_Controller” not found…
It’s a common issue in PHP projects — especially with cron jobs or command-line scripts.
So I sat down with our intern and we solved it together, step by step.
Here’s what we learned — and how you can fix this issue for good.
The Real Problem: Relative Paths & CLI
PHP was trying to include files like this:
include('../../config/config.php');
include('../../model/InvoiceEmail_model.php');
include('../../libraries/autoload.php');
These worked fine when run via a web request. But in the CLI environment — especially within a chroot — PHP didn’t know where to find these paths.
Why?
Because relative paths are resolved from the current working directory, not from the file that contains the include().
So when our intern ran:
php api/controller/cron/invoices.php
PHP looked for ../../model/InvoiceEmail_model.php relative to the terminal’s current directory, not relative to invoices.php.
The Solution: Meet __DIR__
I introduced our intern to a PHP superpower: the __DIR__ constant.
It always points to the directory of the current file — no matter how or where the file is executed.
We changed:
include('../../model/InvoiceEmail_model.php');
To:
include(__DIR__ . '/../../model/InvoiceEmail_model.php');
Now the path is always correct, whether you run the script from your root directory, a cron job, or a chrooted environment.
Problem solved instantly.
How We Refactored the Codebase
To make this bulletproof, we updated all relevant files.
Step 1: Base Path (Optional)
We could’ve added a constant like:
define('APPROOT', dirname(__DIR__, 2));
Then use:
include(APPROOT . '/model/InvoiceEmail_model.php');
But for simplicity, we just stuck to __DIR__.
Step 2: Updated All Includes
We walked through:
- invoices.php (cron job)
- Base_Controller_cron.php
- app_helper.php
And fixed includes like:
require_once '../../libraries/autoload.php';
To:
require_once __DIR__ . '/../../libraries/autoload.php';
🛠 Step 3: Confirmed Folder Structure
Our project looked like this:
app/
├── api/config/config.php
├── api/helper/app_helper.php
├── api/controller/Base_Controller_cron.php
├── api/controller/cron/invoices.php
├── api/model/User_model.php
├── libraries/autoload.php
We mapped paths from each file using __DIR__, walking up or down the tree as needed.
Lessons I Shared with My Intern
These moments are gold for mentoring, so I made sure to pass on some key takeaways:
- Never trust relative paths blindly — they change based on how/where a script runs.
- Always use __DIR__ in CLI scripts — it makes your includes reliable.
- Validate paths before including — especially in production environments.
- Understand the runtime context — web and CLI behave differently.
He appreciated the clarity and ended up fixing a few other scripts on his own after that.
Bonus: Add Path Checks for Production
Here’s a safe pattern we used in a few places:
$path = __DIR__ . '/../../libraries/autoload.php';
if (file_exists($path)) {
require_once $path;
} else {
error_log("Missing file: $path");
exit("Critical error: Missing autoload.");
}
This ensures your app fails gracefully instead of blowing up with a fatal error.
Final Thoughts: Small Win, Big Confidence
Helping our intern troubleshoot and fix this issue wasn’t just about squashing a bug. It was about building confidence, reinforcing good practices, and turning a roadblock into a lightbulb moment.
So if you or someone on your team runs into the classic “Failed to open stream” error — remember:
It’s usually a path problem. Fix your includes with __DIR__, and you’re golden.
Need Expert PHP Help?
At Webwingz, we don’t just build PHP applications — we build them to scale, run reliably, and solve real business problems.
Whether you’re fixing legacy issues, improving performance, or building something from scratch — our PHP experts are here to help.
Let’s build something great together.
Contact Webwingz for PHP Development Services
One Response
“This post discusses the “failed to open stream” error in PHP, outlining typical reasons such as incorrect file references or server permission conflicts, along with actionable solutions to fix them. The explanation is straightforward and helpful for debugging real-time issues.
Comparable php development services can also be seen on PHP Consulting & Development page of this site: https://mobisoftinfotech.com/services/php-consulting-development. Overall, a practical and easy-to-follow guide for resolving common PHP errors.”