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

时间:2015-06-29 12:42:00

标签: 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);
}

这些是我收到的错误:

../../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

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

问题是,我在同一文件夹中的另一个脚本中使用相同的定义,从那里我可以访问这些文件。所以路径是正确的,只有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 

编辑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));

这将导致:

/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

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

1 个答案:

答案 0 :(得分:3)

好吧,经过一次大的努力,我找到了解决方案,given by @naviciroel

  

之前我遇到了同样的错误,但我不知道我的解决方案是否能解决您的问题,您需要删除&#34; \ 0&#34;尝试替换它:

     

$ cleaning = strval(str_replace(&#34; \ 0&#34;,&#34;&#34;,$ buttons_first));