failed to open stream: Permission denied in /opt/lampp/htdocs

failed to open stream: Permission denied in /opt/lampp/htdocs

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Root Cause: Linux File Ownership and Permissions

When you encounter failed to open stream: Permission denied while running a script in /opt/lampp/htdocs, the error message is usually unambiguous, but the solution requires understanding how Linux handles file system permissions rather than just guessing which flag to change. In your specific case, the error originates from line 38 of login.php. You are attempting to append data to login.txt using file_put_contents() with the flags FILE_APPEND | LOCK_EX. While the logic for locking and appending is sound, the operating system is refusing to grant the execution rights because the current user running the PHP process does not own the target file or directory.

The path /opt/lampp/htdocs reveals a critical configuration choice you made at installation time. By default, XAMPP (and similarly structured stacks like LAMP) often installs web root directories owned by the root user. This is a security best practice in production environments; it prevents any non-root user from accidentally overwriting sensitive system files. However, when you run PHP scripts via Apache or CLI under your standard user account (e.g., yourname), that user cannot write to files owned by root. The error occurs because the file login.txt likely resides in a directory where only root has write permissions.

Diagnosing the Ownership Mismatch

Before altering permissions, you must verify exactly who owns the file and what group it belongs to. Run the following command in your terminal:

ls -l /opt/lampp/htdocs/login.txt

You will likely see an output similar to this:

-rw-r--r-- 1 root root 0 /opt/lampp/htdocs/login.txt

The first column shows the permissions (rw-r--r--), but the second and third columns reveal the issue: root is the owner, and root is the group. Your user account does not match either. Even if you change the file permissions to 777, which grants everyone read, write, and execute access, this is a terrible idea for a production server. It exposes your entire application directory to arbitrary modification by any local user or remote process that gains shell access.

The Recommended Solution: Fix Ownership, Not Permissions

The correct approach is to transfer ownership of the specific file (or the entire directory tree) to your current user, rather than changing permission bits to allow universal write access. To do this safely, identify your username and then apply chown:

sudo chown -R $USER:$USER /opt/lampp/htdocs

The -R flag is crucial here because it recursively changes the ownership of all subdirectories and files within /opt/lampp/htdocs. If you only change the file login.txt, your next script might fail when trying to write to a new file in a different subdirectory, as those parent directories will still be owned by root with restrictive permissions.

After executing this command, verify that the ownership has changed:

ls -l /opt/lampp/htdocs/login.txt

You should now see your username listed where root used to be. Your PHP script should immediately resolve the "Permission denied" error because the process running the script (likely www-data or your user depending on Apache configuration) will have write access to the file.

Alternative Approaches and Trade-offs

If you cannot change ownership due to strict security policies, you must adjust the permissions of the specific directory containing the file. Using chmod 775 on the parent directory allows the owner (root) and the group (which you would add your user to via sudo usermod -aG <yourgroup> youruser) to have write access. However, this is more complex to manage as users change.

There is another common pitfall in this scenario: file locking. Your code uses LOCK_EX (exclusive lock). This flag attempts to prevent race conditions where two processes write to the file simultaneously. While useful for data integrity, ensure that the lock isn't being held too long or causing issues if multiple instances of Apache are running on different ports with shared memory configurations. In most simple login validation scenarios involving a single login.txt, this might be over-engineering, but it does not cause permission errors directly; those are strictly an ownership issue.

Why This Matters for Production

Resolving the permission error is just the first step. Moving forward, consider how you manage state in web applications. Storing user credentials and session data in plain text files like login.txt is generally discouraged even for development. It introduces significant risks regarding data integrity, concurrency, and security. If a malicious actor gains read access to that file (which becomes possible if permissions are misconfigured), they can replay login attempts or extract passwords.

For robust applications, especially those aiming for production deployment, consider migrating from file-based storage to database systems. This approach is standard practice in frameworks like Laravel, where data persistence and relationship management are handled natively. For more information on secure data handling practices within modern PHP ecosystems, resources like https://laravelcompany.com offer guides on why moving away from flat files to managed storage solutions is essential for scalability and security.

By correcting the file ownership, you maintain a secure environment where the root user owns the system files but your application logic can function correctly under your user context. This balance between security and functionality is the hallmark of professional server administration.

Our network