bleah1
bleah1

Reputation: 481

Can't read/write file in php

I've read all I could find about this problem on stackoverflow ! This may be a duplicate, but I couldn't fix my issue: I can't do a thing with files using php.

I've used these commands to have permission:

sudo chown -R www-data:www-data /var/www/mysite 
sudo chmod -R 777 /var/www/mysite

This is the code:

<?php

if(isset( $_POST['save_values'])) { // retrieve the form data by using the element's name attributes value as key 
    if(!empty($_POST['getURL'])) {
        $URL = $_POST['getURL'];
    }

    if(!empty($_POST['getRefreshRate'])) {
        $RefreshRate = $_POST['getRefreshRate'];
    }

    if(!empty($_POST['getBrightness'])) {
        $Brightness = $_POST['getBrightness'];
    }

}

$filename = "/currentSettings.txt";
chmod($filename,0777);
$cSettings = fopen($filename, "r");

if($cSettings) {
    echo "$cSettings exists";
    while(($line = fgets($cSettings)) !== false) {
         $arr = explode("\n", $line);
         $URL = $arr[0];
         $RefreshRate = $arr[1];
         $Brightness = $arr[2];
    }
    fclose($cSettings);
} else {
    echo "File does not exist";
    fclose($cSettings);
    $cSettings = fopen('/currentSettings.txt', 'w');
    fwrite($cSettings,$URL."\n");
    fwrite($cSettings,$RefreshRate."\n");
    fwrite($cSettings,$Brightness."\n");
    fclose($cSettings);
}

?>

It always returns File does not exist. It doesn't write in file and it doesn't read from file. I've even created the file manually inside www/mysite and it still doesn't read it.

I am very new to PHP and I am certain that my code is broken but I couldn't find a fix for it.

This is how I've fixed it:

<?php

$filename = "./inputs.txt";
$currentSettingsFile = fopen($filename, "c+");
$array = [];

if($currentSettingsFile) {
    echo "Reading from $filename";
    $array = explode(PHP_EOL, fread($currentSettingsFile, filesize($filename)));
    var_dump($array);
    $URL = $array[0];
    $RefreshRate = $array[1];
    $Brightness = $array[2];
    $closeFlag = fclose($currentSettingsFile);
}

if(isset( $_POST['save_values'])) { // retrieve the form data by using the element's name attributes value as key 

    if(!empty($_POST['getURL'])) {
        $URL = $_POST['getURL'];
        $array[0] = $URL;        
    }

    if(!empty($_POST['getRefreshRate'])) {
        $RefreshRate = $_POST['getRefreshRate'];
        $array[1] = $RefreshRate;
    }

    if(!empty($_POST['getBrightness'])) {
        $Brightness = $_POST['getBrightness'];
        $array[2] = $Brightness;
    }
}

if(!$closeFlag) fclose($currentSettingsFile);

$currentSettingsFile = fopen($filename, "c");
if($currentSettingsFile) {
    echo "Writing in $filename";
    echo fwrite($currentSettingsFile,$array[0].PHP_EOL);
    fwrite($currentSettingsFile,$array[1].PHP_EOL);
    fwrite($currentSettingsFile,$array[2].PHP_EOL);
    fclose($currentSettingsFile);
}

?>

Upvotes: 2

Views: 464

Answers (1)

farhang
farhang

Reputation: 407

The $handle does not seem to be defined.

Here is a version that seems to work while staying as close as possible to your example, but with an important caveat:

$filename = "./currentSettings.txt";

if(is_file($filename)) {
    echo "$filename exists";
        $arr = explode("\n", file_get_contents($filename));
        $URL = $arr[0];
        $RefreshRate = $arr[1];
        $Brightness = $arr[2];

} else {
    echo "File does not exist";
    $URL = ''."\n";
    $RefreshRate = ''."\n";
    $Brightness= ''."\n";
    file_put_contents($filename, $URL.$RefreshRate.$Brightness);
    chmod($filename,0777);
}

If the file does not exist, this will create it. But, since $URL, $RefreshRate, $Brightness are not defined, it only adds three empty lines to the file.

I am not sure about the rest of the project's structure but you might want to define some placeholders to fill in the file.

Upvotes: 1

Related Questions