Moodle config.php file appends text on all JavaScript files

Moodle config.php file appends text on all JavaScript files

by Kehinde Adeoya -
Number of replies: 2

I don't know why a text from a file is appended to all the .js files in my project. I have a text file that I read from, which contains a single string, to my surprise, it appends the string to all .js files which disable them from loading. What am I to do right? Please help. Basically, the text in the file is ken4ward. The text is just called in a PHP file.

session_start();
    require_once('send.txt');
    require_once('/Applications/MAMP/htdocs/register/DBConnect.php');

    $myFile = "send.txt";
    $fh = fopen($myFile, 'r');
    $myFileContents = fread($fh, 21);
    fclose($fh);
    $mytrimmedvalue = trim($myFileContents);

The error I'm getting on the console


[Show/hide message details.] ReferenceError: ken4ward is not defined[Learn More] yui_combo.php:1:1
[Show/hide message details.] ReferenceError: ken4wardM is not defined[Learn More] javascript-static.js:1:1
SyntaxError: unexpected token: identifier[Learn More] require.min.js:1:12
[Show/hide message details.] TypeError: require is not a function[Learn More] search.php:1584:1
[Show/hide message details.] TypeError: Y is undefined[Learn More] search.php:1725:14 


Average of ratings: -
In reply to Kehinde Adeoya

Re: Moodle config.php file appends text on all JavaScript files

by Matteo Scaramuccia -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

Hi Kehinde,
require_once() actually include the file, you should remove the line require_once('send.txt'); and check if the file exists using your $fh:

    $myFile = "send.txt";
    $mytrimmedvalue = '';
    $fh = @fopen($myFile, 'r');
    if ($fh) {
        $myFileContents = fread($fh, 21);
        fclose($fh);
        $mytrimmedvalue = trim($myFileContents);
    }

or, better, using exceptions:

    $myFile = "send.txt";
    $mytrimmedvalue = '';
    try {
        if (!file_exists($myFile)) {
            throw new Exception("File '$myFile' not found.");
        }
        $fh = @fopen($myFile, 'rb');
        if (!$fh) {
            throw new Exception("Failed on opening '$myFile'.");
        }
        $myFileContents = fread($fh, 21);
        fclose($fh);
        $mytrimmedvalue = trim($myFileContents);
    } catch ( Exception $e ) {
        // Do something like logging?
    }

or:

    $myFile = "send.txt";
    $mytrimmedvalue = '';
    if (file_exists($myFile)) {
        $fh = @fopen($myFile, 'rb');
        if ($fh) {
            $myFileContents = fread($fh, 21);
            fclose($fh);
            $mytrimmedvalue = trim($myFileContents);
        } else {
            // Do something like logging "Failed on opening '$myFile'."?
        }
    } else {
        // Do something like logging "File '$myFile' not found."?
    }

HTH,
Matteo

 

P.S.: you'll find better help in https://moodle.org/mod/forum/view.php?id=55 , if you'll have the need of new questions about coding wink.

In reply to Matteo Scaramuccia

Re: Moodle config.php file appends text on all JavaScript files

by Kehinde Adeoya -
Thanks, it is really of a great help. Appreciate.