phppathrelative-pathfile-put-contents

file_put_contents() expects parameter 1 to be a valid path, string given


I have the following simple script (stripped down):

define("LANG_DIR", "../../app/lang/");
// define("LANG_DIR", "/var/www/html/app/lang/");

// #############
// Some parsing of an CSV file, from with we will create .php files
// #############

foreach ($fileContent as $fileName => $value) {
    $fileString = FILE_START;

    foreach ($value as $arrayKey => $arrayValue) {
        $fileString .= TAB . "'" . $arrayKey . "'" . TAB . TAB . "=>" . TAB . TAB .  "'" . $arrayValue . "'," . NL;
    }

    $fileString .= FILE_END;

    $filePath = trim(LANG_DIR . $desLang . "/" . $fileName . ".php");
    echo $filePath;
    file_put_contents($filePath, $fileString);
}

And these are the error's I receive:

../../app/lang/de/behandeling.php
Warning: file_put_contents() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 61
../../app/lang/de/bewaarplaats.php
Warning: file_put_contents() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 61
../../app/lang/de/cron.php
Warning: file_put_contents() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 61
../../app/lang/de/gebruikers.php
Warning: file_put_contents() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 61
../../app/lang/de/handelshuis.php
Warning: file_put_contents() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 61
../../app/lang/de/heftrucks.php
Warning: file_put_contents() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 61
../../app/lang/de/history.php
Warning: file_put_contents() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 61

I get the same error when I use the other (commented) define.

The thing is, that I use the same define's in an another script which is in the same folder, and from there I can access the files. So the path's are right, only file_put_contents can't find it.

edit as requested by @e-Learner here's the output I get when I use

var_dump(is_file(LANG_DIR . $desLang . "/" . $fileName . ".php"));

// This will output:

Warning: is_file() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 63
NULL 
Warning: is_file() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 63
NULL 
Warning: is_file() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 63
NULL 
Warning: is_file() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 63
NULL 
Warning: is_file() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 63
NULL 
Warning: is_file() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 63
NULL 

edit 2 Changed some more code:

$filePath = trim(LANG_DIR . $desLang . "/" . $fileName . ".php");

echo $filePath . "<br />";
var_dump(is_file("/var/www/html/app/lang/de/behandeling.php"));
var_dump(is_file($filePath));

This will result in:

/var/www/html/app/lang/de/behandeling.php
/var/www/html/app/lang/de/behandeling.php 
bool(true) 
Warning: is_file() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 68

As you can see, the script can access the file when I use an whole string. But when I create the string on the fly (by inserting the $fileName), it goes wrong... But both strings are the same


Solution

  • Alright, after one big headace, I've found the solution, given by @naviciroel

    I got the same error before but I don't know if this solution of mine works on your problem you need to remove the "\0" try replace it:

    $cleaned = strval(str_replace("\0", "", $buttons_first));