• Uncategorized

About php : php-not-able-to-write-file-on-ubuntu-EC2-instance

Question Detail


The file temp/71076cc.php is not writable

Code (compiler.php):

$random = substr(md5(mt_rand()), 0, 7);
$filePath = "temp/" . $random . ".php";
if (is_writable($filePath)) {
    if (!$fp = fopen($filePath, 'a')) {
         echo "Cannot open file ($filename)";
    if (fwrite($fp, $code) === FALSE) {
        echo "Cannot write to file ($filePath)";
    echo "Success, wrote ($code) to file ($filePath)";
} else {
    echo "The file $filePath is not writable";

Dir structure of ide directory inside which is inside /var/www/html:

├── app
│   ├── compiler.php
│   ├── temp
│   │   ├── a9211d0.php
│   │   └── c7d561e.php
│   └── test.php
└── ui
    ├── css
    │   └── style.css
    ├── ide.html
    └── js
        ├── ide.js
        └── lib

Things I tried:

  1. Changing ownership to www-data :

sudo chown -R www-data:www-data /var/www/html/ide/

  1. Changing permission to directory:

sudo chmod -R 777 /var/www/html/ide/

  1. Changing user which apache uses (in /etc/apache2/envvars):
export APACHE_RUN_USER=ubuntu
export APACHE_RUN_GROUP=ubuntu

as well as

export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data

(Both doesnt work)

Kindly suggest any more tweaks to try.


Currently two files which are seen in temp folder was not made by running compiler.php code. They were made manually. However compiler.php will create such files when called from webserver.

Question Answer

No answer for now.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.