file_put_contents() 期望参数 1 是有效路径,给定字符串

Mat*_*ght 2 php path relative-path file-put-contents

我有以下简单的脚本(精简):

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);
}
Run Code Online (Sandbox Code Playgroud)

这些是我收到的错误:

../../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
Run Code Online (Sandbox Code Playgroud)

当我使用其他(注释的)定义时,我得到同样的错误。

问题是,我在同一文件夹中的另一个脚本中使用相同的定义,并且从那里我可以访问这些文件。所以路径是对的,只是file_put_contents找不到。

按照@e-Learner的要求进行编辑 这是我使用时得到的输出

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 
Run Code Online (Sandbox Code Playgroud)

编辑2更改了更多代码:

$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));
Run Code Online (Sandbox Code Playgroud)

这将导致:

/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
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,当我使用整个字符串时,脚本可以访问该文件。但是当我动态创建字符串(通过插入 )时$fileName,它出错了......但是两个字符串是相同的

Mat*_*ght 5

好吧,在经历了一大麻烦之后,我找到了解决方案,由 @naviciroel 给出

我之前遇到过同样的错误,但我不知道我的这个解决方案是否适用于您的问题,您需要删除“\0”尝试替换它:

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